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

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]);
 }
?>

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.

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.