php递归无限查询上级或者下级

1.方法1

 $this->get_array($user['uid'],1);

    function get_array($user_id,$top=0){
        $sql = 'SELECT * FROM  ' . tablename('user').'where pid=:pid';
        $params = array(':pid' => $user_id);
        $rows = pdo_fetchall($sql, $params);
        foreach ($rows as $key=>$value)
        {
            $r = $this->get_array($value['uid']); //调用函数,传入参数,继续查询下级
            $arr[0]['children'][$key]['uid']= $value['uid']; //组合数组
            $arr[0]['children'][$key]['wechaname']= $value['wechaname']; //组合数组
            if(is_array($r)){
                $arr[0]['children'][$key]['children']= $r[0]['children'];
            }

        }
        return $arr;
    }

2.方法2

function get_arrays($user_id,$arr = array()){
    $sql = 'SELECT * FROM  ' . tablename('user').'where uid=:uid';
    $params = array(':uid' => $user_id);
    $rows = pdo_fetch($sql, $params);

    $arr = array_push($arr,$rows);
    if (empty($arr['pid'])){
        return $arr;
    }
    $this->get_arrays($rows['pid'],$arr);

}

3.方法3,符合条件则返回

static function get_parent_branch($branch_id)
    {
        $sql = 'SELECT * FROM  ' . self::$table.'where bid='.$branch_id;
        $row = $GLOBALS['db']->getRow($sql);

        if (!$row || empty($row['parent_id'] == 0) || $row['if_zone'] == 1)
        {
            return $branch_id;
        }

        if (!$row || empty($row['parent_id'] == 0))
        {
            return $branch_id;
        }


        return self::get_parent_branch($row['parent_id']);

    }

你可能感兴趣的:(php,html,js,jquery)