Jump to content

elseif


doddsey_65

Recommended Posts

the following is the contents of my index.php file

 

if (isset($_GET['topic'])) {

include 'modules/pages/posts.php';

} elseif (isset($_GET['forum'])) {

include 'modules/pages/topics.php';

} elseif (isset($_GET['parent'])) {

include 'modules/pages/parents.php';

} elseif (isset($_GET['do']) == 'unanswered') {

include 'modules/pages/unanswered.php';

} elseif (isset($_GET['do']) == 'today') {

include 'modules/pages/today.php';

} else {

include 'modules/pages/forums.php';

}

 

as you can see depending on what the get value is it loads a corrosponding page.

 

however on the last 2 statements it loads the same page index.php?do=unanswered. why wont it load index.php?do=today when the $_GET value == today?

Link to comment
Share on other sites

Use a switch statement, then just switch after you have made sure that $_GET isset(), this will clear up any ambiguity of state.

 

Because as it stands you are evaluating the isset() which returns a bool See from the manual, you need to do this instead, which in your case is quite parser intensive once the if/elseif/else chain is invoked.

 

if (isset($_GET['topic']) && ($_GET['topic'] == "whatever")) {
include 'modules/pages/posts.php';
}
elseif (isset($_GET['forum']) && ($_GET['forum'] == "whatever")) {
include 'modules/pages/topics.php';
}
elseif (isset($_GET['parent']) && ($_GET['parent'] == "whatever")) {
include 'modules/pages/parents.php';
}
elseif (isset($_GET['do']) && ($_GET['do'] == "whatever")) {
include 'modules/pages/unanswered.php';
}
elseif (isset($_GET['do']) && ($_GET['do'] == "whatever")) {
include 'modules/pages/today.php';
}
else{//aka default case
include 'modules/pages/forums.php';
}

 

Extrapolate from that, as you can see, check it's set, then evaluate it's contents; I swear that a switch is more efficient than this method though..

 

Rw

Link to comment
Share on other sites

thanks for the repies. I added the switch but how would i do that for the last 2 where get equals something? i just left them last 2 as elseif statements in the emantime but nothing works. when get equals topic it stays on the same page.

 

switch($_GET){

case 'topic':
include 'modules/pages/posts.php';
break;
case 'forum':
include 'modules/pages/topics.php';
break;
case 'parent':
include 'modules/pages/parents.php';
break;
}

if (isset($_GET['do']) && $_GET['do'] == 'unanswered') {

include 'modules/pages/unanswered.php';

} 

elseif (isset($_GET['do']) && $_GET['do'] == 'today') {

include 'modules/pages/today.php';

} else {

include 'modules/pages/forums.php';

}

Link to comment
Share on other sites

also, do a print_r($_GET); to ensure that the $_GET values are being sent as suspected.

 

//remove this line once everything is working
print_r($_GET);

switch($_GET){

case 'topic':
include 'modules/pages/posts.php';
break;
case 'forum':
include 'modules/pages/topics.php';
break;
case 'parent':
include 'modules/pages/parents.php';
break;
case 'do':
if ($_GET['do'] == 'unanswered') {

include 'modules/pages/unanswered.php';

} elseif (isset($_GET['do']) == 'today') {

include 'modules/pages/today.php';

}
break;

//default, include forums page
default:
include 'modules/pages/forums.php';
break;

}


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.