Jump to content

[SOLVED] Can't unset session via if statement


Alexhoward

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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