我已經創建了一個會話數組,用於購物車使用其不同的屬性。每個項目都有一個刪除按鈕,僅刪除該項目。從php中的會話數組中刪除購物車中的商品
創建會議我都用過
session_start();
if (isset($_POST['submit']))
{
$front = $_POST['front_select'];
$back = $_POST['back_select'];
$side = $_POST['side_select'];
$oid = $_POST['orderID'];
$pid = $_POST['pid'];
$cart = array (
'oid' => $oid,
'front' => $front,
'back' => $back,
'pid' => $pid,
'side' => $side
);
$_SESSION['cart'][] = $cart;
print_r($_SESSION['cart']);
}
於顯示陣列
if(isset($_SESSION['cart'])){
$i = 0;
foreach ($_SESSION['cart'] as $item) {
if(isset($item['oid'])){
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo $i;?>
<h5>ORDER ID : <b id="oid">#<?php echo $item['oid']; ?></b> | ORDER TYPE : <b id="product_type">BLOUSE</b> | PRODUCT ID : <b id="product_id"><?php echo $item['pid']; ?></b></h5>
<hr>
</div>
<a id="remove_btn" href="removefromcart.php?id=<?php echo $i; ?>" class="btn btn-danger waves-effect waves-light">Remove</a>
}
}
}
removefromcart.php PAGE
<?php
session_start();
if (isset($_GET['id'])){
$id = $_GET['id'];
unset($_SESSION['cart'][$id]);
unset($cart[$id]);
$_SESSION["cart"] = array_values($_SESSION["cart"]);
header('Location: cart.php');
echo $id ;
}
else
{
echo "ID NOT FOUND";
}
?>
每當我點擊刪除按鈕,頁面刷新,由於頭,但沒有刪除。 我該怎麼辦請幫助我真的相信堆棧溢出幫助noob編碼器。我用下面的代碼來創建陣列
if (isset($_POST['submit']))
{
$front = $_POST['front_select'];
$back = $_POST['back_select'];
$side = $_POST['side_select'];
$oid = $_POST['orderID'];
$pid = $_POST['pid'];
$cart = array (
'oid' => $oid,
'front' => $front,
'back' => $back,
'pid' => $pid,
'side' => $side
);
$_SESSION['oid'] = $oid;
$_SESSION['cart'][$oid] = $cart;
print_r($_SESSION['cart']);
}
和獲取
Array ([0] => Array ([oid] => IT574deb7ddbfe3 [front] => IT1_front_6 [back] => IT1_back_2 [pid] => IT1 [side] => IT1_side_7) [IT574ded04c8af2] => Array ([oid] => IT574ded04c8af2 [front] => IT1_front_6 [back] => IT1_back_3 [pid] => IT1 [side] => IT1_side_2))
刪除按鈕CODE
<div class="row">
<a id="remove_btn" href="removefromcart.php?id=<?php echo $item['oid']; ?>" class="btn btn-danger waves-effect waves-light">Remove</a>
</div>
陣列第一密鑰
AFTER爲0這對我來說造成了一個問題。在我的購物車中,第一個項目始終保持不變,但其刪除按鈕URL ID具有不同的ID
*簡而言之... * => http://php.net/manual/en/function.error-reporting.php –
抱歉沒有找到你 –
你的「FOR DISPLAYING ARRAY」代碼是不正確的混合PHP和HTML。 –