我正在尝试从存储的会话中删除数组中的项目。我尝试了以下但是它反转并且实际上增加了更多!
所以这是我的foreach产品在购物车中显示的一个按钮,从购物车中删除:
foreach ($_SESSION['products'] as $product) {
$name = $product['name'];
$id = $product['id'];
$price = $product['price'];
$img = $product['img'];
$sku = $product['sku'];
$description = $product['description'];
echo '<a href="single_product.php?product_id=' . $product['products'] . '">';
echo "<img src='$img'><br />";
echo "Product: $name<br />";
echo "Price: $price | ID: $id<br />";
echo "$description";
echo '</a><br /><br />';
echo '<form action="removeItem.php" method="post">
<input type="hidden" name="product_id" value="' . $product['id'] . '" />
<button name="removeItem">Remove</button>
</form>';
$sum += $price;
}
这是删除它的表单,但是当你点击删除时它实际上会增加更多:
$_SESSION['products'][] = $itemid;
$id = $_POST['id'];
unset($_SESSION['products'][$id]);
header("location:basket.php");
答案 0 :(得分:2)
尝试这样的事情:
$product_id = $_POST["id"];
foreach($_SESSION['products'] as $key=>$product) {
if($product['id'] == $product_id) {
unset($_SESSION['products'][$key]);
break;
}
}