因此,我有一个购物车系统,需要更新特定会话变量的数量。现在,如果我有一个ID = 1的商品
并添加另一个ID = 1的商品,它将数量更新为2
,这很好。但是,如果我添加一个ID = 2的项目,
然后我再次添加另一个id = 1的项目,它将id = 2的数量更新为2
什么时候应该将id的数量更新为1到3
这是我的代码:
$exists = false;
foreach ($_SESSION['cart'] as $key => $item) {
if ($item['product_id'] == $part_id) {
$exists = true;
}
}
if ($exists == true) {
$_SESSION["cart"][$key]['quantity']++;
}
else{
$_SESSION['cart'][] = array(
'product_id' => $part_id,
'title' => $title,
'price' => $price,
'default_img' => $default_img,
'quantity' => $quantity);
}
婷婷同学_