簡體   English   中英

從會話中的數組中取消設置項目

[英]Unsetting an item from an array in a session

我試圖從存儲的會話中刪除我的數組中的項目。 我嘗試了以下但是它反轉並且實際上增加了更多!

所以這是我的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"); 

嘗試這樣的事情:

$product_id = $_POST["id"];
foreach($_SESSION['products'] as $key=>$product) {
   if($product['id'] == $product_id) {
      unset($_SESSION['products'][$key]);
      break;
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM