In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of sample analysis based on php infinite classification. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Infinite classification is a kind of data structure which is often used in practical development, which is generally called tree structure.
Title: similar to Taobao commodity classification, you can set its subcategory in any classification.
First, create a `type` data table
`id` self-growth
`fid` int (11) default (0), parent node id
`name` varchar (50), category name
The copy 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`)
)
II. Add
Let's add a few top-level categories first.
The copy code is as follows:
INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0mm,' mobile phone')
INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0mm,' computer')
INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0mm,' shoes')
INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0mm,' clothes')
Here fid=0 represents the top-level category
Then we add a few subcategories to {computer}.
The copy code is as follows:
INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '2percent,' desktop'), (NULL, '2percent,' notebook')
Here the id of fid=2,2 is the id of category {computer}, and if it is a subcategory of adding {shoes}, then fid=3
Similarly, if we add subcategories to {notebook}, then fid=6
The copy code is as follows:
INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '6percent,' ausu'), (NULL, '6percent,' hp')
3. Delete
If we want to delete the category {notebook}, it's very simple.
The copy code is as follows:
DELETE FROM `type`WHERE `id` = 6
We should also remember to deal with the subcategories of {notebook}.
The copy code is as follows:
Function del ($fid) {
$sql= "SELECT * FROM `type` WHERE `fid` = $fid"
$rs=mysql_query ($sql)
For ($I = 0; $I < count ($rs); $iTunes +) {
$sql= "DELETE FROM `type` WHERE `id` = {$rs [$I] ['id']}"
Mysql_query ($sql)
Del ($rs ['id']); / / Recursive
}
}
Del (6); / / perform the operation
Here you may wonder why it is so troublesome to use recursion instead of deleting it like this.
The copy code is as follows:
DELETE FROM `type`WHERE `fid` = 6
So we can not delete {ausu}, {hp} directly? But suppose {ausu} has a subcategory {A1}, and {A1} also has a subcategory {a2}, and we can't delete the data completely without recursion.
Third, find
1. Find subcategories of {computer}
The copy code is as follows:
SELECT * FROM `type` WHERE `fid` = 2
two。 Find all subcategories of {computer}
The copy code is as follows:
Function sel ($fid) {
$sql= "SELECT * FROM `type` WHERE `fid` = $fid"
$rs=mysql_query ($sql)
For ($I = 0; $I < count ($rs); $iTunes +) {
Echo $rs [$I] ['name']
Sel ($rs [$I] ['id']); / / Recursive
}
}
Sel (2)
IV. Practical data application
Add a field `tid` to the data table. The value of the field is the id of the category `type` table to which the record belongs. Must be id, not name, because the value of name may change.
For example, query the goods that belong to {computer} category.
The copy code is as follows:
SELECT * FROM `goods` WHERE `tid` = 2
Note: there may be errors if the code has not been run, but the idea is correct, the main thing is to understand the tree structure, not to remember the code.
Thank you for reading! This is the end of this article on "sample Analysis based on php Infinite Classification". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.