Printing complex XML structure from DB

admin

Administrator
Staff member
I'm retrieving database info and turning it into an xml file. The xml file has to be structured exactly like this example below:
Here is what I need:
<xml>

<listitem name="Video 1" url="files/flv" thumb="my_video1.jpg">
<stream name="my_video1.flv" start="0" len="-1" />
</listitem>

<listitem name="Video 2" url="files/flv" thumb="my_video2.jpg">
<stream name="my_video2.flv" start="0" len="-1" />
</listitem>


<menu>
<listitem name="Video 1" />
<listitem name="Video 2" />
</menu>

</xml>
Here is what I have:
//Open Database and select All entries
$query = " SELECT * FROM `gallery` ORDER BY `order` ";
$result = mysql_query($query) or die("Could not complete database query");


//Design the XML structure
$xml ="<xml>\r";

if (mysql_num_rows($result) > 0) {
while ( $row = mysql_fetch_array($result) ) {
$xml .= "<listitem name=\"".$row["label"]."\" url=\"files/flv\" thumb=\"".$row["thumbnail"]."\">\r";
$xml .= " <stream name=\"".$row["data"]."\" start=\"0\" len=\"-1\" />\r";
$xml .= "</listitem>\r\r";
}


$xml .= "\r<menu>\r";
while ( $row = mysql_fetch_array($result) ) {
$xml .= "<listitem name=\"".$row["label"]."\" />\r";
}
}


$xml .= "</menu>\r";
$xml .= "</xml>";
The whole first section works perfectly but I cannot get the <menu> section to return the $row["label"]. I only need it to run back through the while loop and return the "label" for each row.
Do I need a "foreach" loop in there?

Any help would be great.
Thanks
 
Back
Top