ins0mniac_74 Posted February 1, 2011 Share Posted February 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 1, 2011 Share Posted February 1, 2011 Have you tried print_r($array) to see what the array structure actually is? Quote Link to comment Share on other sites More sharing options...
ins0mniac_74 Posted February 2, 2011 Author Share Posted February 2, 2011 I have done that, if you could have a look, because I am not sure how what I am seeing is supposed to help me. So far as I can tell, all of the right values are there. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 3, 2011 Share Posted February 3, 2011 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. Quote Link to comment Share on other sites More sharing options...
ins0mniac_74 Posted February 4, 2011 Author Share Posted February 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 6, 2011 Share Posted February 6, 2011 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. Quote Link to comment Share on other sites More sharing options...
ins0mniac_74 Posted February 6, 2011 Author Share Posted February 6, 2011 That returns: Illegal offset type in /home/caffeine/public_html/testing/currency2.php on line 22. Line 22 being $array[$currency] = $value; Are you 100% that php knows it is a string? Quote Link to comment Share on other sites More sharing options...
btherl Posted February 7, 2011 Share Posted February 7, 2011 $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. Quote Link to comment 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.