[英]How do I list all the products in the sub-categories for the top categories
My database category and product are: 我的数据库类别和产品是:
Category Table
: id,name,parent_id Category Table
:id,名称,parent_id
Product Table
: id,name,category_id Product Table
:ID,名称,类别ID
My Category menu is: 我的类别菜单是:
-Category A
--Category A1
--Category A2
---Category A2a etc..
Each product has only one category related: 每个产品只有一个相关类别:
Product1 -> Category A1
Product2 -> Category A2
Product3 -> Category A2a etc..
The code of all of the product category and subcategory 所有产品类别和子类别的代码
category.php?category_id=1 (exp. category A)
<?php
function FetchAllRows($sql) {
global $db;
$stmt=$db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
function getCategoryChild($children) {
$sql = "SELECT * FROM `category` WHERE `parent_id` = $children";
$rows = FetchAllRows($sql);
$sub_category_id = array();
foreach($rows as $row) {
// $sub_category_id[$row['id']] = getCategoryChild($row['id']);
$cat = getCategoryChild($row['id']);
$sub_category_id[] = array(
'children' => $cat,
'category_id' => $row['id']
);
}
return $sub_category_id;
}
$a = getCategoryChild($category_id);
echo $in = join(',', array_fill(0, count($a), '?'));
$query="SELECT * FROM product WHERE category_id IN ($in)";
I tried this latest application. 我尝试了这个最新的应用程序。 We did not get results.
我们没有得到结果。 My English is not good.
我英文不太好。 Hopefully presence.
希望存在。 Until the array elements in I want to transfer into SQL.
直到我要转移到SQL中的数组元素为止。
I solved the problem. 我解决了问题。
I delete these two lines. 我删除这两行。
echo $in = join(',', array_fill(0, count($a), '?'));
$query="SELECT * FROM product WHERE category_id IN ($in)";
I invested only category_id 's. 我只投资了category_id。
foreach($a as $index => $b) {
$subs[$index] = $b['category_id'];
}
$ids = implode($subs,',');
$query = $db->query("SELECT * FROM `product` WHERE category_id IN($ids);");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.