PHP Unlimited Classification examples (imitation Taobao product classification)

Source: Internet
Author: User
    1. CREATE TABLE ' type ' (
    2. ' id ' int (one) not NULL auto_increment,
    3. ' FID ' int (one) not NULL DEFAULT ' 0 ',
    4. ' Name ' varchar (not NULL),
    5. PRIMARY KEY (' id ')
    6. )
Copy Code

Second, add several top-level categories to add

    1. INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (NULL, ' 0 ', ' 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 is representative of 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,2 this ID is the ID of the category {computer}, if it is a subcategory that adds {shoes} fid=3 the same as {notebook} to add subcategories fid=6

    1. INSERT into ' type ' (' id ', ' fid ', ' name ') VALUES (null, ' 6 ', ' Ausu '), (null, ' 6 ', ' hp ');
Copy Code

Third, delete if you want to delete the category {notebook}, you can:

    1. DELETE from ' type ' WHERE ' ID ' =6
Copy Code

{Notebook} sub-classification also remember to do the corresponding processing

    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

    1. DELETE from ' type ' WHERE ' FID ' =6
Copy Code

So you can delete {Ausu}, {hp} directly? But suppose {Ausu} has a subcategory {A1},{A1} that also has a subclass {A2}, which cannot be completely deleted without recursion.

Third, find

1. Find subcategories for {computer}

    1. SELECT * from ' type ' WHERE ' FID ' =2
Copy Code

2. Find all sub-categories for {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, 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, query for items that belong to the {computer} Category:

    1. SELECT * from ' goods ' WHERE ' tid ' =2
Copy Code

Let's introduce these and hope to help you.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.