Home >Backend Development >PHP Tutorial >Detailed explanation of infinite classification examples in php mysql_PHP tutorial
This article mainly introduces the method of realizing infinite classification in php mysql, and analyzes the mysql database design, database operation and infinite classification with examples. The specific implementation steps are of great practical value. Friends in need can refer to them
The example in this article describes the method of realizing unlimited classification in php mysql. Share it with everyone for your reference. The specific analysis is as follows:
1. The database performs unique indexing by setting the parent class ID, and then uses recursive calls of functions to achieve unlimited classification;
2. The database design is arranged in a specific format, and then uses mysql to query the key function: concat. The program implementation is relatively simple. First, we assume that there is such a three-level classification, News→PHP News→PHP6.0 is out.
If we want to find the news "PHP6.0 is out", we can click on the news first, and then click on the PHP news to find out. In other words, we can go down level by level through the grandfather class. In turn, as long as we know the parent class of a subclass, we can find it. In this way, when designing the database, we can design an additional field for the parent class id to achieve unlimited classification.
The database code is as follows:
Here we create a table "class"
The code is as follows:
The code is as follows:
Then we insert the category 'PHP News' into the database. The id of its parent category 'News' is 1, so its f_id is set to 1.
The code is as follows:
Then we insert the category 'PHP 6.0 is out' into the database. The id of its parent category 'PHP News' is 2, so its f_id is set to 2.
The code is as follows:
Similarly, we can insert categories all the way down, thus reaching infinite categories.
We can find that the key to inserting a category is to find the id of the parent category of this category, and then use it as the value of the f_id field of this category.
Suppose you want to insert the category 'Technology' at the same level as 'News', that is to say it is also the largest category and there is no parent category above it, then its f_id is also set to 0;
Copy the code The code is as follows:
There is another category 'PHP Technology' under 'Technology', so how do we insert it? First find the id of the parent class 'Technology' of 'PHP Technology', and then use it as the value of its own f_id field.
The code is as follows:
Seeing this, everyone should understand how to insert each category into the database, so I won’t give examples. We already know how to insert each category into the database, so how do we list each category?
The code is as follows:
Copy the code The code is as follows:
希望本文所述对大家的php程序设计有所帮助。