This article is divided into two situations. it introduces how PHP implements an infinite classification without recursion. if you are interested, you can refer to PHP for such a long time, an application module that is essential to the background management system is the classification of the topic. In general, the topic should be made infinitely, that is to say, each topic can be added 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.
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:
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 )); $ stack = array (); // defines 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.