billy_111 Posted August 13, 2010 Share Posted August 13, 2010 Hi, I am trying to work with this xml page: http://maps.google.com/maps/api/directions/xml?origin=m20%202nu&destination=m5%205hf&sensor=false public function LoadXml(){ $xml = simplexml_load_file("http://maps.google.com/maps/api/directions/xml?origin=m20%202nu&destination=m5%205hf&sensor=false"); foreach ($xml->xpath('//distance') as $distance): echo $distance->value->getName() . ": " . $distance->value . "<br />"; endforeach; echo "<BR>"; foreach($xml->xpath('//leg') as $address): echo "The start address is: ".$address-> start_address.$address. "<br />"; echo "The end address is: ".$address->end_address.$address; endforeach; } what this code does is echo out the following: value: 102 value: 268 value: 701 value: 103 value: 4512 value: 275 value: 704 value: 1575 value: 64 value: 65 value: 1782 value: 313 value: 10464 The start address is: Manchester, Greater Manchester M20 2NU, UK The end address is: Salford M5 5HF, UK Now i don't need ALL the values, i only need the total value which is 10464. So is there a way to display the last child, or the final value? So all i want to show is value: 10464.. How can i access the final instance of the value? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/210637-xml-last-child-php/ Share on other sites More sharing options...
bh Posted August 13, 2010 Share Posted August 13, 2010 Hi, foreach ($xml->xpath('//distance[last()]') as $distance): ? Quote Link to comment https://forums.phpfreaks.com/topic/210637-xml-last-child-php/#findComment-1098836 Share on other sites More sharing options...
billy_111 Posted August 13, 2010 Author Share Posted August 13, 2010 Hey Thanks! I have got this code: class PostcodeSearch{ public function LoadXml($origin, $destination){ $xml = simplexml_load_file("http://maps.google.com/maps/api/directions/xml?origin=".$origin."&destination=".$destination."&sensor=false"); foreach ($xml->xpath('//distance') as $distance): $lastvalue = $distance->value; endforeach; $total = $lastvalue * 0.00062137119; $total = number_format($total, 2); return $total; } } Then in my front end i have this: <? $review = Review::searchAllReviewsByTitlePoscodeAndAddress(); while($row = mysql_fetch_array($review)){ $pcode = PostcodeSearch::LoadXml($_GET['search'], $row['postcode']); ?> <div style="margin:0 0 10px 0; font-weight: bold">Distance: <span style="color:#990000"><?=$pcode?> miles</span></div> <? } ?> Now i am trying to get this working on this page: http://www.babiesinthecity.co.uk/search/?searchbutton=&search=M25 Notice i have already put in a search of "M25".. So basically for each review i loop through and check its postcode against the given input which in this case is "M25". The first result is "Heaton Park" which has a postcode of "M25 2SW".. If we put this into the xml link like so: http://maps.google.com/maps/api/directions/xml?origin=M25&destination=M25%202SW&sensor=false It gives no results.. Hence why i am getting the error on the display page: Notice: Undefined variable: lastvalue in /home/migbabie/public_html/classes/PostcodeSearch.class.php on line 1 Yet if we look in Google: http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=M25+to+M25+2SW&sll=53.800651,-4.064941&sspn=11.488016,39.506836&ie=UTF8&t=h&z=15 It works fine. Any ideas why i am having this problem. This is all i need to do, just show the distance.. Don't understand why it won't work? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/210637-xml-last-child-php/#findComment-1098859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.