简体   繁体   English

从嵌套集和联接产品表中获取所有子类别

[英]Get all child categories from nested set AND join products table

I want to retrieve all child categories with a given parent category AND I want to join my products table to that result. 我想检索具有给定父类别的所有子类别,并且希望将我的产品表加入该结果。 The result should be a breadcrumb navigation where a user could click on a parent category and retrieves all products in that category with the child categories. 结果应该是面包屑导航,其中用户可以单击父类别并检索该子类别中的所有产品。

Breadcrumb looks like that: Electronic >> Music >> MP3-Player >> Flash If a user clicks on "Music" the result should contain all products in the "Music" "MP3-Player" and "Flash" categories. 面包屑看起来像: Electronic >> Music >> MP3-Player >> Flash如果用户单击“音乐”,则结果应包含“音乐”,“ MP3播放器”和“ Flash”类别中的所有产品。

I use a nested set but I find it really difficult to handle it with complicated queries. 我使用了一个嵌套集,但发现使用复杂的查询来处理它确实很困难。

My database looks like that: 我的数据库如下所示:

category_table: ID / Name / lft / rgt products_table: ID / Title / description / pic / category_id / ... category_table:ID /名称/ ltf / rgt products_table:ID /标题/描述/图片/ category_id / ...

My query looks now like this: 我的查询现在看起来像这样:

    $result=mysql_query('SELECT *, node.id, node.name
FROM category AS node,
        category AS parent,
        category AS sub_parent,
        (
                SELECT node.name
                FROM category AS node,
                category AS parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                AND node.id = 23
                GROUP BY node.name
                ORDER BY node.lft
        )AS sub_tree JOIN products ON products.id = id
WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
        AND sub_parent.name = sub_tree.name
GROUP BY node.name
ORDER BY node.lft;') or die(mysql_error());

But this gives me the error "#1104 - The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay". 但这给了我错误“#1104-SELECT将检查超过MAX_JOIN_SIZE行;检查您的WHERE并使用SET SQL_BIG_SELECTS = 1或SET MAX_JOIN_SIZE =#(如果SELECT可以的话)”。 I don't get it. 我不明白

Try this PHP script. 试试这个PHP脚本。 It may have a couple syntax errors in it, I haven't tested it yet, but it should do exactly what you want. 它可能有一些语法错误,我还没有测试过,但是它应该可以完全满足您的要求。

Its important to note, you need to add a field to your categories table, called category_parent_id and set its default value to either 0 or null . 需要注意的重要一点是,您需要在categories表中添加一个名为category_parent_id的字段,并将其默认值设置为0null

class breadcrumbNav
{
    private $dataArray = array();
    private $breadcrumb = array();

    public function __construct()
    {
        $this->getAllRecords();
        return;
    }


    private function getAllRecords()
    {
        $result = $db->prepare("
            SELECT p.ID as product_id, p.title AS product_title, p.description AS product_description, p.pic AS product_picture, 
                   c.ID as category_id, c.Name as category_name, c.lft AS category_lft, c.rgt AS category_rgt, c.parent_id AS category_parent_id 
            FROM products p 
            LEFT JOIN categories c 
                 ON p.category_id = c.ID 
        ");

        $result->execute();

        $this->dataArray = $result->fetchAll(PDO::FETCH_ASSOC);
        return;
    }

    function buildBreadcrumb($recordId, $firstRun = true)
    {
         foreach($this->dataArray as $row)
         {
             if(($row['product_id']==$recordId && $firstRun) || ($row['category_id']==$recordId && $firstRun===false))
             {
                  $this->breadcrumb[] = '<a href="?categoryId=' . $row['category_id'] . '">' . $row['category_name'] . '</a>';

                  if($row['category_parent_id']!==0 && !is_null($row['category_parent_id']))
                  {
                      $this->buildBreadcrumb($this->buildBreadcrumb($row['category_parent_id'], false));
                  }
             }
         }

         return implode(' &gt;&gt; ', $this->breadcrumb;
    }
}



$currentProductId = 'ENTER THE PRODUCT ID HERE';
$breadcrumbObject = new breadcrumbNav();
$breadcrumb = $breadcrumbObject->buildBreadcrumb($currentProductId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从父类别的子类别中选择所有产品 - select all products from child categories in parent category 获取多个类别的所有产品(Woocommerce/Wordpress) - Get all products from multiple categories (Woocommerce/Wordpress) Magento-按品牌获取包含产品的所有类别 - Magento - get all categories containing products by brand Laravel 从父类别及其所有子类别中获取所有产品 - Laravel get all products from parent category and all it's sub-categories 如何使用 Laravel 5.2 获取所有类别(即多对多表上的所有记录)的所有产品的计数 - How to get count for all products for all categories (i.e all records on many-to-many table) with laravel 5.2 获取仅子类别具有产品的类别的类别计数 - Get category count for a category where only the child categories have products 如何在单个查询中获取具有产品的嵌套类别? - How to get those nested categories who have the products - in single query? Yii:类别为MANY_MANY的产品可能具有父项,也可能没有父项。 获取所有产品 - Yii : Products with MANY_MANY categories and categories may or not has parent. Get all products Magento导航中的类别和产品-儿童 - Magento categories and products in navigation - child Laravel显示带有子类别的产品 - Laravel show products with child categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM