Ankit Sharma Ankit Sharma - 6 months ago 34
PHP Question

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

<?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.

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. In my cart first item always remains although its remove button url id has different id.

Answer

Your delete isn't working, because you're not using that $id as your index when you build the cart:

$_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.

You need to use that $id as the index everywhere:

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

and then your unset() will work as expected.

Comments