The first phase of the project is coming to an end. I will share a callback function for the definition of infinite classification (to be honest, it was written at that time as well. Now let's look at the more objective, O (objective _ callback) O ~)
// Project requirement: search for a product on the home page
// Practice: Write a drop-down list. The list items are nine categorynames in the following table... I want to select any category to search for the items in the item table (including the sub-classes below ).
// Thought process: At first, I thought, where path like. % 0-1% ....... and so on, but later I found that there was a big problem during paging, because at that time, I uploaded a product without uploading the product in the id of a subclass (there will be a free loop when querying the product in a loop-when there is a subclass in this category ).
The more chaotic it is, the more out the table ,,,
Data Structure of the category table:
+ ---- + ----- + ------- + -------------- +
| Id | pid | path | categoryname |
+ ---- + ----- + ------- + -------------- +
| 1 | 0 | 0 | all items 1
| 2 | 1 | 0-1 | domestic product 2
| 4 | 2 | 0-1-2 | domestic goods 4
| 5 | 2 | 0-1-2 | domestic goods 5
| 3 | 1 | 0-1 | imported goods 3
| 6 | 3 | 0-1-3 | import goods 6
| 7 | 3 | 0-1-3 | import goods 7
| 8 | 1 | 0-1 | test Class 8
| 9 | 1 | 0-1 | grocery Area 9
+ ---- + ----- + ------- + -------------- +
It is much better to look at the table. My idea is that if I select "all items" in the drop-down list ", so as long as all the sub-classes of all commodities should be obtained by me (and I do not want to get the "state-owned goods" or "imported goods" as shown in the table above ", because they have sub-classes ). Here, I don't seem to have to worry about writing a callback .~~~~ (>_< )~~~~ , It seems that,, forget, don't give up halfway. It took me more than half an hour to suppress this word... Σ
<? Php
Mysql_connect ('localhost', 'root', 'admin ');
Mysql_select_db ('dzshop ');
Mysql_query ("set names utf8 ");
// Test
Echo rtrim (list_typeid (1), ','); // output, ^ o ^ at this time, select * from goods where typeid in) it's not a problem to take out all the products.
Echo rtrim (list_typeid (2), ','); // output 4, 5
Echo rtrim (list_typeid (4), ','); // output 4
/*
* Function: list_typeid
* Function: gets the specified category and all category. id (category. id = goods. typeid) without subclasses under the specified typeid (that is, the id of the category table)
* $ Id: id to be queried
* $ Category_name: name of the category table
*/
Function list_typeid ($ id, $ category_name = 'category '){
$ SQL = "select B. id, B. pid from {$ category_name} a, {$ category_name} B where. id = {$ id} and B. pid = {$ id }";
$ Result = mysql_query ($ SQL );
If (mysql_affected_rows ()> 0) {// if the number of affected rows is greater than 0, a subclass exists in the category. Go in and continue.
While ($ arr = mysql_fetch_assoc ($ result )){
$ List_id. = list_typeid ($ arr ['id']);
}
} Else {// The row is not affected, indicating that there is no subclass under this subclass, And the id is taken out.
Return $ id. ','; // facilitates the writing of SQL statements... WHERE ID IN ($ id )....
}
Return $ list_id;
}
Author: zdrjlamp