Jump to content

isset, isn't working like I thought it would


NerdConcepts

Recommended Posts

  Ok, here is what I am trying to do. When someone logins in it gives them a "user_id" and "user_type" which is based off of a num (0-3) stored in the database...I have that working just fine. Since when $_Session['user_type'] is echoed it shows up the right number.

 

  Well, I am trying to write a theme selector based off of the users "user_type" . I see if the ['user_type'] is set, if it is then it moves that number (0-3) to a variable called $usertheme. If it is not set (else) then it makes the default value of $usertheme as '0'.

 

  Then at the end it asks if the $usertheme value is 0, 1, 2, 3 and defines the proper $themed value. $themed is then referenced in the main theme files to change the directory to which is looks for images/files etc to show the theme.

 

  You may understand this more...here is my code (excluding me calling to $themed).

 

  When I log in and echo $_SESSION['user_type'] I get the right value. Yet when I echo $usertheme or $themed is comes up with '0'...always...not matter if I am logged in or not.

 

  Also, when I place any "else" or "elseif" commands in the 2nd part it comes up with an error. Saying the else/elseif is unexpected. I've been playing with this for quite sometime now and cannot figure it out...

 

--------------------------------------

if (isset($_SESSION['user_id'])) { // Logged in Already...get User Type....

$usertheme = $_SESSION['user_type'];

} else { // No Match

$usertheme = '0';

}

 

// Make $usertheme $themed and define it

 

if ($usertheme == '1'); { // Not Logged or Free

$themed = 'theme/platinum/';

}

--------------------------------------

first thing with isset, is that if it is null it is still set, so you may want to check if its empty

 

also are you using the session correctly

<?php
session_start(); // start up your PHP session! 
?>

 

 

 

Also, when I place any "else" or "elseif" commands in the 2nd part it comes up with an error. Saying the else/elseif is unexpected. I've been playing with this for quite sometime now and cannot figure it out...

 

Change

if ($usertheme == '1'); { // Not Logged or Free
   $themed = 'theme/platinum/';
}

 

to

 

if ($usertheme == '1') { // Not Logged or Free
   $themed = 'theme/platinum/';
}

 

note the ");" has been removed

if (isset($_SESSION['user_id'])) { // Logged in Already...get user_type....
$usertheme = $_SESSION['user_type'];		
} else { // No Match so assign user_type
$usertheme = '0';
}

if ($usertheme == '1') { // Basic theme
$themed = 'theme/default/';
} elseif ($usertheme == '2') { // Platinum theme
$themed = 'theme/basic/';
} elseif ($usertheme == '3') { // Family theme
$themed = 'theme/platinum/';
} else { // Not Logged in or Default theme
$themed = 'theme/default/';
}

 

That is the new code. Yet it always seems to have a $themed value of '0' ... When it should be '2' when I am logged in. I would think that it is in the first part of the code, yet I cannot seem to figure out what is wrong with it.

 

Also. There is always a session when on the site.

 

When Logged in: echo $usertheme = '0' (when it should be '2'). because echo $_SESSION['user_type'] shows as '2' like it should. BTW, the user I logged in is 'user_type' = '2', defined by the login grabbing the information out of the database.

OK this works

 

1st run i uncommented the

$_SESSION['user_id'] = "its_me";
$_SESSION['user_type'] = "2";

 

result

themed - theme/basic/

usertheme - 2

 

commented them and ran again

result

themed - theme/basic/

usertheme - 2

 

so i would check you have

session_start();

at the start

 

<?php
session_start();
//$_SESSION['user_id'] = "its_me";
//$_SESSION['user_type'] = "2";

if (isset($_SESSION['user_id'])) { // Logged in Already...get user_type....
$usertheme = $_SESSION['user_type'];		
} else { // No Match so assign user_type
$usertheme = '0';
}

if ($usertheme == '1') { // Basic theme
$themed = 'theme/default/';
} elseif ($usertheme == '2') { // Platinum theme
$themed = 'theme/basic/';
} elseif ($usertheme == '3') { // Family theme
$themed = 'theme/platinum/';
} else { // Not Logged in or Default theme
$themed = 'theme/default/';
}

echo "themed - $themed<br>";
echo "usertheme - $usertheme";


?>

Well that is something I didn't notice at first. There is tons of code that I have for the theme, header, footer, error_handler(s), mysql connect files, etc etc. Well I was calling to that code I showed right before I called to the file header, which started the session. So I re-wrote some stuff and now it's fixed. Values are returning like normal and it all works. Thanks. :)

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.