Summary of methods for implementing Php+mysql infinite classification
This article mainly gives you a summary of the implementation of the Php+mysql Unlimited classification of 2 methods, and comparative analysis of the advantages and disadvantages of 2 methods, the need for friends can refer to the following
Infinite classification is an old topic, see how PHP is implemented with MySQL.
The first of these methods
This method is a very common, very traditional one, the first look at the table structure
Table: Category
ID int primary KEY, self-increment
Name varchar category names
PID int parent class ID, default 0
The PID of the top category is 0 by default. When we want to take out the sub-classification tree of a classification, the basic idea is recursion, of course, for efficiency issues do not recommend each recursive query database, the general practice is to first say all the categories are taken out, saved to the PHP array, then processed, and finally can be cached to improve the efficiency of the next request.
Start by building a primitive array, which is simply pulled out of the database:
The code is as follows:
$categories = Array (
Array (' ID ' =>1, ' name ' = ' computer ', ' pid ' =>0),
Array (' ID ' =>2, ' name ' = ' phone ', ' pid ' =>0),
Array (' ID ' =>3, ' name ' = ' notebook ', ' pid ' =>1),
Array (' ID ' =>4, ' name ' = ' desktop ', ' pid ' =>1),
Array (' ID ' =>5, ' name ' = ' smart machine ', ' pid ' =>2),
Array (' ID ' =>6, ' name ' = ' function machine ', ' pid ' =>2),
Array (' ID ' =>7, ' name ' = ' Super Ben ', ' pid ' =>3),
Array (' ID ' =>8, ' name ' = ' game Ben ', ' pid ' =>3),
);
The goal is to turn it into the following structure
Computer
Notebook
Super Ben
Game Ben
Desktop
Cell phone
Intelligent Machine
function machine
With an array, you can add a children key to store its sub-categories:
The code is as follows:
Array
1 corresponding ID, easy to read directly
1 = Array (
' ID ' =>1,
' Name ' = ' computer ',
' PID ' =>0,
Children=>array (
&array (
' ID ' =>3,
' Name ' = ' Notebook ',
' PID ' =>1,
' Children ' =>array (
omitted here
)
),
&array (
' ID ' =>4,
' Name ' = ' Desktop ',
' PID ' =>1,
' Children ' =>array (
omitted here
)
),
)
),
Other categories omitted
)
Processing process:
The code is as follows:
$tree = Array ();
The first step is to use the classification ID as the array key and create the children unit
foreach ($categories as $category) {
$tree [$category [' id ']] = $category;
$tree [$category [' id ']][' children '] = array ();
}
Second, by using a reference, each classification is added to the parent class children array, so that a single traversal can form a tree structure.
foreach ($tree as $k = = $item) {
if ($item [' pid ']! = 0) {
$tree [$item [' pid ']][' Children '] [] = & $tree [$k];
}
}
Print_r ($tree);
The printing results are as follows:
The code is as follows:
Array
(
[1] = = Array
(
[id] = 1
[name] + = computer
[PID] = 0
[children] = = Array
(
[0] = = Array
(
[id] = 3
[name]/= Notebook
[PID] = 1
[children] = = Array
(
[0] = = Array
(
[id] = 7
[Name] = + Super Ben
[PID] = 3
[children] = = Array
(
)
)
[1] = = Array
(
[id] = 8
[Name] + = game Ben
[PID] = 3
[children] = = Array
(
)
)
)
)
[1] = = Array
(
[id] = 4
[Name] + desktop
[PID] = 1
[children] = = Array
(
)
)
)
)
[2] = = Array
(
[id] = 2
[name] + = Phone
[PID] = 0
[children] = = Array
(
[0] = = Array
(
[id] = 5
[Name] = + Smart Machine
[PID] = 2
[children] = = Array
(
)
)
[1] = = Array
(
[id] = 6
[Name]/= function machine
[PID] = 2
[children] = = Array
(
)
)
)
)
[3] = = Array
(
[id] = 3
[name]/= Notebook
[PID] = 1
[children] = = Array
(
[0] = = Array
(
[id] = 7
[Name] = + Super Ben
[PID] = 3
[children] = = Array
(
)
)
[1] = = Array
(
[id] = 8
[Name] + = game Ben
[PID] = 3
[children] = = Array
(
)
)
)
)
[4] = = Array
(
[id] = 4
[Name] + desktop
[PID] = 1
[children] = = Array
(
)
)
[5] = = Array
(
[id] = 5
[Name] = + Smart Machine
[PID] = 2
[children] = = Array
(
)
)
[6] = = Array
(
[id] = 6
[Name]/= function machine
[PID] = 2
[children] = = Array
(
)
)
[7] = = Array
(
[id] = 7
[Name] = + Super Ben
[PID] = 3
[children] = = Array
(
)
)
[8] = = Array
(
[id] = 8
[Name] + = game Ben
[PID] = 3
[children] = = Array
(
)
)
)
Advantages: Clear relationship, modify the relationship between the subordinate simple.
Cons: With PHP processing, if the number of categories is large, the efficiency will be reduced.
The second method of
This method adds a path field to the table fields:
Table: Category
ID int primary KEY, self-increment
Name varchar category names
PID int parent class ID, default 0
Path varchar paths
Sample data:
ID name PID Path
1 PC 0 0
2 Mobile 0 0
3 Notebooks 1 0-1
4 Super Ben 3 0-1-3
5 games Ben 3 0-1-3
The path field records the paths from the root classification to the parent class at the top level, denoted by id+ '-'.
This way, suppose we want to query all the descendants of the computer under the classification, only need an SQL statement:
Select Id,name,path from category where path like (select Concat (Path, '-', id, '% ') as path from category where id=1);
Results:
+----+-----------+-------+
| ID | name | Path |
+----+-----------+-------+
| 3 | Notebooks | 0-1 |
| 4 | Super Ben | 0-1-3 |
| 5 | Game Book | 0-1-3 |
+----+-----------+-------+
This approach has also been adopted by many people, and I summed up the following:
Pros: Easy to query, high efficiency, the path field can be indexed.
Cons: Update node relationship trouble, need to update all descendants of the path field.
The above is the whole content of this article, two ways, which do you like? I hope we can enjoy it.
http://www.bkjia.com/PHPjc/962922.html www.bkjia.com true http://www.bkjia.com/PHPjc/962922.html techarticle The method of realizing infinite classification of php+mysql This article mainly gives you a summary of the implementation of the Php+mysql Unlimited classification of 2 methods, and comparative analysis of the advantages and disadvantages of 2 methods, the need for friends can ...