homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Matching child categories to parent categories
whatson




msg:4493939
 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:
cars
audi
bmw
toyota
bikes
honda
trucks
ford

etc.
I just cant work out the code to match them.

 

incrediBILL




msg:4494596
 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved