Table of Contents
回复内容:
Home Backend Development PHP Tutorial 递归调用 - PHP递归获取数据

递归调用 - PHP递归获取数据

Jun 06, 2016 pm 08:23 PM
php recursive call

需求场景:通过公司某个部门的总监名称获取所有该部门的员工。
比如,通过总监获取所有副总监,再通过各个副总监获取所有各个副总监对应的经理,再通过经理获取对应经理的员工。
mysql的表结构这么设计的,总监的id是1,那么副总监的parent_id就是1,如果副总监的id分别是2、3、4、5等等,那么各个经理的parent_id就是2、3、4、5,依次类推形成一个递归的数据。
问1:PHP如何通过递归一次性把所有总监以下的数据保存到一个变量呢?
问2:取出数据后如何区分哪些是副总监哪些是经理哪些是一线员工呢?
问3:如何分页?(按照我的理解,即使是分页也需要把全部数据递归完毕之后再进行计算,而不是limit 1,10等形式分页)
问题有点多,还请各位大拿请教,多谢。

如下是我的代码。貌似死循环了。。。

<code>public function recuresion($res)
    {
        foreach ($res as $k => $v)
        {
            $tmpRes[] = $this->obj->getResult($v['id']);  //假设通过该方法获取副总监、经理、员工
            if (!empty($tmpRes))
            {
                return $this->recuresion($tmpRes);
            }
            else
            {
                return $tmpRes;
            }
        }
    }
$a = recuresion($res);  //这里假设$res是获取的总监的数据
print_r($a);</code>
Copy after login
Copy after login

回复内容:

需求场景:通过公司某个部门的总监名称获取所有该部门的员工。
比如,通过总监获取所有副总监,再通过各个副总监获取所有各个副总监对应的经理,再通过经理获取对应经理的员工。
mysql的表结构这么设计的,总监的id是1,那么副总监的parent_id就是1,如果副总监的id分别是2、3、4、5等等,那么各个经理的parent_id就是2、3、4、5,依次类推形成一个递归的数据。
问1:PHP如何通过递归一次性把所有总监以下的数据保存到一个变量呢?
问2:取出数据后如何区分哪些是副总监哪些是经理哪些是一线员工呢?
问3:如何分页?(按照我的理解,即使是分页也需要把全部数据递归完毕之后再进行计算,而不是limit 1,10等形式分页)
问题有点多,还请各位大拿请教,多谢。

如下是我的代码。貌似死循环了。。。

<code>public function recuresion($res)
    {
        foreach ($res as $k => $v)
        {
            $tmpRes[] = $this->obj->getResult($v['id']);  //假设通过该方法获取副总监、经理、员工
            if (!empty($tmpRes))
            {
                return $this->recuresion($tmpRes);
            }
            else
            {
                return $tmpRes;
            }
        }
    }
$a = recuresion($res);  //这里假设$res是获取的总监的数据
print_r($a);</code>
Copy after login
Copy after login

貌似纯 SQL 就能搞定,不需要 PHP 掺和……

问 1:

<code>select  id,
        name,
        parent_id 
from    (select * from employee
         order by parent_id, id) sorted,
        (select @pv := '1') initialisation
where   find_in_set(parent_id, @pv) > 0
and     @pv := concat(@pv, ',', id)
</code>
Copy after login

问 2:
外面再套一层就好了:select * from (问 1 的 SQL) e1 where not exists (select * from employee e2 where e2.parent_id = e1.id)

问 3:
既然是纯 SQL 的话,直接 limit 分页就好了。

非要 PHP 的话:

<code>public function recursion($res)
{
    $output = array();
    foreach ($res as $k => $v)
    {
        $tmpRes = $this->obj->getResult($v['id']);
        $output []= $v;
        if (!empty($tmpRes))
        {
            $output = array_merge($output, $this->recursion($tmpRes));
        }
    }
    return $output;
}
$a = recursion($res);</code>
Copy after login

呵呵,既然你有如此个性化的需求,就应该有一个性化的专用表, 在你的表里面再加一个字段,int level ,level这个字段意义就是相对于顶级分类,自己这条记录是属于第几级分类,
如:
总监 的level 是 0
副总监 的level 是 1
经理 的level是 2
.....
插入记录的时候,应该能够知道。父记录的level是多少,然后+1以后存进数据库。

这样子就楼主的问题就简单了,
1,所有总监以下的员工就是level非0的记录。
2,通过level可以分清楚员工的类型,
3,分页,还是通过limit 0,10 这种方法分。

手机码字,排版不方便,请自己慢慢看

$tmpRes[]?不清空的话这数组越来越大

<code>$result=array();
public function recuresion($res)
    {
        $tmpRes=array();
        foreach ($res as $k => $v)
        {
            $result[k]=$v;
            $tmpRes[] = $this->obj->getResult($v['id']);  //假设通过该方法获取副总监、经理、员工
           
        }
         if (!empty($tmpRes))
         {
               
             return $this->recuresion($tmpRes);
         }
            
    }</code>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles