phptp5递归查询

//获取拿货上级
public function getTakeGoodsMember($member_id) {
$allMember = Db::query("SELECT id,superior_userid,user_name,level_id,take_goods_userid
FROM (
SELECT
@r AS _id,
(SELECT @r := take_goods_userid FROM " . config('database.prefix') . "user WHERE id = _id) AS parent_id,
@l := @l + 1 AS lvl
FROM
(SELECT @r :=? , @l := 0) vars,
" . config('database.prefix') . "user h
WHERE @r <> 0) T1
JOIN " . config('database.prefix') . "user T2
ON T1._id = T2.id", [$member_id]);
return $this->getTreeTakeGoodsMember($allMember, $member_id, $depth = 0);
}

/**
* 获取下面团队的人不包括自己
* @param type $member_id
* @return type
*/
public function getChildMember($member_id) {
return Db::query("SELECT id,superior_userid,user_name,level_id,take_goods_userid
FROM (
SELECT
@r AS _id,
(SELECT @r := id FROM " . config('database.prefix') . "user WHERE superior_userid = _id) AS parent_id,
@l := @l + 1 AS lvl
FROM
(SELECT @r :=? , @l := 0) vars,
" . config('database.prefix') . "user h
WHERE @r <> 0) T1
JOIN " . config('database.prefix') . "user T2
ON T1._id = T2.superior_userid", [$member_id]);
}

//获取下级
public function getDownline($allMember, $userid, $depth = 0) {
$arr = array();
foreach ($allMember as $v) {
if ($v['superior_userid'] == $userid) {
$v['depth'] = $depth + 1;
$arr[] = $v;
$arr = array_merge($arr, $this->getDownline($allMember, $v['id'], $depth + 1));
}
}
return $arr;
}

你可能感兴趣的:(phptp5递归查询)