简体   繁体   English

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

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

users table 用户表

在此输入图像描述

In the Registration time every user have to put parent_id , who registration under parent_id, so i made different table for it 在注册时间内,每个用户都必须放置parent_id,在parent_id下注册谁,所以我为它做了不同的表

sponser table 赞助表

在此输入图像描述

after that make tree like that 在那之后做那样的树

在此输入图像描述

and i want to count record like that 我想算上那样的记录

在此输入图像描述

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

i have Adjacency model structure in table, so i got it very nice solution count user_id under parent_id 我在表中有Adjacency模型结构,所以我在parent_id下得到了非常好的解决方案count user_id

that function 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
} 

call function 通话功能

display_children(999, 0)

I would guess, that you are searching for this http://www.sitepoint.com/hierarchical-data-database-2/ . 我猜,你正在寻找这个http://www.sitepoint.com/hierarchical-data-database-2/

It is called TREE TRAVERSAL . 它被称为TREE TRAVERSAL Well in your case you can use it just for counting. 那么在你的情况下你可以使用它只是为了计数。
More information on that topic can be found here Tree data structure 有关该主题的更多信息,请参见树数据结构

for hierarchical data storing you can see Nested Set Model . 对于分层数据存储,您可以看到Nested Set Model see in detail http://en.wikipedia.org/wiki/Nested_set_model . 详见http://en.wikipedia.org/wiki/Nested_set_model hope it will perfectly serve your purpose. 希望它能完美地满足你的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM