I have a database with the following fields:
cat_id TINYINT(3)
parent_id TINYINT(3)
cat_name VARCHAR(64)
The idea is that it will store a category tree for the articles section in my site. Anything whose parent_id is 0, will be assumed to be a root category (ie. top level).
This means I will need to process recursively, with some sort of reference to parent_id passing through layers of recursion.
However, I want to save on database queries and so do a "SELECT *", then use mysql_fetch_row on the results recursively, to build the tree. Does anyone have any idea how this can be done?
ie. the idea is to only have to do one db query.
So:
cat_id parent_id name
1 0 A
2 0 B
3 0 C
4 2 D
5 0 E
6 1 F
7 2 G
8 1 H
9 8 I
gives
A
|-F
+-H
+-I
B
|-D
+-G
C
E
Thanks for any help
cat_id TINYINT(3)
parent_id TINYINT(3)
cat_name VARCHAR(64)
The idea is that it will store a category tree for the articles section in my site. Anything whose parent_id is 0, will be assumed to be a root category (ie. top level).
This means I will need to process recursively, with some sort of reference to parent_id passing through layers of recursion.
However, I want to save on database queries and so do a "SELECT *", then use mysql_fetch_row on the results recursively, to build the tree. Does anyone have any idea how this can be done?
ie. the idea is to only have to do one db query.
So:
cat_id parent_id name
1 0 A
2 0 B
3 0 C
4 2 D
5 0 E
6 1 F
7 2 G
8 1 H
9 8 I
gives
A
|-F
+-H
+-I
B
|-D
+-G
C
E
Thanks for any help