Pulling Data from XML

Hello, Im a beginner to intermediate developer who just got the Sitepoint book and is working on some of my own projects at the same time. I’ve got a small issue and wondering if anyone can help me.

I’'ve got an xml file with galleries objects for different days - Thus, Fri etc …

I’m trying to create a PHP script which pulls ONLY the gallerylinks from when Thursday

Here is what I’ve been trying, BUT It still seems to pull in ALL the images from ALL the days …

PHP script

  				<?
					  $doc = new DOMDocument();
					  $doc->load( '../xml/galleries.xml' );
					  $galleries = $doc->getElementsByTagName( "gallery" );
					  $gallerydays = $doc->getElementsByTagName( "day" );
								  
			     
					  foreach( $gallerydays as $galleryday )
					  {		  
					  	  $days = $galleryday->getElementsByTagName( "day" );
						  $day = $days->item(0)->nodeValue;
					  
					          If ($day = 'Thursday') {
							  foreach( $galleries as $gallery )
							  {
								  $titles = $gallery->getElementsByTagName( "title" );
								  $title = $titles->item(0)->nodeValue;
								  
								  $images = $gallery->getElementsByTagName( "galleryImage" );
								  $image = $images->item(0)->nodeValue;
							  
								  echo "<tr>";
								  echo "<td valign='top'><img src='$galleryImage' alt='$galleryImage' width='100' /></td>";
								  echo "<td><p><strong>$title</strong><br />$image</p><p>$description</p></td>";
								  echo "</tr>";
									  }
							}
							
					  }

				?>

and the xml source file is …

<?xml version="1.0" encoding="UTF-8"?>
<galleries>
<!--THURSDAY GALLERIES -->
	<gallery>
		<day>Thursday</day>
		<galleryLink>http://sample.php</galleryLink>
		<title>Test Gallery One</title>
		<galleryImage>http://www.hl.com/northamerica/sp/images/latin_Ext001.jpg</galleryImage>
	</gallery>
	<gallery>
		<day>Thursday</day>
		<galleryLink>http://http://sample.php</galleryLink>
		<title>Test Gallery One</title>
		<galleryImage>http://www.hl.com/northamerica/sp/images/latin_Ext002.jpg</galleryImage>
	</gallery>
	
<!--FRIDAY GALLERIES -->
	<gallery>
		<galleryLink>http://sample.php</galleryLink>
		<title>Test Gallery Two</title>
		<galleryImage>http://www.hl.com/northamerica/sp/images/latin_Ext011.jpg</galleryImage>
		<day>Friday</day>
	</gallery>

Thank you in advance for your help.

Thanks … in a slightly different version of the script (I’ve been playing around with it all morning) I’ve gotten ALL the values from the .xml to feed into the html - in other words the entire file, not just those obects where <day>Thursday</day>. Its only gotten difficult when I try to isolate the specific day.

And yes, I think it may have something to do with PHP’s interaction with the DOM and whether I am able to parse an array that comes through using the getElementByID function.

But thank you for your help.

Can’t help you with the DOMDocument stuff, you’ll have to wait for someone that knows more about that.
But you could try to put some echo’s here and there, to see if the script is doing what you expect it to do, and to see (for example) what values $day gets (if the script ever enters the foreach loop).

Wow, that makes me feel dumb … but if I correct that then I get none of the data feeding through … here’s the new code just to be sure …

<?
					  $doc = new DOMDocument();
					  $doc->load( '../xml/galleries.xml' );
					  $galleries = $doc->getElementsByTagName( "gallery" );
					  $gallerydays = $doc->getElementsByTagName( "day" );
								  
					  
					  foreach( $gallerydays as $galleryday )
					  {		  
					  	  $days = $galleryday->getElementsByTagName( "day" );
						  $day = $days->item(0)->nodeValue;
					  
					      If ($day == "Thursday") {
							  foreach( $galleries as $gallery )
							  {
								  $titles = $gallery->getElementsByTagName( "title" );
								  $title = $titles->item(0)->nodeValue;
								  
								  $images = $gallery->getElementsByTagName( "galleryImage" );
								  $image = $images->item(0)->nodeValue;
							  
								  echo "<tr>";
								  echo "<td valign='top'><img src='$galleryImage' alt='$galleryImage' width='100' /></td>";
								  echo "<td><p><strong>$title</strong><br />$image</p><p>$description</p></td>";
								  echo "</tr>";
									  }
						   }
							
					  }

				?>

In human terms, I’m cycling through the days with the first foreach
then choosing the Thursday images with the if statement
then clycling through the image & titles with the second foreach which is conditional on the if …

But I’m getting nothing back out to the page …

If ($day = ‘Thursday’)

this gives $day the value ‘Thursday’. Use ==