Page 1 of 1

get index of foreach xml

Posted: Tue Sep 14, 2010 8:10 am
by chiqui3d
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

Code: Select all

<?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>
file select form

Code: Select all

<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>

Re: get index of foreach xml

Posted: Wed Sep 15, 2010 1:40 am
by PradeepKr
Put var_dump($sitemap->gallery); inside the foreach loop and you'll see why PHP is printing that.
If you want to get the number of <gallery ... >tags then you'll have to explicitly make use of an counter