【问题标题】:how to count members in 15 level deep for each level in php如何在php中为每个级别计算15级深度的成员
【发布时间】:2018-01-08 17:00:02
【问题描述】:

我非常需要计算每个级别的每个成员我有 15 级深度佣金模型,所以我可以计算第一级但无法计算一个人在他/她的下线中拥有的整个团队。我想计算每个级别有多少成员。 比如一级 10 名成员,二级 55 名成员,三级 35 名成员等。 我需要计算第一级、第二级......直到第 15 级的父 ID 的成员总数。 我可以指望第一级像

$result = mysqli_query($con, "SELECT count(*) FROM affiliateuser where referedby = '" . $_SESSION['username'] . "' AND active = 1");

如果您需要任何澄清,请发表评论,我会解释。 我在这里分享我的第一个 2 级代码。请检查下面的代码。

开始拉取第一级下线用户

$totalref = 0;
$totalrefear = 0;
$query = "SELECT fname,email,doj,active,username,level,pcktaken FROM affiliateuser where referedby = '" . $_SESSION['username'] . "'";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)) {
    $ac = "$row[active]";
    $countusername = "$row[username]";
    $countuserlevel = "$row[level]";
    $pcktook = "$row[pcktaken]";
}

开始获取第二级下线用户

$totalrefear = 0;
$query = "SELECT fname,email,doj,active,level,username FROM affiliateuser where referedby = '" . $_SESSION['username'] . "'";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)) {
    $ac = "$row[active]";
    $countusername = "$row[username]";
    $countuserlevel = "$row[level]";
    $query2level = "SELECT fname,email,doj,active,level,username,pcktaken FROM affiliateuser where referedby = '$countusername'";
    $result2level = mysqli_query($con, $query2level);
    while($row2 = mysqli_fetch_array($result2level)) {
        $ac2 = "$row2[active]";
        $countusername2 = "$row2[username]";
        $countuserlevel2 = "$row2[level]";
        $pcktook = "$row2[pcktaken]";
    }
}

我正在尝试使用此查询来计算父级第二级的用户。但它算我整个数据库用户。我只想统计 2 级用户。请有大神帮我解决一下吗?

$queryridd =mysqli_query($con, "SELECT COUNT(Id) AS countt, level AS Lebel from affiliateuser WHERE level = '$countuserlevel' GROUP BY level");

【问题讨论】:

标签: php mysql


【解决方案1】:

假设您有一个 parent_id 字段,您可以计算有多少条记录的 parent_id 等于您正在查看的记录。要更上一层楼,您只需对刚刚获得的每条记录重复一次即可。

现在……你是怎么做到的?

首先,您的数据库需要正确规划。从simple parent - child using recursion, to materialized path, to nested sets 开始,有很多方法可以制作树状结构。每个都有优点和缺点。我个人喜欢物化路径;但是您的应用程序的树的深度使父子的一些变化更有可能。

那么数据库结构是什么样的呢?

Superheroes
----------
id         <--\
parent_id  ---/
name

如果你有这样的层次结构:

                           1 Steven Rogers 
                           /             \
                  2 Bruce Banner       3 Wally West
                  /          \
       4 Peter Parker     5 Jay Garrick
           /        \
6 Barry Allen   7 Reed Richards

你的桌子应该是这样的:

       superheroes
       -----------
id   parent_id   name
1    0           Steven Rogers
2    1           Bruce Banner
3    1           Wally West
4    2           Peter Parker
5    2           Jay Garrick
6    4           Barry Allen
7    4           Reed Richards

要查找史蒂夫·罗杰斯正下方的人,

$sth = $dbh->prepare("select id,name from superheroes where parent_id=?");
$sth->execute(array(1));
$result = $sth->fetchAll();

要查找彼得·帕克正下方的人,

$sth = $dbh->prepare("select id,name from superheroes where parent_id=?");
$sth->execute(array(4));
$result = $sth->fetchAll();

太棒了,升级了一级。我如何获得下一个级别?这是一项功能的工作。 (实际上,一个类会更好,因为范围更容易处理。)

class CountDownLine {

    public function __construct($database) {

        $this->db = $database;
    }

    public function getDownline($id, $depth=2) {

        $stack = array($id);
        for($i=1; $i<=$depth; $i++) {

            // create an array of levels, each holding an array of child ids for that level
            $stack[$i] = $this->getChildren($stack[$i-1]);
        }

        return $stack;

    }

    public function countLevel($level) {

         // expects an array of child ids
         settype($level, 'array');

         return sizeof($level);
    }

    private function getChildren($parent_ids = array()) {

        $result = array();
        $placeholders = str_repeat('?,', count($parent_ids) - 1). '?';

        $this->$db->prepare("select id from superheroes where parent_id in ($placeholders)");
        $this->$db->->execute(array($parent_ids));

        while($row=$this->$db->fetch()) {

            $results[] = $row->id;
        }

        return $results;
    }

}

它是这样工作的:

$DL = new CountDownLine(new Database); // inject your favorite database abstraction

$id = 1; // ie, id of Steve Rogers
$depth = 2;  // get the counts of his downline, only 2 deep.

$downline_array = $DL->getDownline($id, $depth=2);

// => $downline_array[1] = [2,3]
// => $downline-array[2] = [4,5]

$count_of_first_level = $DL->countLevel($downline_array[1]);
$count_of_second_level = $DL->countLevel($downline_array[2]);

注意:这是为了展示概念。我没有在数据库上测试它。您必须调整它以使用您的数据库访问方法。

【讨论】:

    【解决方案2】:

    请使用if检查关卡,然后再进入另一个关卡

    <?php
    
    $query = "SELECT fname,email,doj,active,level,username FROM affiliateuser where referedby = '" . $_SESSION['username'] . "'";
    $result = mysqli_query($con, $query);
    while($row = mysqli_fetch_array($result)) {
        $ac = "$row[active]";
        $countusername = "$row[username]";
        $countuserlevel = "$row[level]";
        // use if to check the level
        if($countuserlevel == 2) { // use if to check the level
    
            $query2level = "SELECT fname,email,doj,active,level,username,pcktaken FROM affiliateuser where referedby = '$countusername'";
            $result2level = mysqli_query($con, $query2level);
            while($row2 = mysqli_fetch_array($result2level)) {
                $ac2 = "$row2[active]";
                $countusername2 = "$row2[username]";
                $countuserlevel2 = "$row2[level]";
                $pcktook = "$row2[pcktaken]";
            }
    
            $queryridd = mysqli_query($con, "SELECT COUNT(Id) AS countt, level AS Lebel from affiliateuser WHERE level = '$countuserlevel' GROUP BY level");
            while($rowbb = mysqli_fetch_array($queryridd)) {
                $countingUsers2 = "$rowbb[0]";
            }
    
        }
    } // use if to check the level
    ?>
    <td class='text-left'><?php echo $countingUsers2; ?></td>
    

    【讨论】:

    • 扭转潮流,反对教授/传播草率和危险的编码实践。如果您发布没有准备好的陈述的答案you may want to consider this before posting。另外一个more valuable answer comes from showing the OP the right method.
    • 好的,但问题没有解决,我想统计每个级别的成员。例如,您登录后,仪表板中有 15 个级别。所以我想统计你们 15 个级别中每个级别的成员。基本上,我正在创建传销软件。我已经分享了前2级的代码。请检查
    • 应该有一个与当前用户下的用户相关的parent_id。所以从那里第二个查询应该像 $query2level = "SELECT fname,email,doj,active,level,username,pcktaken FROM associateuser where referedby = '$countusername' AND parent_id = $_SESSION['id'] ";跨度>
    • 我已经通过 (level = '$countuserlevel') 将第 2 级与第 1 级相关联。因为在第一级查询中分配了 $countuserlevel 变量。请仔细检查代码。请帮我解决这个问题。谢谢
    【解决方案3】:

    您可以轻松获取给定用户每个级别的孩子数。

    <?php
    /*
    * $parent_id = ID of user
    * $deep = amount of depth levels
    */
    
    function countChildrenPerLevelDeep( $parent_id, $deep = 15 ) {
        $parent_id = [$parent_id];
        $level = 1;
        $arr = [];
        $i = 1;
        do {
    
            if ( $parent_id ) {
                $query = 'SELECT * FROM customer WHERE parent_id=' . implode( $parent_id, ',' );
    
                if ( $result = $mysqli->query( $query ) ) {
                    /* total children on this level */
                    $arr['level_' . $level] = $result->fetch_row();
    
                    $parent_id = [];
                    /* fetch associative array */
                    while ( $row = $result->fetch_assoc() ) {
                        $parent_id[] = $row['id'];
                    }
    
                    /* free result set */
                    $result->close();
                } else {
                    $arr['level_' . $level] = 0;
                    $parent_id = [];
                }
            } else {
                $arr['level_' . $level] = 0;
                $parent_id = [];
            }
    
            $level++;
            $i++;
        }
        while ( $i <= $deep );
    
        return $arr;
    }
    ?>
    

    结果示例

    Array
    (
        [level_1] => 1
        [level_2] => 2
        [level_3] => 1
        [level_4] => 0
        [level_5] => 0
        [level_6] => 0
    )
    

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多