ray Posted January 11, 2008 Report Posted January 11, 2008 (edited) Ir šāds kods, kur no xml [LV bankas xml faila] vajag masīvā dabūt vērtības. <?php $xmlstr = file_get_contents('xml.xml'); $xml = simplexml_load_string($xmlstr); $currencyList = $xml -> body -> crates -> currencies -> currency; foreach ($currencyList as $currencyItem){ $currenty[$currencyItem -> id]["rate"] = $currencyItem -> rate; $currenty[$currencyItem -> id]["units"] = $currencyItem -> units; } echo $currency["USD"]["units"]." USD = ".$currency["USD"]["rate"]."<br />"; echo $currency["EUR"]["units"]." EUR= ".$currency["EUR"]["rate"] ?> Edited January 11, 2008 by ray
mixis Posted January 11, 2008 Report Posted January 11, 2008 $currency["USD"]["units"] $currency["USD"]["rate"] Vai tad šis nav vērtības masīvā?
ray Posted January 12, 2008 Author Report Posted January 12, 2008 (edited) ir, bet tas kods nedarbojas :/ Edited January 12, 2008 by ray
andrisp Posted January 12, 2008 Report Posted January 12, 2008 (edited) Kas tieši nedarbojas ? Failies neielasas, xml nenpārsējas ? $currencyList nav tas, ko tu domā ? Taču debugot vajag. Edited January 12, 2008 by andrisp
gurkjis Posted January 12, 2008 Report Posted January 12, 2008 kļūdas: 1. xml elementu nosaukumi ir case sensitive 2. $currency masīvs sākumā ir jāinitializē ar $currency = array(); 3. $currencyList XML ceļš (path) nebija pareizs. Kad ielasi ar $xml = simplexml_load_* , tad tas $xml ir root elements, šajā gadījumā $xml elements ir CRates, un body nav vajadzīgs. tagad darbojas: <?php $xmlstr = file_get_contents('xml.xml'); $xml = simplexml_load_string($xmlstr); $currencyList = $xml ->Currencies->Currency; $currency = array(); foreach ($currencyList as $currencyItem){ $currency[(string)$currencyItem->ID]["rate"] = $currencyItem ->Rate; $currency[(string)$currencyItem->ID]["units"] = $currencyItem ->Units; } echo $currency["USD"]["units"]." USD = ".$currency["USD"]["rate"]."<br />"; echo $currency["EUR"]["units"]." EUR= ".$currency["EUR"]["rate"] ?>
ray Posted January 12, 2008 Author Report Posted January 12, 2008 kas nav kārtībā ar noapaļošanu, ka rāda rezultātā nulli? echo $currency["USD"]["units"]." USD = ".round($currency["USD"]["rate"], 4)."<br />"; // 1 USD = 0 echo $currency["EUR"]["units"]." EUR= ".round($currency["EUR"]["rate"], 4); // 1 EUR= 0
gurkjis Posted January 12, 2008 Report Posted January 12, 2008 (edited) round() pirmajam parametram jābūt float tipa, bet tiek dots kā string, tāpēc ir jākonvertē. priekšā pieliec (float), piem round((float)$currency["USD"] [edit] nē, dots tiek kā SimpleXML elements. labāk tur iekš cikla ["rate"] = $currencyItem->Rate pirms $currencyItem pieliec (string), tāpat pie Units rindiņas. Citādi masīvā tiek gāzti elementa objekti, bet tev vajag tikai string. Edited January 12, 2008 by gurkjis
ray Posted January 12, 2008 Author Report Posted January 12, 2008 (edited) bez noapaļošanas: 1 USD = 0.47800000 1 EUR= 0.70280400 nesapratu par to string pielikšanu :/ Edited January 12, 2008 by ray
Recommended Posts