Hi all,
I'm attempting to generate XML output via a query. I'm wanting to generate a parentID style tree structure.
I've successfully created a recursive function to output the data however am having difficulty generating the XML side of things. This is what i have so far:
<?
function maketree($rootcatid,$level)
{
mysql_connect("localhost","root","");
mysql_select_db("theodore_alexander");
$sql="select cat_id,cat_name
from categories
where cat_parent=$rootcatid
order by cat_name";
//echo "$sql<br>\n";
$result=mysql_query($sql);
while (list($DBcatid,$DBcatname)=mysql_fetch_row($result))
{
$width=($level+1)*24;
echo $DBcatid." ".$rootcatid;
$display="<img src=e.gif width=$width height=12>";
$display.="$DBcatname";
$display.="<br>\n";
echo $display;
maketree($DBcatid,$level+1);
}
}
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<node label='categories'>\n";
echo $xml_output;
maketree(0,0);
echo "</node>";
?>
I need the output to resemble the following:
<?xml version="1.0" encoding="iso-8859-1"?>
<node label="categories">
<node label="Cat 1"/>
<node label="Cat 2">
<node label="Cat 2.1">
<node label="Cat 2.4.1"/>
</node>
</node>
<node label="Cat 3"/>
<node label="Cat 4"/>
</node>
The DB looks like:
cat_id cat_parent cat_name
1 0 Cat 1
2 0 Cat 2
3 0 Cat 3
4 2 Cat 2.1
5 0 Cat 4
6 4 Cat 2.4.1
Cheers