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;
Minus 1 from qty in shopping cart
Moderator: General Moderators
Re: Minus 1 from qty in shopping cart
Simple logic would be to loop through the shopping cart looking for the id you wish to decrement by 1. When you find the first instance of it, unset it and leave the loop.
That's pretty much it. You could do the same with delete, only you would find every instance of the id and unset each of them.
Here's an example
I would use a multi dimensional array for the shopping cart and store a quantity key in it. You could then reference them like this:
$cart[$i]['id'] and $cart[$i]['qty'] etc... You could then serialize the carts and store them in the database associating them with a session id or user id etc...
Here's an example
Code: Select all
$found = false;
foreach ($items as $item) {
if($_GET['id'] == $item && !$found) {
unset($item);
$found = true;
}
}
$cart[$i]['id'] and $cart[$i]['qty'] etc... You could then serialize the carts and store them in the database associating them with a session id or user id etc...
Re: Minus 1 from qty in shopping cart
Thanks alot for your reply. greatly appreciated. Whilst waiting for a reply i did actually work out a way of doing it (code below) please could you check it to see if it as a sound way of solving the issue, or if i have over complicated it
Cheers
R100PER
case 'minus': //if the link sends action as minus
$id = $_GET['id'];
if ($cart)
{
$items = explode(',',$cart); //explode $cart into $items (array) separated by ,
$minusids = ''; //the varible in which we are going to put the IDs of which we want to remove one from
$restofcart = ''; //the rest of the cart IDs which we dont want to change
foreach ($items as $item)
{
if ($id == $item) //This gets the cart items with the ID same as ID and puts them into $minusids
{
if ($minusids != '')
{
$minusids .= ','.$item;
}
else
{
$minusids = $item;
}
}
else // this puts the rest of the cart into $restofcart
{
if ($restofcart != '')
{
$restofcart .= ','.$item;
}
else
{
$restofcart = $item;
}
}
}
$minusidsarray = explode(',',$minusids); //explodes minus ids into array
$lossedid = array_pop($minusidsarray); //pops the last entry off the end of the array as we know it is definatly going to be at least one of the IDs we want to get rid of
$cartlessid = '';
foreach ($minusidsarray as $minusidsitem) //go through array and put each entry as minusiditems
{
if ($cartlessid != '') //if cartlessid is not equal to zero...
{
$cartlessid .= ','.$minusidsitem; //append ,item
}
else
{
$cartlessid = $minusidsitem; //else startcart less id with item from array
}
}
if ($restofcart=='') //if rest of cart is equal to zero // This bit puts the two carts back together
{
$cart = $cartlessid; //add cartlessid to $cart
}
else
{
if($cartlessid=='') //if cartlessid is empty then
{
$cart = $restofcart; //just make $cart same as $restofcart
}
else
{
$cart = $cartlessid . ',' . $restofcart; //else add $cartlessid,$restofcart to $cart
}
}
}
break;
}
$_SESSION['cart'] = $cart;
Cheers
R100PER
case 'minus': //if the link sends action as minus
$id = $_GET['id'];
if ($cart)
{
$items = explode(',',$cart); //explode $cart into $items (array) separated by ,
$minusids = ''; //the varible in which we are going to put the IDs of which we want to remove one from
$restofcart = ''; //the rest of the cart IDs which we dont want to change
foreach ($items as $item)
{
if ($id == $item) //This gets the cart items with the ID same as ID and puts them into $minusids
{
if ($minusids != '')
{
$minusids .= ','.$item;
}
else
{
$minusids = $item;
}
}
else // this puts the rest of the cart into $restofcart
{
if ($restofcart != '')
{
$restofcart .= ','.$item;
}
else
{
$restofcart = $item;
}
}
}
$minusidsarray = explode(',',$minusids); //explodes minus ids into array
$lossedid = array_pop($minusidsarray); //pops the last entry off the end of the array as we know it is definatly going to be at least one of the IDs we want to get rid of
$cartlessid = '';
foreach ($minusidsarray as $minusidsitem) //go through array and put each entry as minusiditems
{
if ($cartlessid != '') //if cartlessid is not equal to zero...
{
$cartlessid .= ','.$minusidsitem; //append ,item
}
else
{
$cartlessid = $minusidsitem; //else startcart less id with item from array
}
}
if ($restofcart=='') //if rest of cart is equal to zero // This bit puts the two carts back together
{
$cart = $cartlessid; //add cartlessid to $cart
}
else
{
if($cartlessid=='') //if cartlessid is empty then
{
$cart = $restofcart; //just make $cart same as $restofcart
}
else
{
$cart = $cartlessid . ',' . $restofcart; //else add $cartlessid,$restofcart to $cart
}
}
}
break;
}
$_SESSION['cart'] = $cart;