PHP currency converter with XML feed [message #172506] |
Mon, 21 February 2011 13:11 |
astral
Messages: 14 Registered: February 2011
Karma:
|
Junior Member |
|
|
I'm having trouble to modify this code(currency converter, a combination of
javascript and php, which uses an XML feed from http://www.ecb.int/ to get
up to date exchange rates) to work with http://www.bank.lv/vk/xml.xml Not
worked for me. Any help will be appreciated.
<?php
$real_currency_names = array();
$real_currency_names['EUR'] = "Euro";
$real_currency_names['USD'] = "US Dollar";
$real_currency_names['JPY'] = "Japanese Yen";
$real_currency_names['BGN'] = "Bulgarian Leva";
$real_currency_names['CZK'] = "Czech Koruny";
$real_currency_names['DKK'] = "Denmark Kroner";
$real_currency_names['EEK'] = "Estonia Krooni";
$real_currency_names['GBP'] = "British Pound";
$real_currency_names['HUF'] = "Hungary, Forint";
$real_currency_names['LTL'] = "Lithuania, Litai";
$real_currency_names['LVL'] = "Latvia, Lati";
$real_currency_names['PLN'] = "Poland, Zlotych";
$real_currency_names['RON'] = "Romania, New Lei";
$real_currency_names['SEK'] = "Sweden, Kronor";
$real_currency_names['CHF'] = "Switzerland, Francs";
$real_currency_names['NOK'] = "Norway, Krone";
$real_currency_names['HRK'] = "Croatia, Kuna";
$real_currency_names['RUB'] = "Russia, Rubles";
$real_currency_names['TRY'] = "Turkey, New Lira";
$real_currency_names['AUD'] = "Australia, Dollars";
$real_currency_names['BRL'] = "Brazil, Brazil Real";
$real_currency_names['CAD'] = "Canada, Dollars";
$real_currency_names['CNY'] = "China, Yuan Renminbi";
$real_currency_names['HKD'] = "Hong Kong, Dollars";
$real_currency_names['IDR'] = "Indonesia, Rupiahs";
$real_currency_names['INR'] = "India, Rupees";
$real_currency_names['KRW'] = "Korea (South), Won";
$real_currency_names['MXN'] = "Mexico, Pesos";
$real_currency_names['MYR'] = "Malaysia, Ringgits";
$real_currency_names['NZD'] = "New Zealand, Dollars";
$real_currency_names['PHP'] = "Philippines, Pesos";
$real_currency_names['SGD'] = "Singapore, Dollars";
$real_currency_names['THB'] = "Thailand, Baht";
$real_currency_names['ZAR'] = "South Africa, Rand";
$xml =
simplexml_load_file('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
$xmlString = $xml->asXML();
$pos = stripos($xmlString,"<cube currency=");
$xmlString = substr($xmlString,$pos);
$pos2 = stripos($xmlString,"</cube>");
$xmlString = substr($xmlString,0,$pos2);
$arr = explode("<Cube ",$xmlString);
$js_init = 'currencies["EUR"]=parseFloat(1);';
$options = '<option value="EUR">EUR</option>';
foreach ($arr as $value)
{
if (!empty($value))
{
$currency = substr($value,strlen('currency="'),3);
$curr_value = substr($value,intval(strlen('currency="')+ 11));
$curr_value = floatval($curr_value);
$js_init.= "currencies[\"$currency\"]=parseFloat($curr_value);";
$options.= "<option
value=\"$currency\">".$real_currency_names[$currency]."</option>";
} else {
//Do nothing
}
}
?>
<script type="text/javascript" language="javascript">
var currencies = new Array();
<? echo $js_init; ?>
function convert()
{
var amount = parseFloat(document.getElementById('from').value);
var currFrom =
document.getElementById('currencyFrom').options[document.getElementById('cu rrencyFrom').options.selectedIndex].value;
//Get From amount in Euros
var amount_in_euros = parseFloat(amount/currencies[currFrom]);
var currTo =
document.getElementById('currencyTo').options[document.getElementById('curr encyTo').options.selectedIndex].value;
//Convert to new currency
var final_amount = parseFloat(amount_in_euros) *
parseFloat(currencies[currTo]);
document.getElementById('result').value = final_amount;
}
</script>
<table>
<tr>
<td width="50%">Convert <input type="text" id="from" />
<select name="currencyFrom" >
<? echo $options; ?>
</select>
</td>
<td width="50%">
To <select name="currencyTo" >
<? echo $options; ?>
</select>
<input type="text" id="result" />
</td>
</tr>
<tr>
<td colspan="2"><div align="center"><input type="button" value="Convert"
onclick="convert()" /></div></td>
</tr>
</table>
Regards,
astral
|
|
|