Count multilevel marketing (tree) entry in php mysql

user table

enter image description here

At the time of registration, each user should put parent_id, who is registered in parent_id, so I made another table for him

sponsor table

enter image description here

after that make a tree like this

enter image description here

and I want to read such a record

enter image description here

so plz guide me how can i read a record like this, or is there any way i want to say for this kind of counting i have to change in the database, thanks in advance

+5
source share
3 answers

I have an Adjacency model structure in a table, so I got a very good amount of count user_id under parent_id

that the function count user_id under parent_id

function display_children($parent, $level) {
    $count = 0;
    $result = mysql_query('SELECT user_id FROM sponsers '.'WHERE parent_id="'.$parent.'"');
    while ($row = mysql_fetch_array($result))
    {
           $var = str_repeat(' ',$level).$row['user_id']."\n";

                   //echo $var  after remove comment check tree

                   // i call function in while loop until count all user_id 

           $count += 1 +$this->display_children($row['user_id'], $level+1);

    }
    return $count; // it will return all user_id count under parent_id
} 

call function

display_children(999, 0)
+6
source

to store hierarchical data you can see Nested Set Model. more details http://en.wikipedia.org/wiki/Nested_set_model . hope he does your job perfectly.

0
source

All Articles