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"?>
<tree>
<node label="[img_folder]">
<node label="[img_label]" src="../images/[img_filename]" />
<node label="[img_label]" src="../images/[img_filename]" />
</node>
<node label="[img_folder]">
<node label="[img_label]" src="/images/[img_filename]" />
</node>
</tree>
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/';
?>
<?php
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";
mysql_free_result($Recordset1);
mysql_free_result($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:
http://www.jbobfunky.com/scripts/imageDataXMLParser.php
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,
Johnny