php递归 无限极分类、分销方法 以tp5为例,老年人记忆系列,记不住就copy一下提高效率
无限极从上至下
//无限极从上至下
public function wuxianji($list,$pid=0,$level=0){
//$list为查询数组列表
foreach ($list as $rows){
static $cate_list=array();
if ($rows['type_pid']==$pid){
$rows['level']=$level;
$cate_list[]=$rows;
$this->wuxianji($list,$rows['type_id'],$level+1);
}
}
return isset($cate_list)?$cate_list:null;
}
上效果图:
无限极从下至上(分销居多)
//无限极从下至上
public function fenxiao($id,$pid,$level=1,$goodsfen,$order_no){
static $arr=array();
if($pid!=0){
if($level!=4){
$data=db('user')->where('userid',$pid)->find();
$fen=db('fen')->where('id',$level)->find();
$bfb=$fen['fenbfb'];
$f=floor($goodsfen*$bfb);
db('user')->where('userid',$pid)->setInc('userfen',$f);
$userfen=array(
'user_id'=>$data['userid'],
'status'=>0,
'time'=>time(),
'fen'=>$f,
'order_no'=>$order_no,
'userfenxiao'=>$id
);
db('userfen')->insert($userfen);
$arr[] = $data;
$this->fenxiao($id,$data['userpid'],$level+1,$goodsfen,$order_no);
}
}
}