Jump to content

loop through xml results


dflow

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/239439-loop-through-xml-results/
Share on other sites

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.