Do while loop not adjusted for output
I've got a do while loop that I'm using to read a SQL query and then write a XML file to my server...
<?php
$fp = fopen('output.xml', 'w');
fwrite($fp, '<?xml version="1.0" encoding="utf-8"?>');
?>
<?php do {
fwrite($fp, '<gallery Name="'.$row_Recordset1['name'].'">');
fwrite($fp, '<image file="'.$row_Recordset1['directory'].'/'.$row_Recordset1['photo'].'">');
fwrite($fp, '</image>');
fwrite($fp, '</gallery>');
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
?>
<?php
fclose($fp);
?>
The above creates a properly formatted XML file that works with my application, however, if I have multiple records in the same <gallery> I need the loop to include all of the <image></image> records before closing the </gallery> and moving on to the next <gallery>
MySQL Query on the Gallery_contents Joining the Gallery listing ON gallery IDs.
mysql_select_db($database_ba, $dbconnect);
$query_Recordset1 = "SELECT * FROM gallery_content INNER JOIN gallery ON gallery.rec_id = gallery_content.gallery_id";
$Recordset1 = mysql_query($query_Recordset1, $dbconnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
The desired XML output example below would have (2) Gallery Groups with (2) photos in each group.
<?xml version="1.0" encoding="utf-8"?>
<gallery Name="Special Events">
<image file="Gallery2/sample1.jpg">
</image>
<image file="Gallery2/sample2.jpg">
</image>
</gallery>
<gallery Name="Just for Fun">
<image file="Gallery1/landscape.png">
</image>
<image file="Gallery1/portrait.png">
</image>
</gallery>
</content>
Do I create two loops here? I'm not sure how to accomplish this.
Thanks in advance.