In-depth understanding based on PHP unlimited classification_PHP tutorial

WBOY
Release: 2016-07-21 15:10:22
Original
832 people have browsed it

Infinite classification is a data structure often used in actual development, generally we call it a tree structure.
Title design: Similar to Taobao product classification, you can set its subcategories in any category.

1. Create `type` data table
`id` self-increase
`fid` int(11) default (0), parent Node id
`name` varchar(50), category name

Copy code The code is as follows:

CREATE TABLE ` type` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fid` int(11) NOT NULL DEFAULT '0',
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
)

2. Add
Let’s first add a few top-level categories
Copy code The code is as follows:

INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', 'Mobile phone' );
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 represents the top category

Then we add several sub-categories for {computer}
Copy the code The code is as follows:

INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '2', 'Desktop') , (NULL, '2', 'notebook');

Here fid=2, the id 2 is the id of the category {Computer}, if you add a subcategory of {Shoes}, then fid= 3
Similarly, if we add a subcategory for {notebook}, fid=6
Copy the code The code is as follows:

INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '6', 'ausu'), (NULL, '6', 'hp');

3. Delete
If we want to delete the category {Notebook}, it is very simple
Copy the code The code is as follows:

DELETE FROM `type` WHERE `id`=6

We must also remember to handle the subcategory of {notebook} accordingly
Copy code The code is 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']); //Recursion
}
}
del(6);//Execute the operation

Here you may wonder why it is so troublesome to use recursion instead of deleting directly like this
Copy code The code is as follows:

DELETE FROM `type` WHERE `fid`=6

In this way, can’t we delete {ausu} and {hp} directly? But assuming {ausu} has a subcategory {a1}, and {a1} also has a subcategory {a2}, we cannot completely delete the data without recursion.

3. Search
1. Find the subcategory of {Computer}
Copy code The code is as follows:

SELECT * FROM `type` WHERE `fid`=2

2. Find all subcategories of {Computer}
Copy code The code is 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);

4. Actual data application
Add a field `tid` in the data table, and the field value is the id of the `type` table of the category to which the record belongs. It must be id and not name, because the value of name may change.
For example, query the products belonging to the {Computer} category
Copy the code The code is as follows:

SELECT * FROM `goods` WHERE `tid`=2

Note: The code may not be run before, but there may be errors, but the idea is correct. The main thing is to understand the tree structure, and Not memorizing code.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327096.htmlTechArticleInfinite classification is a data structure often used in actual development, generally we call it a tree structure. Title design: Similar to Taobao's product classification, subcategories can be set in any category. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template