Flight19 Posted November 6, 2009 Share Posted November 6, 2009 A little background on the document... This script is already being used in conjunction with an include statement to pull the appropriate page content into the file. It works just fine, but I figured I should mention it in case it has any bearing on the script I'm having trouble with: <?php require_once("utils.php"); $_params = getParams(); /* $_pageName = ($_params["page"] != "" ? $_params["page"] : "home"); $_pageName .= ".php"; */ $_pageName = getPage() . ".php"; ?> My site has a number of sections. The above script pulls individual page material in, but I'm also trying to include some graphics and a sub-menu that are specific to the "section." For example, I want one set of images and sub-menu to display when someone is viewing the "home," "company," "contact," or "faq" pages, and another set of images and sub-menu to display when someone is viewing the "print overview" or "printing arrangement pages. I've started the following Switch script to accomplish this, but it doesn't work yet. I'm new to PHP and still learning so if anyone can make a suggestion or let me know what I am doing wrong, it would be greatly appreciated. Here is the Switch script thus far: <?php //set page name $page = "home"; switch ($page) { case "home || overview || contact || faq": include("imgdefault.php"); break; case "pr_overview || pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/ Share on other sites More sharing options...
rajivgonsalves Posted November 6, 2009 Share Posted November 6, 2009 your code should be <?php //set page name $page = "home"; switch ($page) { case "home": case "overview": case "contact": case "faq": include("imgdefault.php"); break; case "pr_overview": case "pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952833 Share on other sites More sharing options...
JAY6390 Posted November 6, 2009 Share Posted November 6, 2009 You can't use it like that. Try it like this <?php //set page name $page = "home"; switch ($page) { case "home": case "overview": case "contact": case "faq": include("imgdefault.php"); break; case "pr_overview": case "pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952834 Share on other sites More sharing options...
Flight19 Posted November 6, 2009 Author Share Posted November 6, 2009 Thanks! That helped quite a bit. I was not aware that I could not use "||" for this. The include for the "home," "contact," etc. pages is now working, but it pulls into the other pages as well, so I don't think the switch is working. I've gone back over everything and it looks correct. Is there some dumb little thing I am missing? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952892 Share on other sites More sharing options...
rajivgonsalves Posted November 6, 2009 Share Posted November 6, 2009 your force setting the $page variable to home $page = "home"; it should be something like $page = $_GET['page']; I guess Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952898 Share on other sites More sharing options...
Flight19 Posted November 7, 2009 Author Share Posted November 7, 2009 Okay. I tried using: $page = $_GET['page']; and $page = "_pageName"; as well as leaving it blank like this. $page = ""; If I don't include the statement to set the page at all, no part of the site loads. If I do include it and set it to "home," only the material for the "home" set is pulled into all of the pages. If I do either of the above, the site loads, but with the content to be called by the switch statement completely absent. I know I have a lot to learn, and all help given is much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952918 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 on considering what you said try this <?php //set page name $page = [put the variable name that indicates the page name]; switch ($page) { case "pr_overview": case "pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; default: include("imgdefault.php"); break; } ?> by default now the imgdefault.php will include, you'll have to tell me the url structure if I know that I can track down whats the variable name for the $page Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952920 Share on other sites More sharing options...
Flight19 Posted November 7, 2009 Author Share Posted November 7, 2009 At the moment, all of my .php documents are just in the root folder. (Hopefully that's what you were asking me to clarify. if not, let me know!) Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952936 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 actually only wanted the know what is the url in your browser how do you identify what page it is on? Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952955 Share on other sites More sharing options...
Flight19 Posted November 7, 2009 Author Share Posted November 7, 2009 All of the page content is pulled into the index.php via include statement. Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952958 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 did the above code work well ? or your still facing problems with the header ? Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952961 Share on other sites More sharing options...
Flight19 Posted November 7, 2009 Author Share Posted November 7, 2009 I don't know if the problem is with the header, or if it's something else. It does pull content in, but only the default. The switch does not get made. Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952985 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 that is because page is not getting initialize what do you see in the browser address bar it should be some like index.php?.... ? on each of the pages Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952988 Share on other sites More sharing options...
Flight19 Posted November 7, 2009 Author Share Posted November 7, 2009 http://flight19creative.com/webtest/home http://flight19creative.com/webtest/overview.html (Still trying to figure out why this one comes up with the .html extension. There is no overview.html in this directory, and no links for overview.html, so something odd is going on here.) Anyway, hopefully you can see what's going on and tell me what I am doing wrong! I'm a decent designer, and I'm good with XHTML and CSS, but I want to learn more than that, and I really want to learn PHP! Thanks for all of your help! Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-952998 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 your switch should be the following <?php switch (getPage()) { case "pr_overview": case "pr_arrangement": include("imgprint.php"); break; case "web_overview": include("imgweb.php"); break; default: include("imgdefault.php"); break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-953000 Share on other sites More sharing options...
Flight19 Posted November 7, 2009 Author Share Posted November 7, 2009 Wow! This seems to have done it. I can follow most of it since it's mostly what I started out with, but what do the extra parenthesis around the "getPage" mean and why do they need to be there? One of the problems I've been having with tutorials is that while they offer very basic examples of the syntax to use, they don't really give exercises or examples for how to build on them. Thank you so much for your help! I really really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-953001 Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2009 Share Posted November 7, 2009 Glad to be of help, the extra parenthesis is part of the switch syntax. you should go to php.net and see the documentation, its pretty good and explain the basics of the language http://www.php.net/manual/en/ Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-953002 Share on other sites More sharing options...
Flight19 Posted November 10, 2009 Author Share Posted November 10, 2009 Hmmm... My last reply from a few days ago didn't post. Odd. Anyway, I'll just repeat what I had set to post before... Thank you so much for your help. That last suggestion worked beautifully. Since I am trying to learn this, (only if you have time and it's convenient) can you explain to me why the extra parenthesis work and are needed? And also why the || wouldn't work in my earlier versions of this PHP statement? Thanks so much again! Quote Link to comment https://forums.phpfreaks.com/topic/180602-solved-need-assistance-with-a-switch-statment/#findComment-955094 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.