0

Possible Duplicate:
php / Mysql best tree structure

I'm creating a shop script and I have a little problem. I need to create a php array of tree shop categories, for example:

my mysql table (categories):

'id', 'category_id', 'parentCategory_id'  
1, 11, 0  
2, 12, 0  
3, 13, 12  
4, 14, 12  
5, 15, 14  
6, 16, 0

if parentCategory_id==0 there is no parent. PHP array should look like this: https://i.stack.imgur.com/PLJOL.jpg.

But I'm not convinced to do it with this method, so if you have any suggestion or solution please help me.

EDIT: Maybe I should display categories in xml? how I can do it? I havent any ideas...

Community
  • 1
  • 1
David
  • 746
  • 6
  • 18
  • 3
    Why are you not convinced? Your datamodel perfectly allows unlimited depth of categories. What problem(s) do you encounter? – Konerak Sep 15 '11 at 20:01
  • The only change I'd suggest is using NULL instead of 0, allowing a same-table foreign key on parentCategory_ID to category_id, assuming you don't have any circular references (which would mess up your array, anyway). – Doug Kress Sep 15 '11 at 20:44

1 Answers1

0

As Konerak says this datamodel allows for unlimited levels of categories. What I would suggest is to drop the id column. The category_id column is the natural primary key.

Note that this holds true only if the categories are allowed to have one parent. Otherwise a separate table will be needed.

Martin Dimitrov
  • 4,796
  • 5
  • 46
  • 62