doddsey_65 Posted September 21, 2010 Share Posted September 21, 2010 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 More sharing options...
joel24 Posted September 21, 2010 Share Posted September 21, 2010 ... because isset($_GEt['do']) is returning true, or false. so your if statement is transformed to elseif (true/false == 'today') you need to have } elseif (isset($_GET['do']) && $_GET['do'] == 'today') { Link to comment https://forums.phpfreaks.com/topic/213993-elseif/#findComment-1113646 Share on other sites More sharing options...
onlyican Posted September 21, 2010 Share Posted September 21, 2010 With this many if statements can I advice using SWITCH switch($_GET){ case 'topic': include 'modules/pages/posts.php'; break; case 'forum': include 'modules/pages/topics.php'; break; case ''parent': ..... } Link to comment https://forums.phpfreaks.com/topic/213993-elseif/#findComment-1113648 Share on other sites More sharing options...
rwwd Posted September 21, 2010 Share Posted September 21, 2010 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 More sharing options...
doddsey_65 Posted September 21, 2010 Author Share Posted September 21, 2010 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 More sharing options...
joel24 Posted September 22, 2010 Share Posted September 22, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.