Jump to content

Recommended Posts

I am just helping out a friend with his online store that he would like to add multiple currencies to. I made this script to help him out:

$price = 99.95;
$default_currency = 20;
// displays all the file nodes
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['currency'][1] = 'EUR';
$array['value'][1] = 1.00;
$i = 2;
foreach ($xml->Cube->Cube->children() as $node) {
$arr = $node->attributes();   // returns an array
$array['currency'][$i] = $arr['currency'];
$array['value'][$i] = $arr['rate'];
$i++;
}
}
if (isset($_POST['currency'])){
$currency = $_POST['currency'];
} else {
$currency = $default_currency;
}
$price = $array['value'][$currency] / $array['value'][$default_currency] * $price;
$price = round($price, 2);
$currency_name = $array['currency'][$currency];
echo '<p>Price is '.$currency_name.$price.'<br />
To change currency, select from dropdown box below.</p>';
echo '<form action="currency.php" method="post"><select name="currency">';
$i = 1;
foreach ($array['currency'] as $value){
echo '<option value="'.$i.'">'.$value.'</option>';
$i++;
}
echo '</select><br /><input type="submit" /></form><br />';

$i = 1;
foreach ($array['currency'] as $value){
echo '<p>'.$value.' = '.$array['value'][$i].'</p>';
$i++;	
}

 

But for some reason it only works some of the time and I can't work out why for the life of me.

 

If I could get some help in the matter, that would be greatly appreciated.

 

A link to the above example can be found here.

 

Regards,

 

I.

Link to comment
https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/
Share on other sites

I decided to start again. And for some reason it keeps picking the yen symbol instead of the correct symbol. Also, the same issues are arising with the output not being correct. Still don't have any idea why!

 

Link to here.

 

<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;
}
$price = $array["'".$currency."'"] / $array["'".$default_currency."'"] * $price;
if ($currency == 'JPY' or 'IDR' or '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' or '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>

 

I hope someone has a good idea whats going on this time!

 

Regards,

 

I.

I have tried both 'or' and '||' and neither seem to be working, both keep returning true.

It appears that that is not the route of my problems anyway. The conversion is still not working either for some of the currencies.

The conversion only seems to work on:

JPY, CNY, HKD and IDR.

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.