Recursive call-PHP recursive Data Retrieval

Source: Internet
Author: User
Scenario: obtain all employees of a department by the Director name of a company. For example, obtain all the deputy directors through the Director, then obtain the managers corresponding to all the deputy directors through the Deputy Directors, and then obtain the employees of the corresponding managers through the manager. The table structure of mysql is designed in this way, the director's id... requirement scenario: obtain all employees of this Department through the Director name of a certain department of the company.
For example, obtain all the deputy directors through the Director, then obtain the managers corresponding to all the deputy directors through the Deputy Directors, and then obtain the employees of the corresponding managers through the manager.
If the table structure of mysql is so designed, the director's id is 1, then the Deputy Director's parent_id is 1, if the Deputy Director's id is 2, 3, 4, 5, and so on, then the parent_id of each manager is 2, 3, 4, 5, and so on to form a recursive data.
Question 1: How does PHP Save the data below all directors to a variable at a time through recursion?
Question 2: How can we distinguish between deputy directors, managers, and front-line employees after extracting data?
Question 3: How to paging? (According to my understanding, even paging requires recursion of all the data before computation, rather than paging in the form of limit)
There are a lot of questions. Thank you for your advice.

The following is my code. It seems like an endless loop...

Public function recuresion ($ res) {foreach ($ res as $ k => $ v) {$ tmpRes [] = $ this-> obj-> getResult ($ v ['id']); // assume that the deputy director, manager, and employee if (! Empty ($ tmpRes) {return $ this-> recuresion ($ tmpRes);} else {return $ tmpRes ;}}$ a = recuresion ($ res ); // assume that $ res is the data print_r ($ a) of the obtained director );

Reply content:

Scenario: obtain all employees of a department by the Director name of a company.
For example, obtain all the deputy directors through the Director, then obtain the managers corresponding to all the deputy directors through the Deputy Directors, and then obtain the employees of the corresponding managers through the manager.
If the table structure of mysql is so designed, the director's id is 1, then the Deputy Director's parent_id is 1, if the Deputy Director's id is 2, 3, 4, 5, and so on, then the parent_id of each manager is 2, 3, 4, 5, and so on to form a recursive data.
Question 1: How does PHP Save the data below all directors to a variable at a time through recursion?
Question 2: How can we distinguish between deputy directors, managers, and front-line employees after extracting data?
Question 3: How to paging? (According to my understanding, even paging requires recursion of all the data before computation, rather than paging in the form of limit)
There are a lot of questions. Thank you for your advice.

The following is my code. It seems like an endless loop...

Public function recuresion ($ res) {foreach ($ res as $ k => $ v) {$ tmpRes [] = $ this-> obj-> getResult ($ v ['id']); // assume that the deputy director, manager, and employee if (! Empty ($ tmpRes) {return $ this-> recuresion ($ tmpRes);} else {return $ tmpRes ;}}$ a = recuresion ($ res ); // assume that $ res is the data print_r ($ a) of the obtained director );

It seems that only SQL can be done, without the need to mix PHP ......

Question 1:

select  id,        name,        parent_id from    (select * from employee         order by parent_id, id) sorted,        (select @pv := '1') initialisationwhere   find_in_set(parent_id, @pv) > 0and     @pv := concat(@pv, ',', id)

Question 2:
Just set another layer outside:select * from (SQL statement of Question 1) e1 where not exists (select * from employee e2 where e2.parent_id = e1.id)

Question 3:
Since it is pure SQL, directlylimitPaging is fine.

PHP is required:

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);

Haha, since you have such Personalized Requirements, you should have a personalized dedicated table. Add a field, int level and level, in your table, to indicate that the field is relative to the top-level classification, this record belongs to the level of classification,
For example:
The director's level is 0.
The level of the Deputy Director is 1.
The manager's level is 2.
.....
You should be able to know when inserting records. What is the level of the parent record, and then + 1 is stored in the database.

In this way, the problem of the landlord is simple,
1. Employees below all directors are non-0 level records.
2. The level can be used to identify the employee type,
3, paging, or through the limit method.

It is inconvenient to typeset the phone code. Please take a look.

$ TmpRes []? If it is not cleared, the array will become larger and larger.

$ Result = array (); public function recuresion ($ res) {$ tmpRes = array (); foreach ($ res as $ k => $ v) {$ result [k] = $ v; $ tmpRes [] = $ this-> obj-> getResult ($ v ['id']); // assume that this method is used to obtain the deputy director, manager, and employee} if (! Empty ($ tmpRes) {return $ this-> recuresion ($ tmpRes );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.