I am trying to display a product on each category starting from the top parent category all the way down to the category. I have my db setup like this:
CATEGORY TABLE:
id | parent_id | root_cat | name
5 37 0 bulbs
45 5 0 standard auxiliary
289 45 5 standard
297 289 5 5W
Now this is the products table table:
id | name | cat
470 bulb 5w 297
And this is happening, when i click on the bulbs category i see the product just fine, but when i click in the sub category standard auxiliary i do not see the product. But if i go another level to standard i can see the product again and also if i click on the 5W cat i also see the product. So the problem is only on the standard auxiliary category.
This is the method i am using to retrieve the data: $table_2 is the products table and $table is the category table.
public function getProductsWhereParentIdis($cat) {
$check = $this->checkCategoryChildren($cat);
if($check) {
$query = "
SELECT *
FROM categories
INNER JOIN products
ON products.category = categories.id
WHERE categories.id = '".$this->db->escape($cat)."'
OR categories.parent_id = '".$this->db->escape($cat)."'
OR categories.root_category = '".$this->db->escape($cat)."'";
return $this->db->fetchAll($query);
} else {
$query = "SELECT * FROM {$this->table_2} WHERE category = '".$this->db->escape($cat)."'";
return $this->db->fetchAll($query);
}
}
and this method is just to check if a product has a parent:
public function checkCategoryChildren($id = null) {
if(!empty($id)) {
$query = "SELECT * FROM {$this->table} WHERE parent_id = '".$this->db->escape($id)."' ORDER BY view_order ASC";
return $this->db->fetchAll($query);
}
}
Really looking forward to anyones suggestion as to why i can see the product in all the categories but one, thank you.
from SQL query not displaying the product in each sub category
No comments:
Post a Comment