In various columns and classification settings, the infinite classification is often used, and infinite classification in the order of the time must use recursion, here for a detailed analysis and interpretation.
first, let's take a look at recursive functions:
recursive functions are used in language learning, because it is very common, in essence, recursive function is to call their own functions.
As an example:
<?php
function Test () {
static $a =0;
if ($a <10) {
$a + +;
Test ();
}
echo $a. " <br/> ";
}
Test ();
The test function inside again calls itself, this is commonly known as recursive function. Recursive functions are conditionally set, otherwise the infinite loop, which will cause the program to crash.
So generally I summarize recursive functions with two features:
One is the record condition value, and the record's condition value must be guaranteed that it will not be lost the next time it is called.
The $a in the test function is the record condition value, which relies on the static keyword to ensure that the value of each increment of the record is not lost until the next time the test () function is called, because the static-decorated variable in the function is initialized only for the first time and retains the value of the variable. So as long as this is guaranteed, not only is it static, but other ways can be achieved, such as global and & modifiers.
the other is conditional checking. The limit to the size of the $a in test is the process of checking the condition.
The if ($a <10) in the test function is the procedure for this condition check, which restricts the invocation of the test () function to itself, thus preventing an infinite invocation from causing the program to crash.
Function A inside call another function B, if the B function does not complete, then the a function will wait until the B function completes, will return to a function to continue execution. This feature is used in the recursive process, which helps us sort the infinite categories by using the above test () recursive function Description:
The results of the above test () Print:
10,10,10,10,10,10,10,10,10,10,10
It may not be the same as many people think, and probably a lot of people will think it should not be the result of 0,1,2,3,4,5,6,7,8,9,10.
This is because the function test invocation, as long as the $A<10, will call its own test (), each call of test () did not reach the Echo, that is, each call the test function does not end until $a to 10, the first echo $a, this time $ A is already 10, so the first 10 is actually recursive 11 times after the $a, the 11th recursive test, which does not conform to the <10 condition, ends with the function, which begins returning to the recursive tenth test function to perform echo, which is already 10 because the $a is a static variable. So the result of ECHO is 10, and then go back to the previous test function to complete the previous incomplete echo step, so echo out 11 10, and the last 10 is actually the first time the echo result of the test function is executed.
An infinite sort of sorting is done with this principle, the recursive sort function consistently matches subclasses of the class by ParentID equal to the upper-level ID, and as long as the first subclass is found, the ID of the subclass found is used to find the next level of subclasses until there is no more subordinate subclass. To go back to the previous level and then continue to find, find and then start looking for the subclass of the next class subclass, until there is no return, the process continues to loop. may not be able to understand the text, the following example I will draw the legend, please look back first.
Let's take a look at an infinitely sorted data er diagram:
For example, the category of a skirt, it has a parent category of women, women's clothing and the category of clothes, the assumption that the skirt ID of 3, Women for 2, clothing for 1, then the skirt of the parentid is the direct superior category skirt ID, so parentid= 2, while the child is the entire ancestor element ID of the tree structure of the skirt, then the child should be 1,2,3, so the skirt's depth is deep to 3, I suppose it is used, separated, or separated by other symbols, and the title is not to mention the skirt.
Specifically, let's look at this tree-shaped structure:
from the tree structure classification can continue to go on, the first level of the ID is the next level of ParentID, is through the ID and parent of these two columns to achieve the basic infinite classification, and then the infinite classification of the recursive sort is dependent on the relationship between the two fields.
Here is an infinite assortment of table structures and examples of data:
To get a clearer picture of the following tree chart:
The ID of the previous generation's category is its own parentid, the highest level of ParentID is 0, this is very important, is the basis for the sorting of infinite classification.
We want the infinite sorting effect should be the columns under the columns are placed under the column, with the ID of the order, we want the effect is the following, in order to facilitate the view I used the tab and | | To interval between the relationship between columns:
Clothes
|-|-Men
|--Casual Tops
|--Short Sleeves
|--Long Sleeves
|--Casual Pants
|--Women
|--Women's Tops
|--Women's clothing
|--Jeans
|--Skirts
Because the categories can extend indefinitely, it's obvious here that we need to use recursive functions for sorting categories.
Get all the results of the data and sort by ID
$mysqli =new mysqli (' localhost ', ' root ', ' root ', ' test ') or Die ("Connection Failed");
$mysqli->set_charset ("UTF8");
$re = $mysqli->query ("select * from col-id ASC");
$result = $re->fetch_all (MYSQLI_ASSOC);
Recursive sort function
The function recursion ($result, $parentid =0) {/
* records sorted array of categories */
static $list =array ();
foreach ($result as $k => $v) {
if ($v [' ParentID ']== $parentid) {/
* Put the category's data in the list */
$list []= $v;
Recursion ($result, $v [' id ']);
}
return $list;
}
At this point we can get an array sorted by category, but if you want to have a format like the one above ——, you need to improve the function.
function recursion ($result, $parentid =0, $format = "| |") {/
* The array of categories after sorting/
static $list =array ();
foreach ($result as $k => $v) {
if ($v [' ParentID ']== $parentid) {
if ($parentid!=0) {
$v [' title ']=$ format. $v [' title '];
}
/* Put the category of data into the list
/* * $list []= $v;
Recursion ($result, $v [' id '], "". $format);
}
return $list;
}
$list =recursion ($result, 0, ' | | ');
The results of this sort are as follows:
The implementation of the entire recursive function is probably what it is, we have mentioned above to draw an example diagram, look at the picture is very clear to this principle, the figure is only part of the picture, but enough to understand the process: