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 Link to comment https://forums.phpfreaks.com/topic/126332-solved-cant-unset-session-via-if-statement/ 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]); } ?> Link to comment https://forums.phpfreaks.com/topic/126332-solved-cant-unset-session-via-if-statement/#findComment-653259 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. Link to comment https://forums.phpfreaks.com/topic/126332-solved-cant-unset-session-via-if-statement/#findComment-653264 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! Link to comment https://forums.phpfreaks.com/topic/126332-solved-cant-unset-session-via-if-statement/#findComment-653269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.