如何为php中的每个级别计算15个级别的成员数

如何为php中的每个级别计算15个级别的成员数,php,mysql,Php,Mysql,我非常需要计算每个级别上的每个成员我有15个级别的深度佣金模型,因此我能够计算第一个级别,但无法计算一个人在他/她的底线中拥有的整个团队。我想数一数每个级别上有多少成员。 如第一级10名成员、第二级55名成员、第三级35名成员等。 我需要计算第一级、第二级家长id的成员总数。。。高达15级。 我能像你一样依靠第一级 $result = mysqli_query($con, "SELECT count(*) FROM affiliateuser where referedby = '" . $_S

我非常需要计算每个级别上的每个成员我有15个级别的深度佣金模型,因此我能够计算第一个级别,但无法计算一个人在他/她的底线中拥有的整个团队。我想数一数每个级别上有多少成员。 如第一级10名成员、第二级55名成员、第三级35名成员等。 我需要计算第一级、第二级家长id的成员总数。。。高达15级。 我能像你一样依靠第一级

$result = mysqli_query($con, "SELECT count(*) FROM affiliateuser where referedby = '" . $_SESSION['username'] . "' AND active = 1");
如果您需要任何澄清,请评论,我会解释。 我在这里分享我的第一个2级代码。请检查下面的代码

已开始获取第一级下线用户

已开始获取第二级下线用户

我正在尝试使用此查询来统计父级的第二级用户。但我算上了整个数据库的用户。我只想数一数二级用户。有人能帮我解决吗

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

请使用
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>

假设您有一个parent\u id字段,您可以计算有多少条记录的parent\u id等于您正在查看的记录。要进入下一个级别,只需对刚获得的每个记录重复

现在。。。你是怎么做到的

首先,需要正确规划数据库。有许多方法可以从中创建树结构。各有利弊。我个人喜欢物化的道路;但是,应用程序的树的深度使得父子关系更可能发生一些变化

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

Superheroes
----------
id         <--\
parent_id  ---/
name
您的桌子看起来是这样的:

       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();
太好了,可以上一级。我怎样才能进入下一关?这是一项职能工作。(实际上,类会更好,因为范围更容易处理。)

注:这是为了说明概念。我没有在数据库上测试它。您必须对其进行调整,以适应您的数据库访问方法


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

<?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;
}
?>

一些合理的代码缩进将是一个好主意。它帮助我们阅读代码,更重要的是,它将帮助您调试代码。为了你自己的利益。您可能会被要求在几周/几个月内修改此代码,最终您将感谢我。说。了解。即使是这样也不安全!您的预期输出与实际输出是什么?如果您已经计划好了模式,那么更容易看到答案。你能发布显示父母id或祖先的模式吗。如果你没有,我们需要知道。扭转潮流,反对教授/传播草率和危险的编码实践。如果你在没有准备好陈述的情况下发布了答案。另外,。说。了解。即使是这样也不安全!好的,但问题还没有解决,我想计算每个级别的成员数。例如,您登录后,仪表板中有15个级别。所以我想统计一下你们15个级别中每一个级别的成员。基本上,我创建传销软件。我已经共享了前两个级别的代码。请检查是否存在与当前用户下的用户相关的父\u id。因此,第二个查询应该类似于$query2level=“选择fname、email、doj、active、level、username、pcktaken from affiliateuser,其中referedby='$countusername'和parent_id=$_SESSION['id']”;我已经通过(level='$countuserlevel')将第二级与第一级关联起来。因为$countuserlevel变量是在第一级查询中分配的。请仔细检查代码。请帮我解决这个问题。谢谢
$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]);
<?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
)