wrathican Posted May 10, 2007 Share Posted May 10, 2007 hi i recently tried using the switch method for a navigation system as opposed to using the get method. this is the code i am using with the get method commented out: <?php // Opening tag for PHP /*$p = $_GET['p']; if ( !empty($p) && file_exists('./pages/' . $p . '.php') && stristr( $p, '.' ) == False ) { $file = './pages/' . $p . '.php'; } else { $file = './pages/default.php'; } include $file;*/ switch(£_GET['p']){ case "contact": //sets the variable to contact $file = './pages/contact.php'; break; case "portfolio" $file = './pages/portfolio.php'; break; } include $file; ?> this is the code for my index file. if the url is blank there is no file to include. however i do already have a file that i want to include. how would i check to see if the url is blank using switch so i can include this file? and also. when using switch do i still put ?=variable as the link for links? and is it supposed to seen in the url like: www.domain.com/index.php?=variable ????? Quote Link to comment https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/ Share on other sites More sharing options...
trq Posted May 10, 2007 Share Posted May 10, 2007 switch(£_GET['p']){ case "contact": //sets the variable to contact $file = './pages/contact.php'; break; case "portfolio" $file = './pages/portfolio.php'; break; default: $file = "./pages/yourdefault.php"; break; } include $file; ?> Quote Link to comment https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/#findComment-249871 Share on other sites More sharing options...
obsidian Posted May 10, 2007 Share Posted May 10, 2007 In a switch statement, after all your case statements, just set up a default at the end to catch anything besides the cases you have declared: <?php switch ($_GET['pg']) { case 'contact': require('contact.php'); break; case 'about': require('about.php'); break; default: require('default.php'); } ?> Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/#findComment-249873 Share on other sites More sharing options...
mort Posted May 10, 2007 Share Posted May 10, 2007 Hey dude I tend to use this $view = $_GET['view']; // Navigation if (!empty($view) && file_exists("./" . $view . ".php")) { $file = "./" . $view . ".php"; } elseif (empty($view)) { $file = "main.php"; } else { $file = "not_exist.php"; } include $file; I think this would do a similar function to what you are after, plus if the page doesnt exist it redirects to a custom 404 page. Quote Link to comment https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/#findComment-249874 Share on other sites More sharing options...
wrathican Posted May 10, 2007 Author Share Posted May 10, 2007 hey guys thanks for that!! mort thanks neways but ive been told to steer away from the get method as it is unsafe i suggest you take a leaf out of this book.. maybe? Quote Link to comment https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/#findComment-249885 Share on other sites More sharing options...
per1os Posted May 10, 2007 Share Posted May 10, 2007 Either way, the switch case is much cleaner and less prone to errors. Speaking from expierence. Quote Link to comment https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/#findComment-249887 Share on other sites More sharing options...
trq Posted May 10, 2007 Share Posted May 10, 2007 mort thanks neways but ive been told to steer away from the get method as it is unsafe i suggest you take a leaf out of this book.. maybe? Yes. using the $_GET variable directly is unsafe. mort, if your server has url_wrappers enabled your code is open to script injection. Quote Link to comment https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/#findComment-249892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.