dongzanghong4379 2018-11-12 21:55
浏览 23

如何循环一个表并使用php选择包含在主体中的某个标准的下线数据

I am building a multi-level referral system that has downlines. In the referral table there is a row for only the id of the parent. Now i need to write a query that loops the table selecting all the children, grandchildren, great grandchildren, etc of an upline. For example, I want to select the id of all children of an upline and then use the id of the children and select the downlines of the children and continue like that until the last generation or downlines of the last child is selected.

My challenge is that since I do not know how many great grand children or how many legs that parent will have, i am having challenges constructing my query and writing the script to take care of that.

enter image description here

This is the code i have written so far, i want to get the IDs of all of the direct and indirect downlines, put them in an array and then loop through the array and get their details from the user table.

function cumulative_commission() {
    global $con;
    $user = $_SESSION['user_id'];
    $group_members = array();
    $sql1 = "SELECT * FROM `rpc` WHERE `user_id` = '$user' AND `confirmed` = 1";
    $select = mysqli_query($con, $sql1);
    if(mysqli_num_rows($select)>0){
        $group_members[] = $user;
    }
    $sql = "SELECT rpc.*, users.* FROM rpc ";
    $sql .= "LEFT JOIN users ON users.id = rpc.user_id ";
    $sql .= "WHERE rpc.top_id = '$user' AND confirmed =1";

    $query = mysqli_query($con,$sql);
    if(mysqli_num_rows($query)>0){
        while($data = mysqli_fetch_assoc($query)){
            $group_members[]=$data['user_id'];
        }
    }
    return $group_members;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测