ins0mniac_74 Posted January 31, 2011 Share Posted January 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/ Share on other sites More sharing options...
QuickOldCar Posted January 31, 2011 Share Posted January 31, 2011 I wanted to say I tried this many times and every time it seemed to work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/#findComment-1167578 Share on other sites More sharing options...
jcbones Posted January 31, 2011 Share Posted January 31, 2011 What do you mean by "only works some of the time". What about it doesn't work "only works some of the time"? Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/#findComment-1167589 Share on other sites More sharing options...
ins0mniac_74 Posted January 31, 2011 Author Share Posted January 31, 2011 Try selecting Euro (EUR) or US$ (USD) they return 99.95 as apposed to the value in their currency. EUR should return about $65 but it returns 99.95. But Yen (JPY) does work. Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/#findComment-1167635 Share on other sites More sharing options...
ins0mniac_74 Posted January 31, 2011 Author Share Posted January 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/#findComment-1167721 Share on other sites More sharing options...
BlueSkyIS Posted January 31, 2011 Share Posted January 31, 2011 } else if ($currency == 'JPY' or 'CNY'){ // ALWAYS TRUE should be } else if ($currency == 'JPY' || $currency == 'CNY'){ Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/#findComment-1167890 Share on other sites More sharing options...
ins0mniac_74 Posted February 1, 2011 Author Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/#findComment-1168232 Share on other sites More sharing options...
ins0mniac_74 Posted February 1, 2011 Author Share Posted February 1, 2011 Ahhh, I misread above, the 'or' statements are now working. But the conversions are still only working for the currencies stated above. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/226172-script-only-works-half-of-the-time/#findComment-1168233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.