我创建了一个网站,我将类别放在三个级别中 主要类别 - >子类别 - >子子类别
此类别我用于添加产品,无论我从列表中选择哪个类别,每个产品都进入该类别。现在我试图在我的网站上显示产品类别明智我已经放了一个列表按钮,我已经显示所有的类别和子类别现在的问题是,当我要点击主类别它只显示那些产品选择了该类别但我想在主要类别点击时显示子类别,这里是查询:
SELECT * FROM `products` WHERE `product_category`='$_REQUEST[cat]' ORDER BY `product_id` DESC
在此查询中,请求cat是我在页面上获得的类别的名称。
如果点击了主要类别,我想知道显示子类别的查询
答案 0 :(得分:0)
假设您与product_category
有某些关系,例如parent_category
点击主要类别id=1
时,您还应该搜索它的子项
主要类别
SELECT * FROM `products` WHERE `product_category`='$_REQUEST[cat]' ORDER BY `product_id` DESC
子类别
SELECT * FROM `products` WHERE `category_parent`='$_REQUEST[cat]' ORDER BY `product_id` DESC
也要注意你的$_REQUEST['cat']
,因为它很脆弱,可以被利用。您应该使用mysql_real_escape_string()
或仅(int) ($_REQUEST['cat'])
答案 1 :(得分:0)
SELECT product_category, (SELECT category_name FROM category WHERE c_id = products.c_id) AS Category,
(SELECT sub_category_name FROM sub_category WHERE sub_cat_id = products.sub_cat_id) AS 'Sub Category'
(SELECT sub_sub_category_name FROM sub_sub_category WHERE sub_sub_cat_id = products.sub_sub_cat_id) AS 'Sub sub Category'
FROM `products` WHERE product_category='1'
store your sub categories and sub sub categories in other tables and in product table just store their id's and then utilize the above query for the product with cate name and sub name
and for sub categories of your relevant category you can select from sub cate table
SELECT * FROM subcategory where category_id='$_REQUEST[cat]'