从 MySQL 获取项目数组
Get array of items from MySQL
我正在尝试从 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.'
');
}
什么也没有出现。我知道,当我在尝试仅从 1 个制造商处检索产品时使用 Db::getInstance->getValue in $id_category_default 时,一切正常。当我为 $items 添加测试数组时,例如 (1, 2, 3, 4, 5),产品也会从这个制造商 ID 中显示出来。但是,当我尝试获取 id_manufacturer 的数组并显示基于该数组的产品时,什么也没有显示。有没有其他方法可以为 id_manufacturers 创建数组?
数据库结构是这样的
+------+-------+--------------------------------------------+
| id_product | id_manufacturer | content |
+------+-------+--------------------------------------------+
| 1 | 1 | ... |
| 2 | 1 | ... |
| 3 | 2 | ... |
| 4 | 3 | ... |
+------+-------+--------------------------------------------+
我需要一组 id_manufacturer 个 ID,比如 (1, 2, 3)
试试这个代码:
$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
);
确保使用正确的别名,有时在您的查询中检查 ``
是值得的。让我知道这是否适合你。
我正在尝试从 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.'
');
}
什么也没有出现。我知道,当我在尝试仅从 1 个制造商处检索产品时使用 Db::getInstance->getValue in $id_category_default 时,一切正常。当我为 $items 添加测试数组时,例如 (1, 2, 3, 4, 5),产品也会从这个制造商 ID 中显示出来。但是,当我尝试获取 id_manufacturer 的数组并显示基于该数组的产品时,什么也没有显示。有没有其他方法可以为 id_manufacturers 创建数组?
数据库结构是这样的
+------+-------+--------------------------------------------+
| id_product | id_manufacturer | content |
+------+-------+--------------------------------------------+
| 1 | 1 | ... |
| 2 | 1 | ... |
| 3 | 2 | ... |
| 4 | 3 | ... |
+------+-------+--------------------------------------------+
我需要一组 id_manufacturer 个 ID,比如 (1, 2, 3)
试试这个代码:
$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
);
确保使用正确的别名,有时在您的查询中检查 ``
是值得的。让我知道这是否适合你。