elseif statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

elseif statement

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for something like this, use a switch.

elseif .. else if is different functionality, although very similar, has slightly differing logic handling sometimes..
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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 ..
Last edited by Jim_Bo on Mon Mar 21, 2005 11:20 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you pass nothing to switch(), your breaks don't have semicolons, your default is errored.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post 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 ..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're not asking it to check the session data.

Code: Select all

switch($_SESSION&#1111;'user_level'])
{
  case 5:
    // do a user level 5 thing
    break;

  default:
    // do an uncaptured thing
    break;
}
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Thanks for that .. Works a treat ..

It seems to work with:

Code: Select all

switch (user_level)

{
Is that posible and just bad design leavin out the session?

Thanks ..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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'
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi feyd,

Great .. Thanks for the explaination .. You are a very knowledgeable person .. Your help is very much appreciated!

Jimbo
Post Reply