Jump to content

[SOLVED] SimpleXML question


dhorn

Recommended Posts

I have a site that is reading from a basic XML file structured similar to the one below:

<db>
<content id='1'>
<banner filename='1st-banner.jpg' /> 
<thumbnail filename='1st-preview.jpg' />
	<text> This is the text to display. </text>

<gallery>
	<image id='1' filename='1st Gallery Image' />
	<image id='2' filename='2nd Gallery Image' />
	<image id='3' filename='3rd Gallery Image' />
</gallery>

</content>
<content id='2'>
<banner filename='2nd-banner.jpg' /> 
<thumbnail filename='2nd-preview.jpg' />
	<text>This is the text that should populate the information for the 2nd floor.</text>

<gallery>
	<image id='1-2' filename='1st Gallery Image' />
	<image id='2' filename='2nd Gallery Image' />
	<image id='3' filename='3rd Gallery Image' />
</gallery>

</content>
</db>

 

I need a way to process the gallery part so that it will successively display each filename (ie. traverse the code in the gallery and display each image filename). The PHP has to display a slightly different section of code for the first image (it's the start of the gallery and has the link to it).

 

I can't figure out the syntax to get it to display each filename value for the images in the gallery, so if anyone has any idea, it would be greatly appreciated.

 

Dan

 

Link to comment
https://forums.phpfreaks.com/topic/165194-solved-simplexml-question/
Share on other sites

this should work:

<?php
$code = <<<XML
<db>
<content id='1'>
   <banner filename='1st-banner.jpg' />
   <thumbnail filename='1st-preview.jpg' />
    <text> This is the text to display. </text>
   
   <gallery>
      <image id='1' filename='1st Gallery Image' />
      <image id='2' filename='2nd Gallery Image' />
      <image id='3' filename='3rd Gallery Image' />
   </gallery>
   
</content>
<content id='2'>
   <banner filename='2nd-banner.jpg' />
   <thumbnail filename='2nd-preview.jpg' />
    <text>This is the text that should populate the information for the 2nd floor.</text>
   
   <gallery>
      <image id='1-2' filename='1st Gallery Image' />
      <image id='2' filename='2nd Gallery Image' />
      <image id='3' filename='3rd Gallery Image' />
   </gallery>
   
</content>
</db>
XML;
$xml = simplexml_load_string($code);
$n=0;
foreach($xml->content as $content){
  foreach($content->gallery as $gallery){
    foreach($gallery->image as $image){
      if(!$n){
        print "This is the first one";
      }
      print $image['filename'];
      $n++;
    }
  }
}
?>

 

edit: this also works:

<?php
$xml = simplexml_load_string($code);
$n=0;
foreach($xml->xpath('//image') as $image){
  if(!$n){
    print "This is the first one";
  }
  print $image['filename'];
  $n++;
}
?>

Archived

This topic is now archived and is closed to further replies.

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