Page 1 of 1
elseif statement
Posted: Mon Mar 21, 2005 10:05 pm
by Jim_Bo
Hi,
elseif not working right ..
Code: Select all
if($_SESSION['user_level'] == 5) {
echo "this";
}else if($_SESSION['user_level'] == 4) {
echo "or this";
}else if($_SESSION['user_level'] == 3) {
echo "Or maybe this";
}else{
echo "that";
Is that correct the if .. works .. and the first else if .. but the next else if has srange issues ..
Thanks
Posted: Mon Mar 21, 2005 10:12 pm
by feyd
for something like this, use a switch.
elseif .. else if is different functionality, although very similar, has slightly differing logic handling sometimes..
Posted: Mon Mar 21, 2005 10:43 pm
by Jim_Bo
Hi,
Is it something like the following:
Code: Select all
switch(user_level) {
case 1: ($_SESSION['user_level'] == 5)
echo " | <a href=\"#\" onMouseOver=\"showmenu(event,linkset[1])\" onMouseOut=\"delayhidemenu()\">ADMIN</a>";
break;
case 2: ($_SESSION['user_level'] == 4)
echo " | <a class=\"two\" href=\"#\" onMouseOver=\"showmenu(event,linkset[2])\" onMouseOut=\"delayhidemenu()\">ADMIN</a>";
break;
case 3: ($_SESSION['user_level'] == 3)
echo " | <a class=\"two\" href=\"#\" onMouseOver=\"showmenu(event,linkset[3])\" onMouseOut=\"delayhidemenu()\">ADMIN</a>";
break;
default:
echo "";
}
I get an error ot the first echo ..?
Thanks ..
Posted: Mon Mar 21, 2005 11:08 pm
by feyd
you pass nothing to switch(), your breaks don't have semicolons, your default is errored.
Posted: Mon Mar 21, 2005 11:24 pm
by Jim_Bo
Hi,
Is this correct?
Code: Select all
<?php
switch (user_level) {
case ($_SESSION['user_level'] == 5):
echo " | <a href=\"#\" onMouseOver=\"showmenu(event,linkset[1])\" onMouseOut=\"delayhidemenu()\">ADMIN</a>";
break;
case ($_SESSION['user_level'] == 4):
echo " | <a href=\"#\" onMouseOver=\"showmenu(event,linkset[2])\" onMouseOut=\"delayhidemenu()\">ADMIN</a>";
break;
case ($_SESSION['user_level'] == 3):
echo " | <a href=\"#\" onMouseOver=\"showmenu(event,linkset[3])\" onMouseOut=\"delayhidemenu()\">ADMIN</a>";
break;
default:
echo " ";
}
?>
Thanks ..
Posted: Mon Mar 21, 2005 11:33 pm
by feyd
you're not asking it to check the session data.
Code: Select all
switch($_SESSIONї'user_level'])
{
case 5:
// do a user level 5 thing
break;
default:
// do an uncaptured thing
break;
}
Posted: Mon Mar 21, 2005 11:42 pm
by Jim_Bo
Hi,
Thanks for that .. Works a treat ..
It seems to work with:
Is that posible and just bad design leavin out the session?
Thanks ..
Posted: Mon Mar 21, 2005 11:46 pm
by feyd
it only works to a minor degree. Your code is exploiting the handling of unknown constant strings, and the case comparison system. If you had error_reporting cranked to all, you'd very likely get a notice about user_level being an unknown constant, assuming 'user_level'
Posted: Tue Mar 22, 2005 12:38 am
by Jim_Bo
Hi feyd,
Great .. Thanks for the explaination .. You are a very knowledgeable person .. Your help is very much appreciated!
Jimbo