Jump to content

[SOLVED] Need assistance with a switch statment


Flight19

Recommended Posts

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;

}

?>

Link to comment
Share on other sites

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;
}
?>

 

 

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.