Jump to content

Simple Equation... not working =(


ins0mniac_74

Recommended Posts

I am trying to make a currency converter, using the base currency as AU$ from an XML that uses Euro as the base currency. So far I have this:

<body>
<?
//PRICE IS ALWAYS IN DEFAULT CURRENCY
$price = 34.95;
$default_currency = 'AUD';

if(!$xml=simplexml_load_file('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml')){
    trigger_error('Error reading XML file',E_USER_ERROR);
} else {
$array['EUR'] = 1.00;
foreach ($xml->Cube->Cube->children() as $node) {
$result = $node->attributes();   // returns an array
$currency = $result['currency'];
$value = $result['rate'];
$array["'".$currency."'"] = $value;
}
}
if($_POST['currency']){
$currency = $_POST['currency'];
} else {
$currency = $default_currency;
}
print $array["'".$currency."'"].' / '.$array["'".$default_currency."'"].' * '.$price;
$price = $array["'".$currency."'"] / $array["'".$default_currency."'"] * $price;

if ($currency == 'JPY' || $currency == 'IDR' || $currency == 'CNY'){
$price = round($price, 0);
} else {
$price = round($price, 2);
}
//SYMBOL ASSIGNMENT
if ($currency == 'GPB'){
$symbol = '£';
} else if ($currency == 'EUR'){
$symbol = '€';
} else if ($currency == 'JPY' || $currency == 'CNY'){
$symbol = '¥';
} else if ($currency == 'IDR'){
$symbol = 'Rp';
} else if ($currency == 'AUD'){
$symbol = 'A$';
} else if ($currency == 'USD'){
$symbol = 'US$';
} else if ($currency == 'CAD'){
$symbol = 'CA$';
} else if ($currency == 'SPD'){
$symbol = 'S$';
} else if ($currency == 'HKD'){
$symbol = 'HK$';
} else if ($currency == 'NZD'){
$symbol = 'NZ$';
} else {
$symbol = '$';
}
echo '<p>Price: '.$symbol.$price.'</p>';
echo '<p>'.$currency.' '.$symbol.' '.$price.'</p>';
?>
<form action="" method="post">
Select Currency:
<select name="currency">
<option value="AUD">Australian Dollar</option>
<option value="USD">US Dollar</option>
<option value="EUR">Euro</option>
<option value="JPY">Japanese Yen</option>
<option value="GBP">Pound</option>
<option value="CAD">Canadian Dollar</option>
<option value="HKD">Hong Kong Dollar</option>
<option value="CNY">Chinese Renminbi</option>
<option value="IDR">Indonesian Rupiah</option>
<option value="NZD">New Zealand Dollar</option>
<option value="SGD">Singapore Dollar</option>
</select>
<input type="submit" />
</form>
</body>

 

It appears this line is not functioning properly for what ever reason:

$price = $array["'".$currency."'"] / $array["'".$default_currency."'"] * $price;

 

If anyone could give me some clues as to why this is failing, that would be much appreciated.

 

Regards,

 

I.

Link to comment
Share on other sites

Try $array[$currency] instead.  The following are equivalent:

 

$var = 'EUR';
print $array[$var];
print $array['EUR'];

 

In other words, you don't need to put the quotes in when using a variable to index an array.

 

Without placeing

$array["'".$var."'"];

I get a string of errors telling me invalid argument.

Link to comment
Share on other sites

You need to change where you set the array as well.  These lines are inconsistent:

 

	$array['EUR'] = 1.00;   #<--- this one
foreach ($xml->Cube->Cube->children() as $node) {
$result = $node->attributes();   // returns an array
$currency = $result['currency'];
$value = $result['rate'];
$array["'".$currency."'"] = $value; #<--- this one

 

When you set the EUR value, you don't include quotes in the array index.  When you set other currencies, you are including quotes.  You should remove the quotes from all of them.  So the code will be this:

 

	$array['EUR'] = 1.00;  #<-- correct, quotes are needed to tell PHP that EUR is a string
foreach ($xml->Cube->Cube->children() as $node) {
$result = $node->attributes();   // returns an array
$currency = $result['currency'];
$value = $result['rate'];
$array[$currency] = $value; # <-- correct, quotes are NOT needed here.  PHP already knows $currency is a string.

Link to comment
Share on other sites

$currency is either an array or an object, most likely an object.  The solution is not to add quotes around it, but to cast it to a string like this:

 

$array[(string)$currency] = $value;

 

That's assuming you get the correct value out when you cast it.  The reason you didn't get this error before would be that adding quotes to $currency automatically converted it to a string.

 

Then check that you put the right value into $array.  You can use var_dump() or print_r() on it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.