Top

Building a Parent/Child Category Structure

Please Register to submit score.
Average Score  8.3
Scores Submitted  3
Date Added  Aug 20, 2004
Last Updated  Mar 19, 2007
Tags  category  datafeed  mysql  parentchild  tree 

Introduction

This generates two structures: a tree and a list of objects. Both are generated from the following standard parent/child database format:

- cat_id: primary key, auto_increment
- cat_title: name of category
- cat_parent_id: points to the cat_id of this items parent.

These are two recursive functions that first build a tree structure. The second recursive function builds a sortable list of objects with their full category path as shown below:
For Cat3: Cat1 > Cat2 > Cat3

It's helpful to use a print_r on both objects to get a visual idea of what you've built.

Important Notes:
- This code will not work without a query and a database.
- The parent category needs to be included in the dataset and $directory_id needs to match it's cat_id.


Grab the Code

Comments

  (5)  RSS
Pauladao
Comments: 1
 
PHP Snippet:  Building a Parent/Child Category Structure
Posted on Mar 22, 2006 12:39 pm
Hi Hawkee! This code is very usefull, but i'm getting an error while running it:

Warning: Invalid argument supplied for foreach() in c:webanunciosadmindump.php on line 35

Dump.php is what I named the script, the line with the error is:
foreach($cat_array as $category)

I tried to do something similar to this script myself, but I was not sucessfull. If you can see this and help figuring this error out, I would appreciate very much.

Best Regards;

(Marcio)
Hawkee
Comments: 398
 
PHP Snippet:  Building a Parent/Child Category Structure
Posted on Mar 22, 2006 4:13 pm
It seems like your $cat_tree wasn't created properly. Make sure you check the data you're pulling from mySQL to be sure it's got a cat_title, cat_id and cat_parent_id for each category in your database.
rompers
Comments: 1
 
PHP Snippet:  Building a Parent/Child Category Structure
Posted on Mar 18, 2007 11:03 pm
hello !

as now sql code should look

sorry for my bad english

thxRompers
Hawkee
Comments: 398
 
PHP Snippet:  Building a Parent/Child Category Structure
Posted on Mar 19, 2007 2:38 am
rompers, what are you asking?
F*U*R*B*Y*
Comments: 408
 
PHP Snippet:  Building a Parent/Child Category Structure
Posted on Mar 20, 2007 2:15 am
can you explain what this does please :) i don't really understand what you mean parent/child category structure

Please Register or Login to start posting comments.
Bottom