Php implements unlimited classification query (recursive and non-recursive)

Source: Internet
Author: User
After such a long time as PHP was done, it was found that one of the indispensable application modules of the background management system is the classification of the columns. In general, the columns should be made infinitely, that is to say, you can add sub-topics to each topic theoretically. In my opinion, this situation is not very complicated as a whole. The only relatively difficult point is the query of Infinitely-level columns. Lower

After such a long time as PHP was done, it was found that one of the indispensable application modules of the background management system is the classification of the columns. In general, the columns should be made infinitely, that is to say, you can add sub-topics to each topic theoretically. In my opinion, this situation is not very complicated as a whole. The only relatively difficult point is the query of Infinitely-level columns. Lower

After such a long time as PHP was done, it was found that one of the indispensable application modules of the background management system is the classification of the columns. In general, the columns should be made infinitely, that is to say, you can add sub-topics to each topic theoretically. In my opinion, this situation is not very complicated as a whole. The only relatively difficult point is the query of Infinitely-level columns.

Next I will give you a brief introduction to this situation. There are generally two ways to query such infinitely-level columns, one of which is to use the stack mechanism, another method is to use recursive functions (of course, the implementation mechanism of recursive functions is also implemented by stack ). The two methods are described below.

Recursive Function implementation

As mentioned above, recursive functions are implemented by means of the stack mechanism, but the underlying stack processing is transparent to programmers. Programmers only need to care about the application implementation logic. Therefore, it is easier to understand the above problems through recursive processing, and the code is also concise.

Now that recursive functions are used, we can see that the names must be customized functions. Let me talk about its implementation ideas first. The details are reflected in the Code.

For each layer of functions, the main task is to find the column with the parent Id as the current Id and then call its own function again, use the column id as the parent id of the next layer.

The flowchart is as follows:

I don't know if you can understand the above explanation. It doesn't matter. Let's look at the code below.

<? Php/*** personal blog: trace Yi blog * blog address: www.onmpw.com * recursively implement infinitus Classification */$ channels = array ('id' => 1, 'name' => "Clothes", 'parid' => 0), array ('id' => 2, 'name' => "books ", 'parid' => 0), array ('id' => 3, 'name' => "T-shirt", 'parid' => 1 ), array ('id' => 4, 'name' => "Pants", 'parid' => 1), array ('id' => 5, 'name' => "Shoes", 'parid' => 1), array ('id' => 6, 'name' => "Shoes ", 'parid' => 5), array ('id' => 7, 'name' => "sports shoes", 'parid' => 5 ), array ('id' => 8, 'name' => "Nike", 'parid' => 7), array ('id' => 9, 'name' => "Nike", 'parid' => 3), array ('id' => 10, 'name' => "hongxingke ", 'parid' => 7), array ('id' => 11, 'name' => "novel", 'parid' => 2 ), array ('id' => 12, 'name' => "Science Fiction", 'parid' => 11), array ('id' => 13, 'name' => "classical classics", 'parid' => 11), array ('id' => 14, 'name' => "Literature ", 'parid' => 2), array ('id' => 15, 'name' => "four books and five classics", 'parid' => 14 )); $ html = array (); /*** recursively find the node whose parent id is $ parid * @ param array $ html stores the node found according to the structure of the parent-child * @ param int $ parent specified by parid id * @ param array $ channels data array * @ param int $ depth of dep traversal, initialize to 1 */function getChild (& $ html, $ parid, $ channels, $ dep) {/** to traverse data, find the id specified by the parId parameter $ parid */for ($ I = 0; $ I
 
  
$ Channels [$ I] ['id'], 'name' => $ channels [$ I] ['name'], 'dep' => $ dep ); getChild ($ html, $ channels [$ I] ['id'], $ channels, $ dep + 1) ;}} getChild ($ html, 0, $ channels, 1);?>
 

This is the core code for recursive query of unlimited columns. We should have a clear understanding of its implementation process.

Non-recursive query of unlimited columns using the stack Mechanism

In the above section, we will briefly introduce how to use recursion to query infinite-level columns. Below we will briefly introduce non-recursive methods. Although there is no need for recursive functions, but in view of the structure page of the infinite column, we need to refer to the implementation mechanism of recursion-stack mechanism to solve this problem.

When I went to school, the teacher said, in fact, the core mechanism of stack is also four words: Advanced and later.

This is not much about the stack mechanism. It mainly describes how to use the stack to implement unlimited column query.

1. First, push the top-level columns into the stack.

2. Move the top element of the stack out of the stack

3. Store the output stack element into the array and mark its depth (the depth is to add 1 above the depth of its parent column)

4. Search for its child columns based on the elements of the output stack.

5. Add the searched sub-columns to the stack and repeat Step 2.

6. If the stack is determined to be empty, the process ends;

By translating the above steps, you can translate these steps into PHP code. The core code is as follows:

<? Php/*** personal blog: trace Yi blog * blog address: www.onmpw.com * use non-recursion, that is, use the stack method to query the topic's infinitus Categories */$ channels = array ('id' => 1, 'name' => "Clothes ", 'parid' => 0), array ('id' => 2, 'name' => "books", 'parid' => 0 ), array ('id' => 3, 'name' => "T-shirt", 'parid' => 1), array ('id' => 4, 'name' => "Pants", 'parid' => 1), array ('id' => 5, 'name' => "Shoes ", 'parid' => 1), array ('id' => 6, 'name' => "leather shoes", 'parid' => 5 ), array ('id' => 7, 'name' => "sports shoes", 'parid' => 5), array ('id' => 8, 'name' => "Nike", 'parid' => 7), array ('id' => 9, 'name' => "Nike ", 'parid' => 3), array ('id' => 10, 'name' => "hongxingke", 'parid' => 7 ), array ('id' => 11, 'name' => "novel", 'parid' => 2), array ('id' => 12, 'name' => "Science Fiction", 'parid' => 11), array ('id' => 13, 'name' => "classical classics ", 'parid' => 11), array ('id' => 14, 'name' => "Literature", 'parid' => 2 ), array ('id' => 15, 'name' => "four books and five classics", 'parid' => 14); $ stack = array (); // define an empty stack $ html = array (); // used to save the relationship between each column and the depth of the column/** custom stack function */function pushStack (& $ stack, $ channel, $ dep) {array_push ($ stack, array ('channel' => $ channel, 'dep' => $ dep ));} /** custom stack function */function popStack (& $ stack) {return array_pop ($ stack );} /** first press the top-level column into the stack */foreach ($ channels as $ key => $ val) {if ($ val ['parid'] = 0) pushStack ($ stack, $ val, 0);}/** get the elements in the stack out of the stack and find its subcolumns */do {$ par = popStack ($ stack ); // pull the top element of the stack out of the stack/** find the id of the parent column of this column, and add these columns into the stack */for ($ I = 0; $ I
 
  
$ Par ['channel'] ['id'], 'name' => $ par ['channel'] ['name'], 'dep' => $ par ['dep']);} while (count ($ stack)> 0 );
 

The above is implemented in non-recursive mode.

Download: https://github.com/onmpw/phpApp

Summary

The above two methods have their own advantages and disadvantages. Although the implementation form is different above, the implementation mechanism of the two is the same in view of the structure of the unlimited level column-both are implemented through stack. In reality, we need to select a method based on the actual situation.

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.