计算php mysql中的多级营销(树)记录

the*_*com 5 php mysql database

用户表

在此输入图像描述

在注册时间内,每个用户都必须放置parent_id,在parent_id下注册谁,所以我为它做了不同的表

赞助表

在此输入图像描述

在那之后做那样的树

在此输入图像描述

我想算上那样的记录

在此输入图像描述

所以PLZ指导我怎么能算这样的记录,或者有任何方式我想说这种计数我必须在数据库中更改,谢谢提前

the*_*com 8

我在表中有Adjacency模型结构,所以我在parent_id下获得了非常好的解决方案count user_id

该函数在parent_id下计算user_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
} 
Run Code Online (Sandbox Code Playgroud)

通话功能

display_children(999, 0)
Run Code Online (Sandbox Code Playgroud)