freaky
freaky

Reputation: 1010

xml echo child node loop

I have an xml file with child node with the same name like this:

    <element id="13" size="normal" category="blog" url="Formlabs-3D-printer">
        <date>April 12, 2013</date>
        <tag tag="printer printing 3d formlab kickstarter"></tag>
        <urlpage url="single.php?blog_no=12"></urlpage>
        <urlimage src='./Post thumbnail images/aisa_big.png'></urlimage>
        <sliderimages> 
            <image src='./Post thumbnail images/aisa_big.png'></image>
            <image src='./Post thumbnail images/01.png'></image>
            <image src='./Post thumbnail images/tape-dribbble.jpg'></image>
            <image src='./Post thumbnail images/pissed_pickle.png'></image>
        </sliderimages>
    </element>

To read my xml file I do this actually :

<?php
    if (isset($_GET["page"])) { 

    $i = $_GET["page"];

    $xml = simplexml_load_file('data.xml');

    $element = $xml->xpath(" //element[@url='$i'] ")[0];
    } 
?> 

With this code I echo some node of desired <element>. It works great!

I want to echo each image node inside <sliderimages>. But I have some problem to loop inside it...

Here my code for the loop (EDIT):

<?php
foreach($element->sliderimages as $sliderimages) {
    echo "<div class='Slide'><img src='";
    echo $sliderimages->image['src'];
    echo "'/></div>";
}
?>

With this code it echo only the first child node ....

Sorry for my English, I'm French.

Upvotes: 0

Views: 468

Answers (1)

hr_117
hr_117

Reputation: 9627

You are looping over <sliderimages> and there is only one in your input XML.

Try (not tested):

foreach($element->sliderimages->image  as $image) {
 ...
}

Upvotes: 1

Related Questions