Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PHP currency converter with XML feed

32 views
Skip to first unread message

astral

unread,
Feb 21, 2011, 8:11:40 AM2/21/11
to
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('currencyFrom').options.selectedIndex].value;
//Get From amount in Euros
var amount_in_euros = parseFloat(amount/currencies[currFrom]);
var currTo =
document.getElementById('currencyTo').options[document.getElementById('currencyTo').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

"Álvaro G. Vicario"

unread,
Feb 21, 2011, 8:25:46 AM2/21/11
to
El 21/02/2011 14:11, astral escribió/wrote:
> 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.

Just a personal thought... You drop a couple hundred lines of code and
don't even bother explaining what is failing... It looks like you are
not looking for help or advise but for someone who'll do the job for you
for free.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

astral

unread,
Feb 21, 2011, 8:47:07 AM2/21/11
to

""Álvaro G. Vicario"" <alvaro.NO...@demogracia.com.invalid> wrote in
message news:ijtp4q$kkc$1...@news.eternal-september.org...
-----------------

no, absolutely. This is small enough code. What not work? After I replaced
feed with http://www.bank.lv/vk/xml.xml , and changed other strings as
follows:

$xml = simplexml_load_file('http://www.bank.lv/vk/xml.xml');

$xmlString = $xml->asXML();
$pos = stripos($xmlString,"<Currency=");
$xmlString = substr($xmlString,$pos);
$pos2 = stripos($xmlString,"</CRates>");
$xmlString = substr($xmlString,0,$pos2);
$arr = explode("<CRates ",$xmlString);


saved as html page when tested it - show just form with drop down selectors
with no values inside, and "convert" action not work also.

Thanks.

Luuk

unread,
Feb 21, 2011, 8:49:10 AM2/21/11
to
On 21-02-11 14:11, astral wrote:
> 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.
>
> .....
> Regards,
> astral

That site you mentioned gave help....
http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html#dev

And "help" is not a question... ;)

--
Luuk

astral

unread,
Feb 21, 2011, 9:09:21 AM2/21/11
to

"Luuk" <Lu...@invalid.lan> wrote in message
news:m37a38-...@luuk.invalid.lan...
-----------


Does someone know better currency convertrer php script to work with xml
feed, and without MySQL?

Luuk

unread,
Feb 21, 2011, 9:17:13 AM2/21/11
to
0 new messages