1

我创建了一个网站,我将类别分为三个级别,例如主类别 -> 子类别 -> 子子类别

我在添加产品时使用了这个类别,无论我从每个产品进入该类别的列表中选择什么类别。现在我正在尝试在我的网站上明智地显示产品类别,我已经放置了一个列表按钮,我已经在其中显示了所有类别和子类别现在问题是当我要单击主类别时它只显示那些产品选择了该类别,但如果单击主类别,我也想显示子类别,这是查询:

SELECT * FROM `products` WHERE `product_category`='$_REQUEST[cat]' ORDER BY `product_id` DESC

在此查询中,请求 cat 是我在页面上获得的类别的名称。

如果单击主类别,我也想知道显示子类别的查询

4

2 回答 2

0

假设您在单击主类别时与product_categorylike有一些关系,您还应该搜索它的子类别parent_categoryid=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'])

于 2012-04-24T07:47:44.057 回答
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]'
于 2012-04-24T07:51:06.613 回答