The first project phase is coming to an end. Let me share a custom callback function for infinite categories (to be honest, I was confused when I wrote it, and I am even more confused now when I read it, O(∩_∩)O~)
//Project requirements: I want to do a product search on the homepage
//How to do it: Write a drop-down list, and the list items are the 9 categorynames in the table below. . I want to select any category and search for the products of that category in the product table (including the subcategories below) at one time.
//Thought process: At first I also thought,,,,where path like .%0-1%...., but later I found that I encountered a big problem when paging, because at that time I The uploaded product does not upload the product in the ID that has a subcategory (when looping to query the product, there will be an empty loop - when the category has subcategories).
It’s getting more and more confusing as you talk, so let’s just show it with your watch...
Category table data structure diagram:
+----+-----+-------+-------------+
| id | pid | path | categoryname |
+----+-----+-------+-------------+
| 1 | 0 | 0 | All products 1
| 2 | 1 | 0-1 | Domestic products 2
| 4 | 2 | 0-1-2 | Domestic parallel imports 4
| 5 | 2 | 0-1-2 | Domestic licensed products 5
| 3 | 1 | 0-1 | Imported goods 3
| 6 | 3 | 0-1-3 | Imported parallel imports 6
| 7 | 3 | 0-1-3 | Imported goods 7
| 8 | 1 | 0-1 | Test class 8
| 9 | 1 | 0-1 | Grocery Section 9
+----+-----+-------+-------------+
It's much better when I look at the table. My idea is that if I select "All Products" in the drop-down list, then I should get all the subcategories belonging to all products (and I don't want to get the above "State-owned goods" and "imported goods" in the table, because there are subcategories below them). As I write this, I seem to think that I don’t need to go to such trouble to write a callback. ~~~~(>_<)~~~~ ,,,, it seems, it seems,,,, forget it,,, let’s not give up halfway, it took me more than half an hour to hold back these words. . . ∑
mysql_connect('localhost','root','admin');
mysql_select_db('dzshop');
mysql_query("set names utf8");
//Test
echo rtrim( list_typeid(1), ',' ); //Output 4,5,6,7,8,9 ^o^ At this time, you can select * from goods where typeid in(4,5,6,7,8 ,9) Taking out all the goods is no longer a problem.
echo rtrim( list_typeid(2), ',' ); //Output 4,5
echo rtrim( list_typeid(4), ',' ); //Output 4
/*
* Function: list_typeid
* Function: Get a given category and get all category.id without subcategories under the specified typeid (that is, the id of the category table) (category.id==goods.typeid)
* $id: the id to be queried
* $category_name: Table name of the classification table
*/
function list_typeid($id,$category_name='category'){
$sql="select b.id,b.pid from {$category_name} a,{$category_name} b where a.id={$id} and b.pid={$id}";
$result=mysql_query($sql);
if(mysql_affected_rows()>0){ //The number of affected rows is greater than 0, which means there are subcategories under this category, go in and continue execution
while( $arr = mysql_fetch_assoc($result) ){
$list_id.=list_typeid($arr['id']);
}
}else{ //There is no affected row, which means there are no subclasses under this subclass, so take out this id
return $id.','; //Convenient to write SQL statements .... WHERE ID IN($id) ....
}
return $list_id;
}