Minus 1 from qty in shopping cart
Posted: Wed Sep 23, 2009 5:07 am
Hi there.
I have recently finished my frist e-commerce shop creating my own shopping cart etc. I have managed to create a button which will add 1 to the quantity but i am finding it really difficult to create a button which will remove 1 from the qty.
My cart works very simply but adding the prod code to an array then counting the amount of times that code appear im the array to get the qty.
i.e. $cart = 001, 001, 002, 002, 002 would be - prod 1 X2 & prod 2 X3.
To add to the qty it simply appends the prod code onto the end of the array. But to remove 1 from the qty i would need to remove 1 prod code from the array not all.
I do not know how to right this code i have tried very hard and am stumped by it just wondering if any one has any ideas Any help would be greatly appreciated.
Thanks
Code:
<?php
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action)
{
case 'add':
if ($cart)
{
$cart .= ','.$_GET['id']; Case add is fine }
else
{
$cart = $_GET['id'];
}
break;
case 'delete':
if ($cart)
{
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item)
{
if ($_GET['id'] != $item)
{
if ($newcart != '')
{
$newcart .= ','.$item; Case remove is fine }
else
{
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'minus':
Case minus needs writing so when the correct link is is clicked it deletes1 of the corrct ids from the array
break;
}
$_SESSION['cart'] = $cart;
I have recently finished my frist e-commerce shop creating my own shopping cart etc. I have managed to create a button which will add 1 to the quantity but i am finding it really difficult to create a button which will remove 1 from the qty.
My cart works very simply but adding the prod code to an array then counting the amount of times that code appear im the array to get the qty.
i.e. $cart = 001, 001, 002, 002, 002 would be - prod 1 X2 & prod 2 X3.
To add to the qty it simply appends the prod code onto the end of the array. But to remove 1 from the qty i would need to remove 1 prod code from the array not all.
I do not know how to right this code i have tried very hard and am stumped by it just wondering if any one has any ideas Any help would be greatly appreciated.
Thanks
Code:
<?php
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action)
{
case 'add':
if ($cart)
{
$cart .= ','.$_GET['id']; Case add is fine }
else
{
$cart = $_GET['id'];
}
break;
case 'delete':
if ($cart)
{
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item)
{
if ($_GET['id'] != $item)
{
if ($newcart != '')
{
$newcart .= ','.$item; Case remove is fine }
else
{
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'minus':
Case minus needs writing so when the correct link is is clicked it deletes1 of the corrct ids from the array
break;
}
$_SESSION['cart'] = $cart;