php - 获取上线的所有传销下线 (php)

标签 php mysql binary-tree mlm

我想在一棵二叉树中获取父亲的所有下线,每个父亲都有左臂和右臂,每个臂都有左臂和右臂等等。 like the following image 。 在我的数据库中,我有一个名为 users 的表,每个用户都有一个父亲 id 和位置,即 L 或 R。

这是我的功能..但它仍然没有得到所有下线。 like the following image .

最佳答案

有两件事对我来说很突出:

  1. $i 参数以及 $this->downline_id_arr 的使用。

考虑这样做:

$children = array();
foreach($data as $row) {
    $child_id = $row->id;
    $children[$child_id] = array(/**/);
    $children = array_merge($children, $this->getAllDownline($child_id);
}
return $childen;

现在您不需要 $i 变量或 $this->downline_id_arr

  • 您正在逐一查询每个节点。
  • 考虑按级别查询:

    function getAllDownlines($fathers) {
        $data = "SELECT * FROM users WHERE father_id IN (/*fathers*/)";
        $new_father_ids = array();
        $children = array();
        foreach ($data as $child) {
            $children[$child->id] = array(/**/); // etc
    
            $new_father_ids[] = $child->id;
        }
        $children = array_merge($children, $this->getAllDownlines($new_father_ids);
        return $childen;
    }
    

    通常,查询越少,速度就越快,因此您应该会看到更好的性能。

    关于php - 获取上线的所有传销下线 (php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303705/

    相关文章:

    c++ - 存储部分和的二叉树 : Name and existing implementations

    php - 你如何将你的 sql 查询嵌入到 php 脚本中(编码风格)?

    php - Laravel Firebase 身份验证

    java - 如何在Java中返回二叉树中最频繁的元素

    mysql - 在 MySQL Workbench 中同步模型

    php - 按产品过滤订单

    java - 打印二叉树中所有根到叶的路径

    php - 如何在 PHP 中检查不完整的 POST 请求

    php - 如何将表 mysql 列设置为 hijri shamsi,阿拉伯日期

    mysql - 查找 1.00 美元以内的货币/金额