I am trying to get my data which is hierarchically set up with a tree traversal model into an < ul> in order to show on my site.
Here is my code:
Simply loop thru the result will do:
$sql = "SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft";
$query_result = mysql_query($sql)
$result = "";
$currDepth = 0;
while($row = mysql_fetch_assoc($query_result))
{
if($row['depth'] > $currDepth)
{
$result .= ""; // open sub tree if level up
}
if($row['depth'] < $currDepth)
{
$result .= str_repeat("
", $currDepth - $row['depth']); // close sub tree if level down
}
$result .= "- $row['name']
"; // Always add node
$currDepth = $row['depth'];
}
$result .= "
";
echo $result;