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




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)



for hierarchical data storing you can seeNested Set Model. see in detail hope it will perfectly serve your purpose.




I would guess, that you are searching for this

It is calledTREE TRAVERSAL. Well in your case you can use it just for counting.
More information on that topic can be found here Tree data structure

People are also looking for solutions to the problem: php - what are sessions_ids (tokens) for and how to use for security reasons?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.