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

标签 php mysql database

用户表

enter image description here

在注册的时候每个用户都要放parent_id,谁注册在parent_id下面,所以我做了不同的表

赞助商表

enter image description here

然后像那样做树

enter image description here

我想像这样统计记录

enter image description here

所以请指导我如何计算这样的记录,或者我想说的是,对于这种计数,我必须更改数据库,在此先感谢

最佳答案

我在表中有邻接模型结构,所以我得到了非常好的解决方案 count user_id under parent_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
} 

调用函数

display_children(999, 0)

关于php - 统计php mysql中的多级营销(树)记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10477533/

相关文章:

javascript - 我如何获取并标记谷歌地图中的位置

php - 从 xml 中删除子节点

database - 如何使用 Pentaho 同步两个数据库?

php - 使用 hasColumns laravel 更改数据库表

php - 当一个或多个未选中时,如何在数据库中插入多个复选框值?

database - ASP.NET MVC5 - 将用户保留在 Oracle 数据库中

java - 类和接口(interface)

php - 使用 Zend_Pdf 创建表格的最简单方法

mysql - 1114(HY000): The table is full

java - Hibernate saveOrUpdate 不会更新现有记录,而是将记录另存为新条目