0

我想检索具有给定父类别的所有子类别,并且我想将我的产品表加入到该结果中。结果应该是一个面包屑导航,用户可以在其中单击父类别并检索该类别中的所有产品及其子类别。

Breadcrumb 看起来像这样:Electronic >> Music >> MP3-Player >> Flash 如果用户点击“音乐”,结果应该包含“音乐”、“MP3 播放器”和“Flash”类别中的所有产品。

我使用嵌套集,但我发现用复杂的查询处理它真的很困难。

我的数据库如下所示:

category_table:ID / 名称 / lft / rgt products_table:ID / Title / description / pic / category_id / ...

我的查询现在看起来像这样:

    $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());

但这给了我错误“#1104 - SELECT 将检查超过 MAX_JOIN_SIZE 行;检查您的 WHERE 并使用 SET SQL_BIG_SELECTS=1 或 SET MAX_JOIN_SIZE=# 如果 SELECT 没问题”。我不明白。

4

1 回答 1

0

Try this PHP script. 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.

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);
于 2013-06-01T13:17:49.523 回答