我正在尝试从 MySQL 数据库获取一系列项目,以根据购物车(prestashop)中的制造商 ID 显示特定产品。我做了这个查询 $cart_items = $cartObj->getProducts(); if (is_array($cart_items) && count($cart_items) > 0) { $cart_ids = array(); foreach ($cart_items as $cart_item) { $cart_ids[] = $cart_item['id_product']; } $arrayKeys = array_keys($cart_ids); $cart_ids[$arrayKeys[0]]; $id_manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT id_manufacturer FROM `'._DB_PREFIX_.'product` WHERE id_product IN (' . implode(',', array_map('intval', $cart_ids)) . ') '); $items = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' select id_product from `'._DB_PREFIX_.'product` p where p.id_manufacturer IN (' . implode(',', array_map('intval', $id_manufacturers)) . ') '.(count($excluded_ids) ? ' AND p.id_product NOT IN (' . implode(', ', $excluded_ids) . ')' : '').' group by p.id_product limit '.(int)$limit.' '); }但什么也没有出现。我知道,当我在 $id_category_default 中使用 Db::getInstance->getValue 尝试仅从 1 个制造商检索产品时,一切正常。当我为 $items 添加测试数组时,例如 (1, 2, 3, 4, 5),产品也会从此制造商 ID 中显示。但是当我尝试获取 id_manufacturer 数组并显示基于该数组的产品时 - 什么也没有显示。还有其他方法为 id_manufacturers 创建数组吗?DB结构是这样的+------+-------+--------------------------------------------+| id_product | id_manufacturer | content |+------+-------+--------------------------------------------+| 1 | 1 | ... || 2 | 1 | ... || 3 | 2 | ... || 4 | 3 | ... |+------+-------+--------------------------------------------+我需要一个 id_manufacturer id 数组,例如 (1, 2, 3)
1 回答
眼眸繁星
TA贡献1873条经验 获得超9个赞
试试这个代码:
$manufacturers = [];
foreach ($cart->getProducts() as $product) {
$manufacturers[] = $product['id_manufacturer'];
}
$items = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.`id_product`
FROM `'._DB_PREFIX_.'product` p
WHERE p.`id_manufacturer` IN (' . implode(',', $manufacturers) . ')
'.(count($excluded_ids) ? ' AND p.`id_product` NOT IN (' . implode(', ', $excluded_ids) . ')' : '').'
GROUP BY p.`id_product`
LIMIT '.(int) $limit
);
确保使用正确的别名,有时值得``在查询中检查。让我知道这是否适合您。
- 1 回答
- 0 关注
- 69 浏览
添加回答
举报
0/150
提交
取消