我想检查product_id
购物车表中是否存在以及是否存在定义变量$class = 'show'
这是我的代码:
$article = $db->query("
SELECT
b.price as price,
b.shop_id as shop_id,
c.image as image,
c.name as name,
a.id as id
FROM products AS a
INNER JOIN prices AS b ON a.ean = b.sku
INNER JOIN shops as c on b.shop_id = c.id
WHERE a.id = '$product_id'
");
while ($data = $article->fetch_object()) {
$price = $data->price;
$price2 = $data->price;
$price = number_format($price, 2, ',', '');
$shop_id = $data->shop_id;
$logo = $data->image;
$name = $data->name;
//some html code here
}
现在我不知道如何检查购物车表中是否存在该产品。在两个表中,uniqe是product_id
我会LEFT JOIN
为此目的使用a (请注意本CASE WHEN .. THEN ..
节)。它与购物车表联接,并且exists_in_cart
如果购物车中存在产品,则选择一个名为1的列,否则选择0:
$article = $db->query("
SELECT
b.price as price,
b.shop_id as shop_id,
c.image as image,
c.name as name,
a.id as id,
CASE WHEN cart.product_id IS NULL THEN 0 ELSE 1 END as exists_in_cart
FROM products AS a
INNER JOIN prices AS b ON a.ean = b.sku
INNER JOIN shops as c on b.shop_id = c.id
LEFT JOIN cart on a.id = cart.product_id
WHERE a.id = '$product_id'
");
如果购物车中存在产品,则为1,否则为0:
$productInCart = $data->exists_in_cart == 1 ? TRUE : FALSE;
if ($productInCart) {
$class = 'show';
}
else {
// ...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句