數據庫思想——查詢出指定用戶的所有下級

在這裏插入圖片描述

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
class Index extends Controller
{
    public function index()
    {
    	$arr=db('users')
    		->select();
    	$res=$this->GetTeamMember($arr ,1);
    	echo "<pre>";
    	print_r($res);die;
    	$this->assign('res',$res);
       	return view('index');
    }


	function GetTeamMember($arr,$mid) {
		$Teams=array();//最終結果
		$mids=array($mid);//第一次執行時候的用戶id
		do {
			$othermids=array(); 
			$state=false;
			foreach ($mids as $valueone) {
				foreach ($arr as $key => $valuetwo) {
					if($valuetwo['parentid']==$valueone){
						$Teams[]=$valuetwo;//找到我的下級立即添加到最終結果中
						$othermids[]=$valuetwo['id'];//將我的下級id保存起來用來下輪循環他的下級
						array_splice($arr,$key,1);//從所有會員中刪除他
						$state=true;	
					}
				}			
			}
			$mids=$othermids;//foreach中找到的我的下級集合,用來下次循環
		} while ($state==true);
		return $Teams;
	}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章