The infinite classification is a kind of data structure often used in the actual development, we call it the tree structure generally.
title: Similar to Taobao commodity classification, you can set its subclasses in any category.
First, create ' type ' datasheet
' ID ' from growth
' FID ' int (11) default (0), parent Node ID
' Name ' varchar (50), category name
Copy Code code as follows:
CREATE TABLE ' type ' (
' id ' int (one) not NULL auto_increment,
' FID ' int (one) not NULL DEFAULT ' 0 ',
' Name ' varchar not NULL,
PRIMARY KEY (' id ')
)
Second, add
Let's add a few top-level categories first
Copy Code code as follows:
INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (NULL, ' 0 ', ' mobile ');
INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (NULL, ' 0 ', ' computer ');
INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (NULL, ' 0 ', ' shoes ');
INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (NULL, ' 0 ', ' clothes ');
Here Fid=0 is representing the top category
Then we add a few subcategories for {computer}
Copy Code code as follows:
INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (null, ' 2 ', ' Desktop '), (null, ' 2 ', ' notebook ');
Here fid=2,2 this ID is the ID of the category {computer}, if you add a subcategory of the {shoe} fid=3
In the same vein, we add subcategories for {notebook} fid=6
Copy Code code as follows:
INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (null, ' 6 ', ' Ausu '), (null, ' 6 ', ' hp ');
Third, delete
If we want to delete {notebook} This category, it's very simple
Copy Code code as follows:
DELETE from ' type ' WHERE ' ID ' =6
{sub-classification of Notebook} We should also remember to do the appropriate processing
Copy Code code as follows:
Function del ($FID) {
$sql = "SELECT * from ' type ' WHERE ' fid ' = $fid";
$rs =mysql_query ($sql);
for ($i = 0; $i < count ($rs); $i + +) {
$sql = "DELETE from ' type ' WHERE ' id ' ={$rs [$i] [' ID ']}";
mysql_query ($sql);
Del ($rs [' id ']);//recursive
}
}
Del (6);//Perform action
Here you may wonder why it's so troublesome to use recursion instead of just deleting it
Copy Code code as follows:
DELETE from ' type ' WHERE ' FID ' =6
So we can not directly delete {Ausu}, {hp}? However, suppose {Ausu} has a subcategory {A1},{A1} also has a subcategory {A2}, we cannot delete the data completely without recursion.
Third, find
1. Find a subcategory of {computer}
Copy Code code as follows:
SELECT * from ' type ' WHERE ' FID ' =2
2. Find all sub categories of {computer}
Copy Code code as follows:
Function sel ($FID) {
$sql = "SELECT * from ' type ' WHERE ' fid ' = $fid";
$rs =mysql_query ($sql);
for ($i = 0; $i < count ($rs); $i + +) {
echo $rs [$i] [' name '];
SEL ($rs [$i] [' id ']);//recursion
}
}
SEL (2);
Iv. Practical Data applications
Add a field ' Tid ' to the datasheet, and the field value is the ID of the category ' type ' table to which the record belongs. Must be ID cannot be name, because the value of name may change.
For example, to query items belonging to the {computer} category
Copy Code code as follows:
SELECT * from ' goods ' WHERE ' tid ' =2
Note: The code is not running and there may be errors, but the idea is correct and the main thing is to understand the tree structure, not to remember the code.