views:

154

answers:

3

in my application user starts a new tree or get added under a child-user and keep on adding users in branches in such a way-
>there are 10 level of tree type structure.
>root node contain 1 user and each node(user) can have max 5 child-user in this way tree will be like level 0 = 1 user ,
level 1 = 5 user,
level 2 = 25 user ,
level 3 = 125 user and so on.

I created one MySQL table having columns like-

User_id , level, super_id, child1_id, child2_id, child3_id, child4_id, child5_id

my question is How can I get all child-user(child to child also) of a particular user at any level do I need to add some more columns in my table??

A: 

You should have a look at Nested Sets.

In PHP, you could use Doctrine Nested Sets, that will make your life much easier.

Pierre Spring
-1 for a solution that requires not only a very specific and complex framework but also is an OO only approach, and provides no explanation of how it works. While this toolkit may have factory support for rebuiding trees rather than a straight ORM (which is really inefficient) - you don't say which is the case.
symcbean
+1  A: 

You might find it interesting to read my presentation from last week's PHP TEK-X conference:

Models for Hierarchical Data with SQL and PHP

This talk describes alternative solutions in SQL, including:

  • Adjacency List
  • Path Enumeration
  • Nested Sets
  • Closure Table

Also see my answer to this Stack Overflow question: What is the most efficient/elegant way to parse a flat table into a tree?

Bill Karwin
A: 

why don't you create a table that contains:

table1:
id, level, users // this way you can manipulate the max users from this level

table2:
id, level, super_id // and you can set create as many as you like (tree based table)
// eg:

id1 | level0 | id0
id2 | level1 | id0
id3 | level2 | id1
id4 | level2 | id1

i hope you understand what i meant by this ..

Mihai Iorga