Question

I have a shopping cart set up on a website which uses a file called cart.php to do all the work. The 'add' function works perfectly, and I am also able to empty the cart, BUT not delete a single item

The delete link looks like this :

<a href='cart.php?action=delete&id=$cartId'>delete</a> 

which creates a link similar to this: cart.php?action=delete&id=1

The file cart.php is here:

<?php
require_once('Connections/ships.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];

$items = explode(',',$cart);


    if (count($items) > 5) 
    {
    header("Location: shipinfo_full.php") ;
    }
    else 
    {
    switch ($action) 
        {
        case 'add':
        if ($cart) 
            {
            $cart .= ','.$_GET['ship_id'];
            } 
            else 
            {
            $cart = $_GET['ship_id'];
            }
            header("Location: info_added.php?ship_id=" .  $_GET['ship_id']) ;
            break;
            case 'delete':
            if ($cart) 
            {
            $items = explode(',',$cart);
            $newcart = '';
            foreach ($items as $item) 
                {
                if ($_GET['ship_id'] != $item) 
                    {
                    if ($newcart != '') 
                        {
                        $newcart .= ','.$item;
                        } 
                        else 
                        {
                        $newcart = $item;
                        }
                    }
                }
                $cart = $newcart;

            }
            header("Location: info.php?ship_id=" .  $_GET['ship_id']) ;
            break;
            $cart = $newcart;
            break;
        }
        $_SESSION['cart'] = $cart;

    }
?>

Any ideas how I can just delete a single item?

Was it helpful?

Solution

Check this out:

case 'delete':
    if ($cart) 
    {
        $items = explode(',',$cart);
        $newcart = array();
        foreach ($items as $item) 
        {
            if ($_GET['ship_id'] != $item) 
            {
                $newcart[] = $item;

            }
        }
        $_SESSION['cart'] = implode(',', $newcart);

    }
    header("Location: info.php?ship_id=" .  $_GET['ship_id']) ;
break;

it will fill newcart array with all items except $_GET['ship_id']. One more thing, fill session before redirecting.

OTHER TIPS

You can write it in a better way, by storing items in an array inside the session like the following

$_SESSION['cart'] = array(); // cart is initially empty

Now adding items to the cart

$_SESSION['cart'][] = array('name' => 'some name', 'price' => 100);

Deleting an item from the cart

unset($_SESSION['cart'][22]); // assuming that 22 is the item ID

Listing items

$cart = $_SESSION['cart'];
forearch($cart as $item){
    echo $item['name']; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top