Hello Everyone! I am having the most difficult time figuring out a nested loop i would like to perform.

The Senario: I have a simple flash image gallery that accepts an XML scheme:
The page: http://www.jbobfunky.com/multipage_site/gallery.php

An example of the XML scheme i am trying to render out with PHP from a SQL database looks like this:

<?xml version="1.0"?> 
	<node label="[img_folder]">
		<node label="[img_label]" src="../images/[img_filename]" />
                <node label="[img_label]" src="../images/[img_filename]" />
        <node label="[img_folder]">
		<node label="[img_label]" src="/images/[img_filename]" />

In my database table I have these fields img_id, img_folder, img_filename and img_label.

the php:

mysql_select_db($database_calendarDB, $calendarDB);
$query_Recordset1 = "SELECT DISTINCT img_folder FROM galleryDB";
$Recordset1 = mysql_query($query_Recordset1, $calendarDB) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_db_connect, $db_connect);
$query_Recordset2 = "SELECT * FROM galleryDB";
$Recordset2 = mysql_query($query_Recordset2, $db_connect) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

$path = '../images/gallery_images/';


echo "<?xml version=\"1.0\"?>\n";
	echo "<tree>\n";
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) 
		echo "<node label=\"" .  $row_Recordset1["img_folder"] . "\"" . ">\n";
		if ($row_Recordset2["img_folder"] = $row_Recordset1["img_folder"])
			while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)) 
				echo  "<node label=\"" . $row_Recordset2["img_label"] . "\" " . "src=\"" . $path . $row_Recordset2["img_filename"] . "\"" . $row_Recordset2["img_folder"] . "/>\n";			
	echo "</node>\n";
	echo "</tree>\n";
	echo "$totalRows_Recordset1";
	echo "<br />";
	echo "$totalRows_Recordset2";



I have messed with the code in so many ways but none that give me the correct result, because i am note sure how to go about a nested loop issue.What needs to happen is: first all the folders need to be printed then 2nd loop needs to place the img labels and file names under the correct folder. My second query needs to check some how against my first query and appropriate the data respectively. I hope i am explaining myself well enough. I am a young developer who needs some guidance on this one. I feel like with the right 2nd query could fix the problem but what that is I have no idea! You can check the page and view the source here:
Another really weird thing i can't seem to figure out is that although the page prints the correct total amount of records and rows, it doesn't print the first row when you view the source. Crazy! I appreciate greatly any help offered!

Thoroughly Frustrated and Exhausted with Head Spinning,

Here is how I would do it. It minimizes the number of queries and should work (at least in my head it does). I don't have time to test it right now.


$con = mysql_connect('localhost','user','password');

$result = array();
$path = '../images/gallery_images/';

$query = mysql_query( "SELECT * FROM `galleryDB`",$con );
if ( mysql_num_rows( $query ) > 0 ) {
	while( $row = mysql_fetch_assoc( $query ) ) {
		$result[$row['img_folder']][] = $row;
	$xml = "<?xml version=\"1.0\"?>\n<tree>\n";
	foreach( $result as $folder => $data ) {
		$xml .= "\t<node label=\"{$folder}\">\n";
		foreach( $data as $row ) {
			$xml .= "\t\t<node label=\"{$row['img_label']}\" src=\"{$path}{$row['img_filename']}\" />\n";
		$xml .= "\t</node>\n";
	$xml .= "</tree>";
	echo $xml;


Wow This place is great. Thank you so much. Leave the testing to me! I appreciate it so much! I'll check it out and let you how it goes. If there is anything I can do for you let me know! We'll chat soon!


I tried the code! It looks perfect but the browser kicks this message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/j/b/o/jbobfunky/html/scripts/imageDataXMLParser2.php on line 11.

Trying to figure it out.....

WHOA! It was my mistake. I put the wrong database in! I think it works! I need to add some more records to confirm!


This article has been dead for over six months. Start a new discussion instead.