Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Matching child categories to parent categories

7:47 pm on Sep 11, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1541
votes: 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 Sept 13, 2012 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
votes: 88

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.