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
https://forums.phpfreaks.com/topic/213993-elseif/
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
https://forums.phpfreaks.com/topic/213993-elseif/#findComment-1113649
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
https://forums.phpfreaks.com/topic/213993-elseif/#findComment-1113666
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
https://forums.phpfreaks.com/topic/213993-elseif/#findComment-1113909
Share on other sites

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.