3

I have a list of categories stored in mysql db. The categories can have child categories at any deep level user want.

Database Table

id  name                    parents
1   Apparel                                                                    
2   Appliances                                                                 
46  Apparel                 1                                                  
47  Child Apparel           1                                                  
48  Other Child Category    46                                                                                          

Now parents column tells me which category is a child of which parent. What best data structure algorithm I can use here without recursion in PHP?

Umair A.
  • 6,690
  • 20
  • 83
  • 130
  • 1
    Just wondering, is this homework? – Jared Farrish Mar 04 '12 at 03:12
  • 1
    possible duplicate of [How can I convert a series of parent-child relationships into a hierarchical tree?](http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tre). Includes recursive and non-recursive solutions – Mike B Mar 04 '12 at 03:13
  • Mike the post is what I was looking for. Thanks. – Umair A. Mar 04 '12 at 03:18

1 Answers1

1

This article explains how to store a tree based model, with the ability to look up children without recursive queries.

If the child nodes can be children of multiple parents, you should check out this model

Wesley
  • 693
  • 4
  • 9