[英]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
的字段,并将其默认值设置为0
或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(' >> ', $this->breadcrumb;
}
}
$currentProductId = 'ENTER THE PRODUCT ID HERE';
$breadcrumbObject = new breadcrumbNav();
$breadcrumb = $breadcrumbObject->buildBreadcrumb($currentProductId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.