1

我正在使用 php 进行传销,其中每个节点只能有 3 个子节点。MLM 有 N 个级别。任何父节点的级别将等于其子节点的最低级别 + 1。深度为 n 的子节点的级别 = 1。

function calculate_level($starId=0) {

if($starId == null) return 1;

$result = getAllChildren($starId);

if($result && count((array)$result) == 3){

    return 1 + min(calculate_level($result[0]->user_ref_id),calculate_level($result[1]->user_ref_id),calculate_level($result[2]->user_ref_id));

}else{ return 1;}

}

function level_calculator($starId = 0) {

$result = getAllParents($starId);

//now we have all parents of current star now we need to find the level for each.

foreach($result as $res){

    if($res->user_status == 3 ){

        //we only update level if status == 3

        $level = calculate_level($res->user_ref_id);

        echo  $level  .':' . $res->user_ref_id .',';


    }
}

}

数据库布局图像。

在此处输入图像描述

预期结果

在此处输入图像描述

我怎样才能做到这一点。

谢谢。

4

0 回答 0