Alexhoward Posted September 29, 2008 Share Posted September 29, 2008 Hi Guys, Having a bit of trouble here, not sure why it's not working, could anyone please help me out...? I'm trying to say that is a session class equals zero then unset the session, i think the problem may lie in the nested if.. here's what i'm doing : <?php if($action == "add" ) { $_SESSION['a'][$product_id]++; } else if ($action == "remove" ) { $_SESSION['a'][$product_id]--; } else if($_SESSION['a'][$product_id] == 0 ) { unset($_SESSION['a'][$product_id]); } else if($action = "empty" ) { unset($_SESSION['mycart']); } ?> the problems with the unset not working.. Thanks in advance Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 29, 2008 Share Posted September 29, 2008 If one evaluates true, the rest below it are ignored. Isolate the check you want to perform every time <?php if($action == "add" ) { $_SESSION['a'][$product_id]++; } else if ($action == "remove" ) { $_SESSION['a'][$product_id]--; } else if($action = "empty" ) { unset($_SESSION['mycart']); } if($_SESSION['a'][$product_id] == 0 ) { unset($_SESSION['a'][$product_id]); } ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 29, 2008 Share Posted September 29, 2008 Also, you can write it like this <?php if($action == "add" ) $_SESSION['a'][$product_id]++; else if ($action == "remove" ) $_SESSION['a'][$product_id]--; else if($action = "empty" ) unset($_SESSION['mycart']); if($_SESSION['a'][$product_id] == 0 ) unset($_SESSION['a'][$product_id]); ?> Or like this <?php if($action == "add" ) $_SESSION['a'][$product_id]++; else if ($action == "remove" ) $_SESSION['a'][$product_id]--; else if($action = "empty" ) unset($_SESSION['mycart']); if($_SESSION['a'][$product_id] == 0 ) unset($_SESSION['a'][$product_id]); ?> I prefer the former, myself. Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted September 29, 2008 Author Share Posted September 29, 2008 Excellent!! It's working now! Thanks for all your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.