mysql - 如何获得多层次营销(树)子计数

标签 mysql codeigniter

我已经使用 codeigniter 作为前端 mysql 作为后端创建了 MLM 项目。

这里我对获取 child 数量有疑问。

这是我的表格流程:

enter image description here

这是我的树流:

enter image description here

This is my query How to get Multi-level marketing (tree) child count.

例如: 主用户有 14 个子用户。

我试过这段代码:

查看:

$product2_users = $this->login->getProduct2Users($top_id);

型号:

public function getProduct2Users($top_id)
    {
        $count = 0;
        $this->db->select('id');
        $this->db->from('member');
        $this->db->where('sponsor', $top_id);
        $first_child = $this->db->get();

        foreach($first_child->result_array() as $f_child)
        {
            $f_child_id[] = $f_child['id'];
            if(isset($f_child_id))
            {
                $this->db->select('id');
                $this->db->from('member');
                $this->db->where('sponsor', $f_child_id);
                $second_child = $this->db->get();
            }
        }
        echo'<pre>';print_r($second_child->result_array());exit;
        return $first_child->result_array();
    }

最佳答案

如果你真的想要一个解决方案,你必须以递归的方式来做

像下面这样的东西应该可以工作

public function getCountChilds($id)
{
    $count = 0;

    $query = $this->db
        ->select('id')
        ->from('member')
        ->where('sponsor', $id)
        ->get();

    if ($query->num_rows() > 0)
    {
        foreach($query->result() AS $objChild)
        {
            $count += $this->getCountChilds($objChild->id);
            ++ $count;
        }
    }
    return $count;
}

关于mysql - 如何获得多层次营销(树)子计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52402721/

相关文章:

php - 当使用 jQuery/AJAX 和 PHP 选中多个复选框时,如何对多个选项进行排序

mysql - 我在 mysql 中收到错误 1452 (23000)

php - 将 codeigniter 迁移到另一台服务器

php - CodeIgniter:$this->db->query 中的 LEFT JOIN 返回数据库错误

php - PasswordHash 不适用于 CodeIgniter

php - CodeIgniter 项目中的 Assets 应该放在哪里?

使用 mysql -e 将 MySQL 转储为 SQL 格式?

mysql - 在部门平均最高工资的基础上增加员工工资 1000

php - 使用 nusoap Web 服务将数据插入 MySql?

php - Codeigniter 在 View 问题中显示私有(private)消息?