Hello, I have a bunch of categories in my database which uses the adjacency model to reference parent categories e.g.
category_id | parent_id | category_name
1 | 0 | [B]mammal[/B]
2 | 1 | dog
3 | 2 | poodle
4 | 0 | [B]reptile[/B]
5 | 4 | turtle
My SQL for retriveing such information looks like this:
SELECT root.category_name AS root_name,
down1.category_name AS down1_name,
down2.category_name AS down2_name,
root.category_id AS root_id,
down1.category_id AS down1_id,
down2.category_id AS down2_id,
FROM table_categories as root
LEFT OUTER JOIN table_categories AS down1 ON down1.parent_id = root.category_id
LEFT OUTER JOIN table_categories AS down2 ON down2.parent_id = down1.category_id
WHERE root.parent_id = 0
ORDER BY root_name, down1_name, down2_name
My php for displaying the fetched data looks like this
foreach($returnData as $row)
{
echo $row['root_name'];
echo ' > '.$row['down1_name'];
echo ' > '.$row['down2_name'];
echo '<br />';
}
The output of which looks something along the lines of this:
Mammal >
Mammal > Dog
Mammal > Dog > Poodle
Reptile >
Reptile > Turtle
However what I’m after is output that looks more like this:
Mammal
Dog
Poodle
Reptile
Turtle
As you can see the parent is always duplicated the way I have it at the moment. My head is going in cirlces trying to figure out how to combine each category under one unique parent. Is there a way of doing this?