我正在嘗試從 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 關注
- 89 瀏覽
添加回答
舉報
0/150
提交
取消