scarhand Posted August 2, 2007 Share Posted August 2, 2007 I'm stumped with this. I'm trying to create a conditional statement for my menu that will check to see if the page is on the default case. Here is my switch code (this works fine): switch($_GET['p']) { case 'about': AboutPage(); break; case 'rules': RulesPage(); break; case 'join': JoinPage(); break; case 'files': FilesPage(); break; case 'donate': DonatePage(); break; case 'staff': StaffPage(); break; case 'wiki': SoonPage(); break; default: IndexPage(); } Heres the code I can't seem to get to work: <?php if (is_null($_GET['p'])) { echo "<td class=\"topmenu\"> \n"; } else { echo "<td class=\"topmenu2\"> \n"; } ?> <a href="#">INDEX</a> </td> Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/ Share on other sites More sharing options...
scarhand Posted August 2, 2007 Author Share Posted August 2, 2007 OK nevermind that works, but when you go to index.php?p=(insert anything that doesnt have a case here) then it doesnt work. Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313523 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 is this sort out or what? Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313528 Share on other sites More sharing options...
scarhand Posted August 2, 2007 Author Share Posted August 2, 2007 not entirely it works if there is no ?p= at the end of index.php but if i try to go to index.php?p=(anything without a case for it here) it doesn't work Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313534 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 add break; in your default Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313540 Share on other sites More sharing options...
btherl Posted August 2, 2007 Share Posted August 2, 2007 There is no need for break in the default case. Can you try adding a simple statement like "print 'hello, i am the default case'; exit(0);" in the default case? Just to check that it's actually being reached. I have a suspicion that IndexPage() is not working as you expect. Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313542 Share on other sites More sharing options...
scarhand Posted August 2, 2007 Author Share Posted August 2, 2007 add break; in your default default has a break, and it still doesnt work... Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313545 Share on other sites More sharing options...
scarhand Posted August 2, 2007 Author Share Posted August 2, 2007 here, i'll post my entire code. here is the switch in index.php switch($_GET['p']) { case 'about': AboutPage(); break; case 'services': ServicesPage(); break; case 'scripts': ScriptsPage(); break; case 'websites': WebsitesPage(); break; case 'images': ImagesPage(); break; case 'contact': ContactPage(); break; default: IndexPage(); } here is the menu in header.php (header.php is included at the top of index.php): <table cellpadding="0" cellspacing="0" border="0" width="1%"> <tr> <?php if (is_null($_GET['p'])) { echo "<td class=\"topmenu2\"> \n"; echo "HOME \n"; echo "</td> \n"; } else { echo "<td class=\"topmenu\"> \n"; echo "<a href=\"index.php\">HOME</a> \n"; echo "</td> \n"; } if ($_GET['p'] == "about") { echo "<td class=\"topmenu2\"> \n"; echo "ABOUT \n"; echo "</td> \n"; } else { echo "<td class=\"topmenu\"> \n"; echo "<a href=\"index.php?p=about\">ABOUT</a> \n"; echo "</td> \n"; } if ($_GET['p'] == "services") { echo "<td class=\"topmenu2\"> \n"; echo "SERVICES \n"; echo "</td> \n"; } else { echo "<td class=\"topmenu\"> \n"; echo "<a href=\"index.php?p=services\">SERVICES</a> \n"; echo "</td> \n"; } if ($_GET['p'] == "scripts") { echo "<td class=\"topmenu2\"> \n"; echo "SCRIPTS \n"; echo "</td> \n"; } else { echo "<td class=\"topmenu\"> \n"; echo "<a href=\"index.php?p=scripts\">SCRIPTS</a> \n"; echo "</td> \n"; } if ($_GET['p'] == "websites") { echo "<td class=\"topmenu2\"> \n"; echo "WEBSITES \n"; echo "</td> \n"; } else { echo "<td class=\"topmenu\"> \n"; echo "<a href=\"index.php?p=websites\">WEBSITES</a> \n"; echo "</td> \n"; } if ($_GET['p'] == "images") { echo "<td class=\"topmenu2\"> \n"; echo "IMAGES \n"; echo "</td> \n"; } else { echo "<td class=\"topmenu\"> \n"; echo "<a href=\"index.php?p=images\">IMAGES</a> \n"; echo "</td> \n"; } if ($_GET['p'] == "contact") { echo "<td class=\"topmenu2\" style=\"border-right: 0px;\"> \n"; echo "CONTACT \n"; echo "</td> \n"; } else { echo "<td class=\"topmenu\" style=\"border-right: 0px;\"> \n"; echo "<a href=\"index.php?p=contact\">CONTACT</a> \n"; echo "</td> \n"; } ?> </tr> </table> get what im trying to figure out now? Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313546 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 ContactPage(); <---function??? can we seee even one of those Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313547 Share on other sites More sharing options...
scarhand Posted August 2, 2007 Author Share Posted August 2, 2007 function AboutPage () { ?> <div class="headd"> ABOUT THIS WEBSITE </div> <br /> <div class="content"> Insert about page content here. </div> <?php } Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313554 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 try to echo the get p inside the switch if you cant still figure the error by that post the code having the switch statement Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313556 Share on other sites More sharing options...
scarhand Posted August 2, 2007 Author Share Posted August 2, 2007 try to echo the get p inside the switch if you cant still figure the error by that post the code having the switch statement I dont get any errors. I want the TD class to be "topmenu2" for the HOME link when someone tries to go to a ?p=page that does not exist. ahh I can't explain it.... Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313559 Share on other sites More sharing options...
teng84 Posted August 2, 2007 Share Posted August 2, 2007 try this no need to query string it gets the actual page switch(basename($_SERVER['REQUEST_URI']){ case 'index.php': break; etc.... } Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313569 Share on other sites More sharing options...
btherl Posted August 2, 2007 Share Posted August 2, 2007 Scarhand, can you create a small piece of code that replicates your problem. Then we can debug that. The other option is to post your FULL code, along with instructions on how to replicate the problem. So far, you have only given us portions of your code, and all those portions work correctly. Showing us code that works does not help us fix your problem Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313577 Share on other sites More sharing options...
scarhand Posted August 2, 2007 Author Share Posted August 2, 2007 OK this is what I want to do. I am trying to make an if statement that will show something if the user is on the default case page. Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313687 Share on other sites More sharing options...
btherl Posted August 2, 2007 Share Posted August 2, 2007 Ok. Given that description, the simplest way I can think of is to set a variable inside the default case, like this: ... default: $default_case = true; IndexPage(); } Then later you can do this: if ($default_case) { # Do the default case stuff } Edit: If you are inside a function, you must do "global $default_case;" before accessing the variable Link to comment https://forums.phpfreaks.com/topic/62965-solved-conditional-statement-for-default-case/#findComment-313714 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.