简体   繁体   English

从php的会话数组中删除购物车项目

[英]Deleting cart items from session array in php

I have created a session array for items to go in a cart with its different properties. 我创建了一个会话数组,用于将商品放入具有其不同属性的购物车中。 Each item has a remove button which deletes that item only. 每个项目都有一个删除按钮,该按钮仅删除该项目。

FOR CREATING SESSION I HAVE USED 我曾经使用过创建会话

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']);
}

FOR DISPLAYING ARRAY 用于显示阵列

 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 removefromcart.php页面

<?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";
}
?>

Whenever I click on remove button page refreshes due to header but nothing deleted. 每当我单击“删除”按钮时,页面都会由于标题而刷新,但没有删除任何内容。 What should I do Please HELP I really trust Stack overflow for helping noob coders. 我该怎么办请帮助我真的相信Stack溢出可以帮助菜鸟编码人员。

AFTER THAT I USED below code to create array 在使用下面的代码创建数组之后

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']);
}

and getting 并得到

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 ) )

REMOVE BUTTON 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>

Array first key is 0 which creates a problem for me. 数组的第一个键是0,这给我带来了问题。 In my cart first item always remains although its remove button url id has different id. 在我的购物车中,第一个项目始终保留,尽管其“删除”按钮的URL ID具有不同的ID。

Your delete isn't working, because you're not using that $id as your index when you build the cart: 您的删除操作无效,因为在构建购物车时您没有将$ id用作索引:

$_SESSION['cart'][] = $cart;
                  ^--generate new ID based on current length of array

So if you have 3 items in your array, and add another one, its new index will be 3 , even if its $id happens to be 9999 or whatever. 因此,如果您的数组中有3个项目,并添加另一个,则其新索引将为3 ,即使其$ id恰好是9999或其他。

You need to use that $id as the index everywhere: 您需要在各处使用该$ id作为索引:

$_SESSION['cart'][$id] = $cart;
                  ^^^---

and then your unset() will work as expected. 然后您的unset()将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM