dflow Posted June 15, 2011 Share Posted June 15, 2011 i have generated a xml file with this schema i want to loop thourgh each details with a foreach loop but i get the same ID looped although the xml is fine im using simplexml to load a remote file <? foreach ($xml->details as $apartment) {echo'apartment'$xml->details->id;} ?> //xml <apartments> <details> <ID>32116</ID> <Description></Description> <Floor>3</Floor> <Elevator>0</Elevator> <bedrooms>0</bedrooms> <bathrooms>1</bathrooms> <city_id>3</city_id> <city_name>Berlin</city_name> </details> <details> <ID>32117</ID> <Description></Description> <Floor>5</Floor> <Elevator>0</Elevator> <bedrooms>1<details> <ID>32116</ID> <Description></Description> <Floor>3</Floor> <Elevator>0</Elevator> <bedrooms>0</bedrooms> <bathrooms>1</bathrooms> <city_id>3</city_id> <city_name>Berlin</city_name> </apartments> </details></bedrooms> <bathrooms>1</bathrooms> <city_id>3</city_id> <city_name>Berlin</city_name> </details> Quote Link to comment https://forums.phpfreaks.com/topic/239439-loop-through-xml-results/ Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 Well, I dunno what your $xml object is... provide more code. And take a look at http://www.php.net/manual/en/function.xml-parse.php Quote Link to comment https://forums.phpfreaks.com/topic/239439-loop-through-xml-results/#findComment-1230054 Share on other sites More sharing options...
dflow Posted June 15, 2011 Author Share Posted June 15, 2011 Well, I dunno what your $xml object is... provide more code. And take a look at http://www.php.net/manual/en/function.xml-parse.php $xml = simplexml_load_file("xmlfly2-bycity.php?city_id=".$city_id.""); Quote Link to comment https://forums.phpfreaks.com/topic/239439-loop-through-xml-results/#findComment-1230068 Share on other sites More sharing options...
mikesta707 Posted June 15, 2011 Share Posted June 15, 2011 you get the same ID each time because you don't use the foreach alias for your object that your iterating through. foreach ($xml->details as $apartment) {echo'apartment' .$apartment->id;} But if I were you I would look through: http://php.net/manual/en/function.simplexml-load-file.php the comments have some examples of how to iterate through the object. I dont have much prior experience with this object, so I can't guarentee that the above snippet will fix your problem Quote Link to comment https://forums.phpfreaks.com/topic/239439-loop-through-xml-results/#findComment-1230082 Share on other sites More sharing options...
salathe Posted June 15, 2011 Share Posted June 15, 2011 Further to mikesta707's link, see also http://php.net/simplexml.examples-basic for a guide to the basic usage of SimpleXML. Post back if you have further questions. Quote Link to comment https://forums.phpfreaks.com/topic/239439-loop-through-xml-results/#findComment-1230083 Share on other sites More sharing options...
dflow Posted June 15, 2011 Author Share Posted June 15, 2011 you get the same ID each time because you don't use the foreach alias for your object that your iterating through. foreach ($xml->details as $apartment) {echo'apartment' .$apartment->id;} But if I were you I would look through: http://php.net/manual/en/function.simplexml-load-file.php the comments have some examples of how to iterate through the object. I dont have much prior experience with this object, so I can't guarentee that the above snippet will fix your problem great it works $apartment->ID was case sensitive i also dont understand what you mean by the $xml object, i understand that it is according to the xml schema Quote Link to comment https://forums.phpfreaks.com/topic/239439-loop-through-xml-results/#findComment-1230207 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.