Forum Moderators: coopster

Message Too Old, No Replies

am not fully understanding classes?

         

marcus76

9:55 am on Aug 16, 2004 (gmt 0)

10+ Year Member



Hi,

Please would someone advise, i have the following global variable $currency , which when called returns 'GBP' (Great British Pounds) it is the 'Code' part of the $currencies_query below. What i'm looking to do is create a variable which holds the 'symbol_left' part of the query?

I'm unsure of the correct syntax?

Is there someone who can offer some asistance? It's not something i can directly query from the database as the 'symbol_left' depends on the currency the user has selected.

I rather suspect it's within one of the class statements below. I'm just unsure how to assign it to a global variable. Perhaps something like this?

$symbol_left => $currencies['symbol_left']

Many thanks for your help.

Marcus

////
// Class to handle currencies
// TABLES: currencies
class currencies {
var $currencies;

// class constructor
function currencies() {
$this->currencies = array();
$currencies_query = tep_db_query("select code, title, symbol_left, symbol_right, decimal_point, thousands_point, decimal_places, value from " . TABLE_CURRENCIES);
while ($currencies = tep_db_fetch_array($currencies_query)) {
$this->currencies[$currencies['code']] = array('title' => $currencies['title'],
'symbol_left' => $currencies['symbol_left'],
'symbol_right' => $currencies['symbol_right'],
'decimal_point' => $currencies['decimal_point'],
'thousands_point' => $currencies['thousands_point'],
'decimal_places' => $currencies['decimal_places'],
'value' => $currencies['value']);
}
}

// class methods
function format($number, $calculate_currency_value = true, $currency_type = '', $currency_value = '') {
global $currency;

if (empty($currency_type)) $currency_type = $currency;

if ($calculate_currency_value == true) {
$rate = (tep_not_null($currency_value))? $currency_value : $this->currencies[$currency_type]['value'];
$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($number * $rate, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
// if the selected currency is in the european euro-conversion and the default currency is euro,
// the currency will displayed in the national currency and euro currency
if ( (DEFAULT_CURRENCY == 'EUR') && ($currency_type == 'DEM' ¦¦ $currency_type == 'BEF' ¦¦ $currency_type == 'LUF' ¦¦ $currency_type == 'ESP' ¦¦ $currency_type == 'FRF' ¦¦ $currency_type == 'IEP' ¦¦ $currency_type == 'ITL' ¦¦ $currency_type == 'NLG' ¦¦ $currency_type == 'ATS' ¦¦ $currency_type == 'PTE' ¦¦ $currency_type == 'FIM' ¦¦ $currency_type == 'GRD') ) {
$format_string .= ' <small>[' . $this->format($number, true, 'EUR') . ']</small>';
}
} else {
$format_string = $this->currencies[$currency_type]['symbol_left'] . number_format(tep_round($number, $this->currencies[$currency_type]['decimal_places']), $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];
}

return $format_string;
}

function is_set($code) {
if (isset($this->currencies[$code]) && tep_not_null($this->currencies[$code])) {
return true;
} else {
return false;
}
}

function get_value($code) {
return $this->currencies[$code]['value'];
}

function get_decimal_places($code) {
return $this->currencies[$code]['decimal_places'];
}

function display_price($products_price, $products_tax, $quantity = 1) {
return $this->format(tep_add_tax($products_price, $products_tax) * $quantity);
}
}
?>

Netizen

10:09 am on Aug 16, 2004 (gmt 0)

10+ Year Member



If you do something like

$currencies=new currencies();

then you can access the 'symbol_left' part by doing

$symbol_left=$currencies->currencies['GBP']['symbol_left'];

if you just want it for GBP that is.

Hope that helps.

marcus76

11:43 am on Aug 16, 2004 (gmt 0)

10+ Year Member



hi Netizen

Ok cool, - thanks for your post.

I noticed if i echoed out $currencies on it's own it returns object, so from this your interrogating the object for one of the object's value right?

The global $currency variable holds the 'GBR' - so i guess i could do the following too right?

$symbol_left=$currencies->currencies[$currency']['symbol_left'];

thanks again for your help

Marcus

Warboss Alex

11:48 am on Aug 16, 2004 (gmt 0)

10+ Year Member



That'd work as far as I can see, since the statement'd be in your script, and the variable in its scope. :)

Netizen

3:56 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



And I second that. You could also add another method to the class:

class currencies {

<code here>

function get_symbol_left($code) {
return $this->currencies[$code]['symbol_left'];
}

}

$symbol_left=$currencies->get_symbol_left($currency);