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.
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 / ...
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". I don't get it.
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(' >> ', $this->breadcrumb;
}
}
$currentProductId = 'ENTER THE PRODUCT ID HERE';
$breadcrumbObject = new breadcrumbNav();
$breadcrumb = $breadcrumbObject->buildBreadcrumb($currentProductId);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.