Jump to content

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.