递归调用 - PHP递归获取数据
需求场景:通过公司某个部门的总监名称获取所有该部门的员工。
比如,通过总监获取所有副总监,再通过各个副总监获取所有各个副总监对应的经理,再通过经理获取对应经理的员工。
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>
回复内容:
需求场景:通过公司某个部门的总监名称获取所有该部门的员工。
比如,通过总监获取所有副总监,再通过各个副总监获取所有各个副总监对应的经理,再通过经理获取对应经理的员工。
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>
貌似纯 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>
问 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>
呵呵,既然你有如此个性化的需求,就应该有一个性化的专用表, 在你的表里面再加一个字段,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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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