c_shelswell Posted January 25, 2007 Share Posted January 25, 2007 got a problem i can't seem to figurei'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 Link to comment https://forums.phpfreaks.com/topic/35673-what-am-i-doing-wrong-im-sure-this-if-statment-is-cheating/ Share on other sites More sharing options...
boo_lolly Posted January 25, 2007 Share Posted January 25, 2007 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. Link to comment https://forums.phpfreaks.com/topic/35673-what-am-i-doing-wrong-im-sure-this-if-statment-is-cheating/#findComment-168989 Share on other sites More sharing options...
Orio Posted January 25, 2007 Share Posted January 25, 2007 If that doesn't work, give this a try:[code]<?phpif(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. Link to comment https://forums.phpfreaks.com/topic/35673-what-am-i-doing-wrong-im-sure-this-if-statment-is-cheating/#findComment-168992 Share on other sites More sharing options...
c_shelswell Posted January 25, 2007 Author Share Posted January 25, 2007 thanks i'm an idiot i didn't even notice the wee ; guy. Removed that and it works great.Always the simple things.Thanks Link to comment https://forums.phpfreaks.com/topic/35673-what-am-i-doing-wrong-im-sure-this-if-statment-is-cheating/#findComment-168998 Share on other sites More sharing options...
boo_lolly Posted January 25, 2007 Share Posted January 25, 2007 [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! =) Link to comment https://forums.phpfreaks.com/topic/35673-what-am-i-doing-wrong-im-sure-this-if-statment-is-cheating/#findComment-169002 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.