Jump to content

remove from basket


phpanon

Recommended Posts

Hi,

 

My remove from basket is working exactly right. At the moment when [-] is clicked it removes the item from the basket even if the quantity is more than one.

 

Ideally I would like to just decrease the quantity by one.

 

<?php
session_start();
foreach($_SESSION['basket'] as $key => $product) {
	if ($_SESSION['basket'][$key]['URN'] == $_GET['URN'])
	{
		unset($_SESSION['basket'][$key]);
	}
}
header("Location: StationaryMain.php?var=URN");
exit();
?>

Link to comment
Share on other sites

I presume $_SESSION['basket'][$key]['URN'] holds the quantity, if so something like the following should do it:

<?php
session_start();

foreach($_SESSION['basket'] as $key => $product)
{
    if ($_SESSION['basket'][$key]['URN'] == $_GET['URN'])
    {
        // decrease quantity by one
        $_SESSION['basket'][$key]['URN']--;

        // unset quantity if new quantity is less than 1
        if($_SESSION['basket'][$key]['URN'] < 1)
        {
            unset($_SESSION['basket'][$key]);
        }
    }
}

header("Location: StationaryMain.php?var=URN");
exit();

?>

Link to comment
Share on other sites

this is what i got

 

Array

(

    [0] => Array

        (

            [uRN] => 2

            [productName] => T2 Stapler

            [description] => Metal industrial Stapler. For use on all surfaces and walls.

            [price] => 12.99

            [quantity] => 3

        )

 

    [1] => Array

        (

            [uRN] => 5

            [productName] => Year Planner

            [description] => A3 laminated Year Planner made from 100% recycled card.

            [price] => 19.99

            [quantity] => 1

        )

 

    [2] => Array

        (

            [uRN] => 1

            [productName] => Ink 890 Cartridge

            [description] => High definition Ink cartridge for use with Laser Jet printers

            [price] => 24

            [quantity] => 1

        )

 

)

 

 

Link to comment
Share on other sites

I understand now what URN is, its the product identifier. I have modified my code.

<?php
session_start();

foreach($_SESSION['basket'] as $key => $product)
{
    if ($_SESSION['basket'][$key]['URN'] == $_GET['URN'])
    {
        // decrease quantity by one
        $_SESSION['basket'][$key]['quantity']--;

        // unset quantity if new quantity is less than 1
        if($_SESSION['basket'][$key]['quantity'] < 1)
        {
            unset($_SESSION['basket'][$key]);
        }
    }
}

header("Location: StationaryMain.php?var=URN");
exit();

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.