I have a SQL table tbl_categories with these fields:
id , parent , title
for instance, the table may contain this information:
id parent title
1 0 the main item
2 1 first sub item
3 1 second sub item
4 2 first sub sub item
5 3 second sub sub item
for example: 1 is the top category, 2 and 3 are children of 1, 4 is child of 2 and 5 is child of 3.
I want to list this information like a tree structure using PHP, like this:
- 1. the main item
-- 2.first sub item
---4.first sub sub item
-- 3. second sub item
---5.second sub sub item
and consider to add the “-“‘s according to the level of the item in the tree.
So the question is: what is an appropriate algorithm for this task?
I’m assuming you use MySQL: