Creating lists from categories and sub categories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vanderlooj
Forum Newbie
Posts: 3
Joined: Tue Oct 12, 2010 6:26 am

Creating lists from categories and sub categories

Post 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
Post Reply