Results 1 to 8 of 8

Thread: switch statement with no page found option

  1. #1
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default switch statement with no page found option

    All,

    I have been running this for quite some time and it's solid, but I want to be able to utilize a page not found option.

    Code:
    <?php
      switch($_GET['page']){
    	case "contact":
    		$displaypage = "page_content/contact.php";
    		$title = "Contact";
    		$description = "";
    		$keywords = "";
    		break;
    	case "about":
    		$displaypage = "page_content/forum.php";
    		$title = "About";
    		$description = "";
    		$keywords = "";
    		break;
    	default:
    		$displaypage = "page_content/home.php";
    		$title = "Home";
    		$description = "";
    		$keywords = "";
    		break;
    }
    ?>
    I want users to be able to get a "page not found" message or set in place a 404.php page if they type domain.com/index.php?page=abc or they misspell the page name like "cntact"

    This switch statement can get pretty long obviously if there are many pages, so maybe there is a better way to write this with say an array like...

    Code:
    $page = array('home', 'about', 'contact');
    $titles = array('Home', 'About us', 'Contact Us');
    Thank you
    Last edited by ?foru; 03-08-2014 at 01:37 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ?foru View Post
    I want users to be able to get a "page not found" message or set in place a 404.php page if they type domain.com/index.php?page=abc or they misspell the page name like "cntact"
    The logical place for a "page not found" option would be in the default case.

    Quote Originally Posted by ?foru View Post
    This switch statement can get pretty long obviously if there are many pages, so maybe there is a better way to write this with say an array…
    Sure. I'd highly recommend making it a multi-dimensional array, however, not separate arrays for the title, page name, etc.. If you have to make sure items are always in a particular (but arbitrary) order, it is too easy to mess things up. Maybe like:
    PHP Code:
    <?php

    $pages 
    = array(
        
    "home" => array(
            
    "title"       => "Home",
            
    "display"     => "page_content/home.php",
            
    "description" => "This is the home page!",
        ),
        
    "about" => array( /* etc. ... */ ),
        
    // etc. ...
        
    404   => array(
            
    "title"       => "Page Not Found",
            
    "display"     => "page_content/404.php",
            
    "description" => "The page you requested was not found!",
        )
    );
    Then, you could dispose of the switch statement for a simple else/if:
    PHP Code:
    <?php

    if( isset( $_GET["page"] ) && isset( $pages$_GET["page"] ] ) ){
        
    $page $_GET["page"];
    }else{
        
    $page 404;
    }
    $title       $pages[$page]["title"];
    $display     $pages[$page]["display"];
    $description $pages[$page]["description"];

  3. #3
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you. Sorry for the delayed reply, that worked! The only thing left I need to do is set a default "home" page if no "page" is specified.

    IE. domain.com/ or domain.com/index.php should by default display "page_content/home.php" since the user didn't attempt to reach a page domain.

    I've been trying to work with it, and can't seem to get it.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    So, you want $page to default to "home" if it is not specified, correct?

    Try describing what you want in "psuedo"-code terms:
    Code:
    did user request a page?
        yes: does the requested page exist?
            yes: show it
            no:  show a 404 page
        no: show the home page
    Once you have your plan, it's very easy to translate it into actual code:
    PHP Code:
    <?php

    // did user request a page?
    if( isset( $_GET["page"] ) ){
        
    // yes: does the requested page exist?
        
    if( isset( $pages$_GET["page"] ] ) ){
            
    // yes: show it
            
    $page $_GET["page"];
        else{
            
    // no:  show a 404 page
            
    $page 404;
        }
    }else{
        
    // no: show the home page
        
    $page "home";
    }

  5. The Following User Says Thank You to traq For This Useful Post:

    ?foru (03-08-2014)

  6. #5
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you for helping to think out the code which helped to visualize it as code. I was getting ELSE parse errors, but after some thought I was able to set the default with the following. I appreciate your help!

    if( !isset( $_GET["page"] ) && !isset( $pages[ $_GET["page"] ] ) ){
    $page = home;
    }

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ?foru View Post
    Thank you for helping to think out the code which helped to visualize it as code. I was getting ELSE parse errors, but after some thought I was able to set the default with the following. I appreciate your help!
    PHP Code:
    if( !isset( $_GET["page"] ) && !isset( $pages$_GET["page"] ] ) ) 
    You're welcome. Think about that code for a minute, though: If $_GET['page'] does not exist, then is it even possible for it to be an index in your $pages array?

    Checking both conditions doesn't make sense:
    • If the first condition is true, the second is always true also.
    • If the first condition is false, then it doesn't matter if the second condition is true or not (in fact, PHP doesn't even bother checking).


    If you share the code that was giving you a parse error, we can probably figure out why and fix it.

  8. The Following User Says Thank You to traq For This Useful Post:

    ?foru (03-08-2014)

  9. #7
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Resolved

    You're right, and your explanation makes since...thank you! I was able to track down the parse error in the code posted in #4, and the error was throwing due to a missing } prior to the first else. It stated line 27...so I looked above and below that line and caught the error.

    PHP Code:
    // did user request a page?
    if( isset( $_GET["page"] ) ){
        
    // yes: does the requested page exist?
        
    if( isset( $pages$_GET["page"] ] ) ){
            
    // yes: show it
            
    $page $_GET["page"];
    //added }

        
    }else{
            
    // no:  show a 404 page
            
    $page 404;
        }
    }else{
        
    // no: show the home page
        
    $page "home";


    If it helps anyone else, here is the finalized code.
    PHP Code:
    <?php
    $pages 
    = array(
        
    "home" => array(
            
    "title"       => "Home",
            
    "display"     => "page_content/home.php",
            
    "description" => "This is the home page!",
        ),
        
    "administration" => array(
            
    "title"       => "Administration",
            
    "display"     => "page_content/administration.php",
            
    "description" => "This is the administration page!",
        ),
        
    "404" => array( /* etc. ... */ ),
        
    // etc. ...
        
    404   => array(
            
    "title"       => "Page Not Found",
            
    "display"     => "page_content/404.php",
            
    "description" => "The page you requested was not found!",
        )
    );
    // did user request a page?
    if( isset( $_GET["page"] ) ){
        
    // yes: does the requested page exist?
        
    if( isset( $pages$_GET["page"] ] ) ){
            
    // yes: show it
            
    $page $_GET["page"]; 
        }else{
            
    // no:  show a 404 page
            
    $page 404;
        }
    }else{
        
    // no: show the home page
        
    $page "home";
    }
    $title       $pages[$page]["title"];
    $display     $pages[$page]["display"];
    $description $pages[$page]["description"];
    ?>

  10. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ?foru View Post
    You're right, and your explanation makes since...thank you! I was able to track down the parse error in the code posted in #4, and the error was throwing due to a missing } prior to the first else. It stated line 27...so I looked above and below that line and caught the error.
    Great, glad you got it working!

Similar Threads

  1. Replies: 6
    Last Post: 09-08-2013, 01:02 AM
  2. Switch Content Script - Problems with option boxes
    By MydKnight in forum Dynamic Drive scripts help
    Replies: 10
    Last Post: 06-27-2008, 06:30 PM
  3. URL not found Page
    By Jas in forum Looking for such a script or service
    Replies: 7
    Last Post: 04-07-2007, 10:41 PM
  4. Switch Menu II - searched & found, still not working!!
    By sirhexx in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 04-15-2006, 08:36 PM
  5. Switch Content Script II: Need collapse previous option from ver 1
    By ethereal in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-23-2006, 06:38 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •