This is my database
+-------------+----------------------+-----+-----+ | category_id | name | lft | rgt | +-------------+----------------------+-----+-----+ | 1 | ELECTRONICS | 1 | 20 | | 2 | TELEVISIONS | 2 | 9 | | 3 | TUBE | 3 | 4 | | 4 | LCD | 5 | 6 | | 5 | PLASMA | 7 | 8 | | 6 | PORTABLE ELECTRONICS | 10 | 19 | | 7 | MP3 PLAYERS | 11 | 14 | | 8 | FLASH | 12 | 13 | | 9 | CD PLAYERS | 15 | 16 | | 10 | 2 WAY RADIOS | 17 | 18 | +-------------+----------------------+-----+-----+
The result I would like to achieve should look like this
ELECTRONICS TELEVISIONS TUBE TUBE LCD LCD PLASMA PLASMA TELEVISIONS PORTABLE ELECTRONICS MP3 PLAYERS FLASH FLASH MP3 PLAYERS CD PLAYERS CD PLAYERS 2 WAY RADIOS 2 WAY RADIOS PORTABLE ELECTRONICS ELECTRONICS
(the indent is only for readability in this post)
This is the code I have so far
$sql_result = mysql_query("SELECT node.lft, node.rgt, 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;");
while($row = mysql_fetch_array($sql_result))
{
$tag[] = $row['name'];
$depth[] = $row['depth'];
}
echo($tag[0]."</br>");
$child_count=0;
for ($i=1; $i<=count($tag)-1; $i++)
{
echo($tag[$i] . "</br>");
if($depth[$i+1]<=$depth[$i])
{
$child_count+=1;
echo($tag[$i]. "</br>");
}
else
{
$child_count=0;
}
if($depth[$i+1]<$depth[$i])
{
echo($tag[$i-$child_count]. "</br>");
// ^This is where things start to go wrong I think..
}
}
echo($tag[0]."</br>");
With this approach, I believe it is how I handle child_count that is flawed, and that it is because I don't account for the child within the child of the parent. (FLASH)
A thousand thanks to anyone who can help me with this!

New Topic/Question
Reply



MultiQuote




|