Hi
I have some shopping cart code to add items to a shopping cart:
$action = $_GET['action'];
switch ($action) {
case 'add' :
if(!$cart) {
$cart = $_GET['id'];
} else {
$cart .= ',' . $_GET['id'];
}
break;
case 'delete' :
This results (for my test run) in a session variable containing the following:
cart|s:5:"1,2,1";
For this example I selected one item twice. In my shopping cart I only show two items because this is expensive artwork, and multiple copies are not available. This is working OK. However, my message which shows how many items are in the cart says 3, I guess because I am just getting a count of the string from the session variable.
So I tried this new code to get rid of duplicate variables:
$action = $_GET['action'];
switch ($action) {
case 'add' :
if(!$cart) {
$cart = $_GET['id'];
} else {
$items = explode(',', $cart);
$newcart = '';
foreach($items as $item) {
if($item != $_GET['id']) {
if($newcart != '') {
$newcart .= ',' . $_GET['id'];
} else {
$newcart = $_GET['id]'];
}
}
}
$cart = $newcart;
}
break;
I have tried so many different ways to come up with the correct method. Usually the result is that the cart is emptied.
Any help or hints would be very welcome. If someone can point me to where my logic is going wrong, I would like to try and fix it myself.
Thanks in advance
Cliff