get index of foreach xml

Discussion in 'PHP' started by chiqui3d, Sep 14, 2010.

  1. #1
    hi very good
    I'm trying to get the indices of the gallery that I have a xml file to display it in a <select>
    xml file is as follows ..
    I'm trying to use "foreach" but what brings me, is twice galleria, but that's fine, what happens is that it brings are the names, but I want indices, 0 and 1
    xml file
    <?xml version="1.0"?>
    <content>
     <gallery Name="Hostal" Folder="Gallery/hostal">
     <image Thumb="thumb0.jpg" Large="0.jpg" Caption="Hostal Regina" Colour="181f0a">
    	<comentarios><![CDATA[<p>Hostal Regina comentarios</p>]]></comentarios>
        </image>
        <image Thumb="thumb1.jpg" Large="1.jpg" Caption="Hostal Regina" Colour="181f0a">
    	<comentarios><![CDATA[<p>Hostal Regina</p>]]></comentarios>
        </image>
      </gallery>
    <gallery Name="Vistas" Folder="Gallery/nerja">
        <image Thumb="thumb0.jpg" Large="0.jpg" Caption="Vista Nocturna" Colour="eeeeeee">
    	<comentarios><![CDATA[<p>Vista Nocturna realizada por Diego Palomo</p>]]></comentarios>
        </image>
    <image Caption="eeeeeee"><comentarios>http://php-design-patterns.com</comentarios></image>
    </gallery>
    </content>
    
    PHP:
    file select form
    <select name="action">
    <option value="">Escoger de la Lista</option>
    <?php
    $source = 'content.xml';
    // load as string
    $xmlstr = file_get_contents($source);
    $sitemap = new SimpleXMLElement($xmlstr);
    // load as file
    $sitemap = new SimpleXMLElement($source,null,true);
    foreach($sitemap->gallery as $index=>$content) {
    $atributo = $content->attributes();
    echo "<option value='".$index."'>".$index. "</option>";///in value no show index
    
    
    }
    ?>
    </select>
    PHP:

     
    chiqui3d, Sep 14, 2010 IP
  2. GreenT

    GreenT Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $x=0;
    foreach($sitemap->gallery as $index=>$content) {
    $atributo = $content->attributes();
    echo "<option value='".$x."'>".$index. "</option>";///in value no show index
    $x++;
    }
    
    Code (markup):
    Like this?
     
    GreenT, Sep 15, 2010 IP
  3. chiqui3d

    chiqui3d Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    thank you very much, you've hit the jackpot.
    Perfect
     
    chiqui3d, Sep 15, 2010 IP