ray Posted January 11, 2008 Report Share 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 Link to comment Share on other sites More sharing options...
Delfins Posted January 11, 2008 Report Share Posted January 11, 2008 Un kur `?`? Link to comment Share on other sites More sharing options...
ray Posted January 11, 2008 Author Report Share Posted January 11, 2008 nesapratu! Link to comment Share on other sites More sharing options...
bubu Posted January 11, 2008 Report Share Posted January 11, 2008 Kur ir `jautājums`? Link to comment Share on other sites More sharing options...
mixis Posted January 11, 2008 Report Share Posted January 11, 2008 $currency["USD"]["units"] $currency["USD"]["rate"] Vai tad šis nav vērtības masīvā? Link to comment Share on other sites More sharing options...
ray Posted January 12, 2008 Author Report Share Posted January 12, 2008 (edited) ir, bet tas kods nedarbojas :/ Edited January 12, 2008 by ray Link to comment Share on other sites More sharing options...
andrisp Posted January 12, 2008 Report Share 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 Link to comment Share on other sites More sharing options...
gurkjis Posted January 12, 2008 Report Share 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"] ?> Link to comment Share on other sites More sharing options...
ray Posted January 12, 2008 Author Report Share 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 Link to comment Share on other sites More sharing options...
andrisp Posted January 12, 2008 Report Share Posted January 12, 2008 Kā izskatās bez noapaļošanas ? Link to comment Share on other sites More sharing options...
gurkjis Posted January 12, 2008 Report Share 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 Link to comment Share on other sites More sharing options...
ray Posted January 12, 2008 Author Report Share 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 Link to comment Share on other sites More sharing options...
andrisp Posted January 12, 2008 Report Share Posted January 12, 2008 $x = 'bla'. (string) $bla . 'asd'; Link to comment Share on other sites More sharing options...
ray Posted January 12, 2008 Author Report Share Posted January 12, 2008 paldies, viss kārtībā Link to comment Share on other sites More sharing options...
Recommended Posts