PHP递归
/*
* 递归获取所有分类
* @param int $parent_id
* @param int $lv
* @param array $res
* @return arary
*/
public function get_all_type($parent_id=0,&$lv=0,&$res=array()){
$where['parent_id']=$parent_id;
$list=$this->where($where)->select();
if(!empty($list[0])){
$lv++;
foreach($list as $item){
$item['lv']=$lv;
$nbsp='';
for($i=1;$i<$lv;$i++){
$nbsp.=' ';
}
$item['lv_str']=$nbsp;
$res[$item['type_id']]=$item;
$this->get_all_type($item['type_id'],$lv,$res);
}
$lv--;
}
return $res;
}
/**
* 递归获取用户所有下线
* @param int $parent_id
* @param array $res
* @return array
*/
function get_user_all_childs($parent_id = 0, &$res = array()) {
$list = $db->getAll("SELECT user_id FROM users WHERE parent_id = '$parent_id'");
if (!empty($list[0])) {
foreach ($list as $item) {
$res[] = $item['user_id'];
get_user_all_childs($item['user_id'], $res);
}
}
return $res;
}