|Matching child categories to parent categories|
| 7:47 pm on Sep 11, 2012 (gmt 0)|
In my CMS I want to be able to create pages, and then use these pages as parent categories, that can have child pages, I want this to go three levels, e.g.
cars > toyota > camry
All the pages and categories are treated the same, and are in the same table. There is a column called category, which is the id of the parent page.
My question is, how can I associate the categories with pages to create a drop down box like:
I just cant work out the code to match them.
| 5:13 am on Sep 13, 2012 (gmt 0)|
You only need two fields - each record has an ID # and a parent record #. Any record can be a child of any other record, totally flexible, and you can easily traverse it backwards or forwards.
To build a drop down of all cars you start with the car record and query all it's children that claim "cars" as a parent ID#, so on and so forth down the chain as deep as you want.
To get a drop list of all top level categories, like cars, bikes, trucks, etc. you query for parent ID = 0 since they have no parents. Then query for everything that has the ID for cars, bikes, etc., so on and so forth.
I would cache the dropdown in a file that's included at run-time to save on those queries.