简体   繁体   中英

Removing item from shopping cart

I have a shopping cart function on my website and I am able to add items to the shopping cart and I can remove all items except the first item put into the cart. When I hit "remove" the page reloads but the item is still in the cart. Any other item added to the cart will be removed except that first one. Here's my add to cart code:

<?php
session_start();

if(isset($_GET['id']) & !empty($_GET['id'])){
    if(isset($_SESSION['cart']) & !empty($_SESSION['cart'])){

        $items = $_SESSION['cart'];
        $cartitems = explode(",", $items);
        if(in_array($_GET['id'], $cartitems)){
            header('location: cartIndex.php?status=incart');
        }else{
            $items .= "," . $_GET['id'];
            $_SESSION['cart'] = $items;
            header('location: cartIndex.php?status=success');

        }

    }else{
        $items = $_GET['id'];
        $_SESSION['cart'] = $items;
        header('location: cartIndex.php?status=success');
    }

}else{
    header('location: cartIndex.php?status=failed');
}
?>

And here is my remove from cart code:

<?php 
session_start();
$items = $_SESSION['cart'];
$cartitems = explode(",", $items);
if(isset($_GET['remove']) & !empty($_GET['remove'])){
    $delitem = $_GET['remove'];
    unset($cartitems[$delitem]);
    $itemids = implode(",", $cartitems);
    $_SESSION['cart'] = $itemids;
}
header('location:cart.php')
?>

As people in the comments have pointed out, there are better ways to manage carts. Typically, websites store carts on their servers database through services like MySQL or MongoDB and then do XHR/AJAX calls to update them either when a product is added or removed from the cart. But this is neither here nor there, you want your specific code fixing so I'll help with that.


The problem you're most likely having (I say most likely because its hard to tell when it could be a variety of other things to do with the GET values themselves) is with this line:

unset($cartitems[$delitem]);

What this is doing is searching in the array for item $delitem as the key, and not for the value which is what you want to do. My guess is the ID of the $delitem you're trying to delete is equal to 1 right? Well arrays start from 0 which means it's deleting the item in the second spot, and not the item that has an ID matching $delitem .

The code I've added is:

if (($key = array_search($delitem, $cartitems)) !== false) {
    unset($cartitems[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset() . It will return FALSE on failure, however it can return a false-y value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.

The if() statement will check whether array_search() returned a value, and will only perform an action if it did.

Complete new code:

<?php 
    session_start();

    $items = $_SESSION['cart'];
    $cartitems = explode(",", $items);

    if(isset($_GET['remove']) & !empty($_GET['remove'])){

        $delitem = $_GET['remove'];

        if (($key = array_search($delitem, $cartitems)) !== false) {
            unset($cartitems[$key]);
        }

        $itemids = implode(",", $cartitems);
        $_SESSION['cart'] = $itemids;
    }

    header('location:cart.php');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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