01hanstu Posted November 18, 2009 Share Posted November 18, 2009 Hi, Hope you can help. I am trying to use PHP switch to make the navigation cleaner. So far i have the code and when i try to access the switch command in the address bar it don't work i.e. http://site.co.uk/index.php?page=home. <?php //set page name //$page = "home"; switch ($page) { case "home": include("imgdefault.php"); break; case "pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; } ?> And it wont work. I have deliberately commented the variable $Page so i can access it through the address bae Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/ Share on other sites More sharing options...
premiso Posted November 18, 2009 Share Posted November 18, 2009 <?php //set page name //$page = "home"; switch ($_GET['page']) { case "home": include("imgdefault.php"); break; case "pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; } ?> You have to access the variable via GET as it is being passed as such. In prior versions of PHP register_globals was turned on which basically extract'ed all the variables from GET/POST/COOKIES into their corresponding associative index name as the variable. This was a major security flaw and has since been turned off. So if you were counting on that being on, I would code like it is not on to avoid any type of possible exploiting of your code Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-960035 Share on other sites More sharing options...
01hanstu Posted November 18, 2009 Author Share Posted November 18, 2009 Brilliant. I know where I went wrong I was trying to 'get' the value before the switch command rather than in the switch command. Thanks Stuart Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-960043 Share on other sites More sharing options...
premiso Posted November 18, 2009 Share Posted November 18, 2009 Brilliant. I know where I went wrong I was trying to 'get' the value before the switch command rather than in the switch command. Not necessarily, this would have worked as well: <?php //set page name $page = $_GET['page']; switch ($page) { case "home": include("imgdefault.php"); break; case "pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; } ?> You just had to define $page to be the value of $_GET from the URL Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-960050 Share on other sites More sharing options...
01hanstu Posted November 19, 2009 Author Share Posted November 19, 2009 Hi premiso, I know what I did wrong, dont it make you feel like an idiot when its something simple. I did the Switch and its working. Thanks premiso, 01hanstu Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-961072 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Share Posted November 19, 2009 i did this for my site to clean up nav, <? $requested = $_GET['p']; if($requested == "policies"){ include ("p.html"); } elseif($requested == "join"){ include ("join.php"); } elseif($requested == "projects"){ include ("projects.php"); } elseif($requested == "progression"){ include ("progression.php"); } elseif($requested == "suggest"){ include ("suggest.php"); } elseif($requested == "Forum"){ include ("Forum.php"); } elseif($requested == "contact"){ include ("suggest.php"); } elseif($requested == "about"){ include ("suggest.php"); } else{ //default page if no $requested include ("home.html"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-961203 Share on other sites More sharing options...
Koobi Posted November 19, 2009 Share Posted November 19, 2009 Hi premiso, I know what I did wrong, dont it make you feel like an idiot when its something simple. I did the Switch and its working. Thanks premiso, 01hanstu haha i've been working with PHP since the early 4.x releases and i still make stupid mistakes that make me feel like a total idiot once little thing about your switch case...always have a 'default' catch all. because, what if i decide to type the URL in manually and i do: http://site.co.uk/index.php?page=imanidiotsometimes if you really want to make your navigation cleaner, you can do this: $definedPages = array('default', 'print', 'web'); $myPath = 'path/to/dir/'; if (in_array($_GET['page'], $definedPages)) { require_once $path . 'img' . $_GET['page'] . '.php'; } else { require_once $path . 'imgdefault.php'; } the url's would look like: http://site.co.uk/index.php?page=web http://site.co.uk/index.php?page=print http://site.co.uk/index.php?page=default http://site.co.uk/index.php?page= http://site.co.uk/index.php the last three url's would give you the same result. the only disadvantage here is that the switch/case is fractionally faster than an if condition but for a simple navigation, this wont be an issue. i'm talking about microseconds. why do i hide the 'img' part from the end user? just because it's better that the end user has no idea whats going on in your backend for security reasons. Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-961227 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Share Posted November 19, 2009 ok i got a question, can you take $definedPages = array('default', 'print', 'web'); and make a script to polulate the array based on the links it can find on any given page. Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-961250 Share on other sites More sharing options...
j0n Posted November 19, 2009 Share Posted November 19, 2009 ok i got a question, can you take $definedPages = array('default', 'print', 'web'); and make a script to polulate the array based on the links it can find on any given page. Sure. Crappy example, but here you go: <ul> <?php foreach($definedPages as $page) { echo sprintf('<li><a href="/some/path/index.php?page=%s">%s</a></li>', $page, ucfirst($page)); } ?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-961264 Share on other sites More sharing options...
Koobi Posted November 19, 2009 Share Posted November 19, 2009 j0n, if i'm not mistaken, i think he meant that he wanted to populate the $definedPages array based on the HTML anchor tags in a page. not the other way around. ok i got a question, can you take $definedPages = array('default', 'print', 'web'); and make a script to polulate the array based on the links it can find on any given page. there are always ways to do things like that via regular expressions and whatnot but that's really bad practice. ideally, you could have an include at the top of the page that defines this array. but grabbing the links in a page by parsing the HTML would be a very bad idea though it can be done. anyway, if you have more questions on this, you should probably make a new thread unless the OP doesn't mind this in his thread Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-961268 Share on other sites More sharing options...
Trizen Posted November 19, 2009 Share Posted November 19, 2009 i was asking because i think that is what he is after. i tried it written like you (koobi) wrote it and i ran into the same problem. i would have to define the array manually for every single page, but i think he wants the nav to be i made the links and the php now automatically knows them and dynamically loads them. Quote Link to comment https://forums.phpfreaks.com/topic/182007-php-switch/#findComment-961279 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.