Forum Moderators: coopster
I found this snippet elsewhere on this forum, but I'd like to know if it is possible not to show just ONE currency, but use a similar method to get ALL of the country names and rates into a drop down like so..
<select name="rates">
<option value="1.5">GBR</option>
<option value="2.5">EUR</option>
..and so on..
</select>
I'd really appreciate help on this, I've been trying to get something like this working for almost 2 days!
The XML file is here:
[ecb.int...]
----xpath code below shows just one rate---
<?php
require("XPath.class.php");
$xp = new XPath();
$xp->importFromFile("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
$EUR_GBP_RATE = $xp->getAttributes("//Cube[@currency='GBP']","rate");
echo($EUR_GBP_RATE);
?>
<?php
require("XPath.class.php");
// a local copy of the xml (for now)
$file = "eurofxref-daily.xml";
// load file
$xp = simplexml_load_file($file) or die ("Unable to load XML file!");
//try and find all of the country codes and their rates (my code here is wrong, I am sure of it)
$results = $xp->xpath('//Cube/currency');
// hopefully you can see what I am trying to do here
foreach($results as $result) {
echo $result."<br>";
}
?>
I just have no experience with XML
$xml = simplexml_load_file('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
$options = '';
foreach ($xml->Cube->Cube->Cube as $cube) {
$options .= "<option value=\"{$cube['rate']}\">{$cube['currency']}</option>\n";
}
print "<select name=\"rates\">{$options}</select>"; foreach ($xml->xpath('//Cube[@time]/Cube') as $cube) {
$options .= "<option value=\"{$cube['rate']}\">{$cube['currency']}</option>\n";
} <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
and then using xpath worked just fine.
<form name="fxform" method="post" action="">
1 x
<select name="rates" id="rates" onchange="this.form.elements['rate'].value=this.options[this.selectedIndex].value;">
<option value="1" selected="selected">EUR (default)</option>
<?php
$xml = simplexml_load_file('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
$options = '';
foreach ($xml->Cube->Cube->Cube as $cube) {
$options .= "<option value=\"{$cube['rate']}\">{$cube['currency']}</option>\n";
}
print "{$options}";
?>
</select>
=
€
<input name="rate" type="text" id="rate" value="" size="10">
</form>
BTW, do you think they used the Cube namespace enough in that XML? :P
With your help, the final version doesn't use xpath at all and may I say it is MUCH faster now too! :)
Yes, the multiple use of 'code' is what was tripping me. I didn't know if I had to use all of the descendant name i.e"<Cube time='2008-06-17'>", but now I know :)
If you lived closer, I'd owe you a beer!
EDIT: in fact, my speciality is graphics.. if you need any logos or site style modernisation or anything, just shout.