Example of unlimited classification in PHP (imitation Taobao product classification)

WBOY
Release: 2016-07-25 08:58:43
Original
1486 people have browsed it
  1. CREATE TABLE `type` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `fid` int(11) NOT NULL DEFAULT '0',
  4. `name` varchar(50) NOT NULL,
  5. PRIMARY KEY (`id`)
  6. )
Copy code

2. Add Add several top-level categories

  1. INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', 'mobile phone');
  2. INSERT INTO `type` (`id`, `fid `, `name`) VALUES (NULL, '0', 'computer');
  3. INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', 'shoes') ;
  4. INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', 'Clothes');
Copy code

Here fid=0 represents the top category

Then add several subcategories for {computer}

  1. INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '2', 'Desktop'), (NULL, '2', 'Notebook');
Copy code

Here fid=2, the id 2 is the id of the category {Computer}, if you add the subcategory of {Shoes}, then fid=3 In the same way, if you add a subcategory to {notebook}, fid=6

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

3. Delete If you want to delete the category {Notebook}, you can do this:

  1. DELETE FROM `type` WHERE `id`=6
Copy code

{Notebook} subcategories must also be processed accordingly.

  1. function del($fid) {

  2. $sql="SELECT * FROM `type` WHERE `fid`=$fid";
  3. $rs=mysql_query($sql);
  4. for ($i = 0; $i < count($rs); $i++) {
  5. $sql="DELETE FROM `type` WHERE `id`={$rs[$i]['id']}" ;
  6. mysql_query($sql);
  7. del($rs['id']);//Recursion
  8. }
  9. }

  10. del(6);//Perform operation

Copy code

Why is it so troublesome to use recursion instead of deleting it directly like this?

  1. DELETE FROM `type` WHERE `fid`=6
Copy the code

So you can directly delete {ausu}, {hp}? But assuming {ausu} has a subcategory {a1}, and {a1} also has a subcategory {a2}, the data cannot be completely deleted without recursion.

3. Search

1. Find subcategories of {Computer}

  1. SELECT * FROM `type` WHERE `fid`=2
Copy code

2. Find all subcategories of {Computer}

  1. function sel($fid) {

  2. $sql="SELECT * FROM `type` WHERE `fid`=$fid";
  3. $rs=mysql_query($sql);
  4. for ($i = 0; $i < count($rs); $i++) {
  5. echo $rs[$i]['name'];
  6. sel($rs[$i]['id'] );//Recursion
  7. }
  8. }

  9. sel(2);

Copy code

IV. Practical application Add a field `tid` to the data table, the field value is the id of the `type` table to which the record belongs. It must be id and not name, because the value of name may change.

For example, query products belonging to the {Computer} category:

  1. SELECT * FROM `goods` WHERE `tid`=2
Copy code

That’s it, I hope it will be helpful to everyone.



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