category_id - (the primary key, auto-incrementing int)
parent_id - (int, refers to the category_id of its "parent" or owner)
Basically, what I've created is a hierarchical category system, where
any record with a parent_id of 0 is a "top level" or root category,
and all others have a parent_id which refers to its owner. This
allows for an infinite tree of categories and subcategories and so
forth.
My problem is...while I can get everything in the DB correctly and
such, I'm lost on the logic for displaying the hierarchy. I wanna
bascially loop through all the "top level" categories, showing all the
subcategories of the current one, and sub-categories of those, etc.
etc.
Does anyone have some expertise on how I should be approaching this?
I'm way lost on the logic at this point, and was hoping to get some
basic pseudo code or how I should approach this logic.
Any help is sincerely appreciated. Thanks!
-Mike
You may find this useful:
http://pear.php.net/package-info.php?pacid=104
Incidentally, your method of storing the categories is what the author of
the Tree package calls the "Brain dead" approach, and I'm inclined to
agree. With this approach the best way of building the tree is probably to
extract all the nodes and build the tree in application code.
By storing the data more intelligently you might find things easier, you may
find this of interest:
http://searchdatabase.techtarget.com/tip/1,289483,sid13_gci537290,00.html
http://searchdatabase.techtarget.com/tip/1,289483,sid13_gci801943,00.html
Other people use the so called "Materialized path" approach, this is
describe here (together with nested set):
http://www.dbazine.com/tropashko4.html
Finally there's the transitive closure approach, you simply store all
ancestor-descendant relationships.
André Næss