【问题标题】:display the subcategory data also on submit main category query in php在 php 中提交主类别查询时也显示子类别数据
【发布时间】:2012-05-04 20:07:59
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: php mysql categories


    【解决方案1】:

    假设您与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'])

    【讨论】:

      【解决方案2】:
      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]'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多