Jump to content

what am i doing wrong? i'm sure this if statment is cheating


c_shelswell

Recommended Posts

got a problem i can't seem to figure

i've got a little bit of code that should remove an item from a cart if the user clicks remove. If the qty of that item goes down to 0 then the item is altogether take off the cart. thing is though if they have two of the same item at the moment and click remove it removes both of them.

Here's the offending code
[code]
@$id=$_GET['id'];
$_SESSION['cart'][$id] --;
if (($_SESSION['cart'][$id]) == 0);
{
unset($_SESSION['cart'][$id]);

}
$_SESSION['total_price']= calculate_price($_SESSION['cart']);
$_SESSION['items'] = array_sum($_SESSION['cart']);
header("Location: show_cart.php");[/code]

i was hoping that the unset would only happen if that item was down at 0 can't figure out why it's not working like that.

Any ideas?
Thanks very much
try:
[code]
<?php
/*removed error suppression '@'*/
$id=$_GET['id'];
$_SESSION['cart']['$id'] --;

/*added single quotes around id
  and removed parenthesis
  and removed the ; after if    */
if ($_SESSION['cart']['$id'] == 0)//;
{
unset($_SESSION['cart'][$id]);

}
$_SESSION['total_price']= calculate_price($_SESSION['cart']);
$_SESSION['items'] = array_sum($_SESSION['cart']);
header("Location: show_cart.php");
?>
[/code]

also try echoing the values of each variable to make sure they are correct. if you do this you MUST remove the header() otherwise it will bring you an error.
If that doesn't work, give this a try:

[code]<?php

if(isset($_GET['id']))
{
$id = intval($_GET['id']);
if(isset($_SESSION['cart'][$id]))
{
$_SESSION['cart'][$id] --;
if (($_SESSION['cart'][$id]) <= 0);
{
unset($_SESSION['cart'][$id]);
}
$_SESSION['total_price']= calculate_price($_SESSION['cart']);
$_SESSION['items'] = array_sum($_SESSION['cart']);
header("Location: show_cart.php");
}
else
die("Please follow only valid links");
}

?>[/code]

Orio.
[quote author=c_shelswell link=topic=123995.msg513224#msg513224 date=1169740093]
thanks i'm an idiot i didn't even notice the wee ;  guy. Removed that and it works great.

Always the simple things.

Thanks
[/quote]
glad you got it workin! =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.