I don’t understand why this code won’t only delete the pizza_id with the given key that is passed through the URL. I echo back the $_GET['key'] value to check if I have the desired number and it works fine.
The key value comes from another foreach $key=>$value loop that builds the shopping cart and has a delete link with the pizza id and the $key in it.
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET['pizza_id'];
} else {
$cart = $_GET['pizza_id'];
}
break;
case 'delete':
echo "KEY: ".$_GET['key']."<br>";
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $key => $item)
{
if ($_GET['pizza_id'] != $item && $GET['key']!= $key )
{
if ($newcart != '')
{
$newcart .= ','.$item;
}
else
{
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
}
EDIT: maybe I should also mention the basis of the code comes from this webpage http://v3.thewatchmakerproject.com/journal/276/
Well, at a minimum, you don’t need the $key in the foreach.
Another poster pointed out that your original code also has
$GET['key']. Badness 10000. Do you have your warnings and error output set at the maximum level during development?