Do PHP for such a long time, found that the background management system is not a small application module is the classification of columns, the general situation of the column to be made is infinite class, that is, each column can theoretically add sub columns. In my opinion, this kind of situation is not very complicated as a whole, the only one that is relatively difficult is the query of the infinite level column.
So here's what I'm going to do to give you a simple introduction, for this kind of infinite column query generally has two kinds of methods, one is to use the stack mechanism, the other is the use of recursive functions (of course, recursive function Implementation mechanism is also implemented through the stack). In these two ways, we'll introduce each of the following.
Recursive function Implementation method
As mentioned above, recursive functions are also implemented with the help of stack, but the low-level processing of stacks is transparent to programmers, and programmers only need to care about the implementation logic of the application. So it is easier to understand the problem with recursion, and the code is simpler.
Now that we use recursive functions, we know that we have to rely on custom functions to see the names. I would like to talk about the implementation of the idea, the specific details we reflected in the code.
For each layer of the function of its main work is to find the parent ID is the current ID of the column, find to call their own function again, the ID of the found column as the next layer of the parent ID.
Its flowchart is as follows
Figure I
I don't know if we can understand the above explanation, it's okay. Look at the code directly below
<?php/** * Personal blog: Trace Memory Blog * Blog Address: www.onmpw.com * Recursive implementation of infinite polar Classification * * $channels = array (' ID ' =>1, ' name ' => ' clothing, ' p
ArId ' =>0 ', array (' ID ' =>2, ' name ' => ' book ', ' 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 ' => ' sneakers ', ' parid ' =>5 '), array (' ID ' =>8, ' name ' = > "Nike", ' Parid ' =>7, array (' ID ' =>9, ' name ' => ' Nike ', ' Parid ' =>3 '), array (' ID ' =>10, ' name ' => "" Parid ' =>7 ', array (' ID ' =>11, ' name ' => "novel", ' Parid ' =>2), array (' ID ' =>12, ' name ' => "Science Fiction", ' parid ' =
>11), array (' ID ' =>13, ' name ' => ' classical masterpiece ', ' Parid ' =>11 '), array (' ID ' =>14, ' name ' => ' literature ', ' Parid ' =>2),
Array (' ID ' =>15, ' name ' => ' Classics ', ' Parid ' =>14));
$html = Array (); /** * Recursive lookup node with parent ID $parid * @param array $html Store the lookup node in the structure of the parent-"@param int $parid the parent ID specified * @param array $Channels Data Array * @param int $DEP Traversal depth, initialized to 1/function Getchild (& $html, $parid, $channels, $DEP) {/* * traverse data, find p Arid the ID for the parameter $parid/for ($i = 0; $i <count ($channels), $i + +) {if ($channels [$i] [' parid '] = = $parid) {$html [
= Array (' ID ' => $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 recursive implementation of the infinite Level column query core code, combined with a graph of its implementation process should have a clearer understanding.
Non-recursion, that is, using stack mechanism to realize the query of infinite level column
On the above we have probably introduced a recursive way to achieve the infinite level of the query, the following we briefly introduce the way of not recursive. Although the recursive function is not the way, but in view of the structure of the infinite columns need reference recursive implementation mechanism-stack mechanism, to solve this problem.
In school, the teacher said, in fact, the core mechanism of the stack is four words: Advanced and out.
In this for the stack mechanism is not much said, mainly about how to use the stack to achieve unlimited level column query.
1. First the top column into the stack
2. Stack top element out stack
3. Put the stack element into the array, mark its depth (its depth is in the depth of its parent column plus 1)
4. Take out the stack element as the parent column, find its sub columns
5. Place the found sub column into the stack, repeat step 2
6. Judgment stack is empty, the process is over;
Through the translation of the above steps, you can translate these steps into PHP code, the core code is as follows
<?php/** * Personal blog: Trace Memory Blog * Blog Address: www.onmpw.com * Using a non-recursive, that is, using the stack way to achieve the column's infinite Polar classification query * * $channels = array (' ID ' =>1, ' name ' => ' clothes, ' Parid ' =>0, array (' ID ' =>2, ' name ' => ' book ', ' Parid ' =>0 '), array (' ID ' =>3, ' name ' => ' t-shirt ", '
Parid ' =>1 ', array (' ID ' =>4, ' name ' => ' trousers ', ' parid ' =>1 '), array (' ID ' =>5, ' name ' => ' shoes ', ' Parid ' =>1), Array (' ID ' =>6, ' name ' => ' shoes ', ' Parid ' =>5), array (' ID ' =>7, ' name ' => ' sneakers ', ' Parid ' =>5), array (' id ' = >8, ' name ' => ' Nike ', ' Parid ' =>7, array (' ID ' =>9, ' name ' => ' Nike ', ' Parid ' =>3 '), array (' ID ' =>10, ' name ' = > "hongxing", ' Parid ' =>7, array (' ID ' =>11, ' name ' => "novel", ' Parid ' =>2), array (' ID ' =>12, ' name ' => "Science Fiction" , ' Parid ' =>11), array (' ID ' =>13, ' name ' => ' classical masterpiece ', ' Parid ' =>11 '), array (' ID ' =>14, ' name ' => ' literature ', ' Parid '
=>2), array (' ID ' =>15, ' name ' => ' Classics ', ' Parid ' =>14)); $stack = Array (); Defines an empty stack $html = Array (); Used to keep the relationships between the columns and the depth of the column/* custom into the stack functions/function Pushstack (& $stack, $channel, $DEP) {Array_push ($stack, Array (' channel ' => $channel, ' dep ' => $DEP)}/* Custom out Stack function/function popstack (& $stack)
{return Array_pop ($stack);} * * First push top column into Stack/foreach ($channels as $key => $val) {if ($val [' parid '] = 0) pushstack ($stack, $val, 0);} * * Stack the elements out of a stack, find its sub-column/do{$par = Popstack ($stack);////* Find the stack top element out of the stack * * to the ID of this column as the parent column, these columns into the stack * * for ($i =0; $i <count ($channels); $i + +) {if ($channels [$i] [' parid '] = = $par [' channel '] [' ID ']] {pushstack ($stack, $channels [$i], $par [' DEP
']+1 '); }/* * To save the stack column and the depth of the column to the array/$html [] = array (' ID ' => $par [' channel '] [' id '], ' name ' => $par [' channel '] [' na ']
Me '], ' dep ' => $par [' DEP ']];
}while (Count ($stack) >0);
This is accomplished using a non recursive method.
Download code: HTTPS://GITHUB.COM/ONMPW/PHPAPP
Summarize
The above two ways have advantages and disadvantages, although the implementation of different forms above, but in view of the structure of the infinite column, both the implementation of the mechanism are the same-all by means of the stack to achieve. In reality, we have to choose a way to achieve according to the needs of the actual situation.