get index of foreach xml

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chiqui3d
Forum Newbie
Posts: 1
Joined: Tue Sep 14, 2010 7:46 am

get index of foreach xml

Post 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>
PradeepKr
Forum Newbie
Posts: 14
Joined: Wed Aug 11, 2010 8:29 am

Re: get index of foreach xml

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