Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Multi-level marketing (tree) record in php mysql

users table

enter image description here

In the Registration time every user have to put parent_id , who registration under parent_id, so i made different table for it

sponser table

enter image description here

after that make tree like that

enter image description here

and i want to count record like that

enter image description here

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

like image 910
thecodedeveloper.com Avatar asked May 07 '12 06:05

thecodedeveloper.com


1 Answers

i have Adjacency model structure in table, so i got it very nice solution count user_id under parent_id

that 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)
like image 110
thecodedeveloper.com Avatar answered Sep 19 '22 00:09

thecodedeveloper.com