Page 1 of 1

Creating lists from categories and sub categories

Posted: Wed Mar 30, 2011 6:54 am
by vanderlooj
Hi,

I will try my best to explain what I am trying to do.

I want to create nested html unordered lists from categories and sub categories in my database.

My DB structure looks something like this:

cat_id | cat_name | cat_parent_id
=======================================

1 Category1 NULL
2 Blue 1
3 Light Blue 2
4 Category2 NULL
5 Black 4

I am trying to read through this table and create a tree-structure like list that would look something like this:

Category1
-> Blue
->-> Light Blue

Category2
-> Black

For example, "Blue" has a cat_parent_id of 1, which is Category1's cat_id, which means Blue is a sub-category of Category1 and so on..

I have no problem when there is only one sub-category for each main category. But as soon as the tree starts getting deeper, with sub categories within sub-categories, I am having trouble understanding the logic to loop through them all.

Any help?

Thanks,
Cheers