Grow your CSS skills. Land your dream job.

PHP Switch/Case and the Dreaded Unclean URLs

  • # March 19, 2013 at 6:14 pm

    The menu can’t be simple html, else you wouldn’t get parse errors.

    # March 19, 2013 at 6:14 pm

    Yeah. I tried the above php method, but it didn’t do anything. It just cause some php perrors. I can try it again…

    # March 19, 2013 at 6:15 pm

    By the way, I’m surprised this hasn’t come up already…

    I assume you will have a root variable for your site’s root directory (I’ll call it “$root”):

    < ?php
    if( !isset( $_GET ) )
    $page = ‘home';
    else
    $page = $_GET;
    if( file_exists( $root . ‘theme/default/pages/’ . $page . ‘.php’ ) )
    include( ‘theme/default/pages/’ . $page . ‘.php’ );
    else
    include( ‘theme/default/pages/404.php’ );

    # March 19, 2013 at 6:20 pm

    Oh, I see the problem… I need to specify that the default index page uses home.php

    # March 19, 2013 at 6:27 pm

    Ok, so my default page is ‘home’, so how would this be written? The above code you gave didn’t change anything.

    # March 19, 2013 at 6:41 pm

    When going directly to http://rpotterjr.dev I get errors. If I go to http://rpotterjr.dev/home I no longer have any errors. Any suggestions?

    # March 19, 2013 at 7:15 pm

    It seems that I need to set the default page within the navigation, which should be ‘home’. Anyone know how to achieve this?

    http://pastebin.com/rnjeLb1J

    # March 19, 2013 at 7:32 pm

    [http://pastebin.com/aWcuKks0](http://pastebin.com/aWcuKks0 “http://pastebin.com/aWcuKks0″)

    My suggestions seem to be following a theme. If you’re still having difficulties, I’m happy to put together a working zip file.

    # March 19, 2013 at 8:09 pm

    Here’s a zip file anyways:

    [https://dl.dropbox.com/u/2477024/rpotterjr.zip](https://dl.dropbox.com/u/2477024/rpotterjr.zip “https://dl.dropbox.com/u/2477024/rpotterjr.zip”)

    I used a sub-folder rather fiddling with my hosts, so the htaccess file has a line reading:

    RewriteBase /rpotterjr/

    You will either need to unzip the folder to:

    127.0.0.1/rpotterjr

    or just change the line to:

    RewriteBase /

    # March 20, 2013 at 2:53 am

    It is saying ‘Unidentified Index Page…” It’s saying that there is no identified index page in navigation.php?

    http://pastebin.com/tRbLzd13

    # March 20, 2013 at 6:45 am

    Add `$page = isset($_GET) ? $_GET : ‘home';` (if you haven’t declared `$page` somewhere else already) before the list and replace all the `$_GET` with `$page` inside the list.

    # March 20, 2013 at 12:01 pm

    Thanks again man, you definitely rock! Thanks goes out to BenWalker also, for contributing to solving this issue.

    # March 20, 2013 at 12:24 pm

    One more thing, in your switch you default to your home page. I would default to a 404 error page like this:

    $page = isset($_GET) ? $_GET : ‘home';

    // include header and menu here (so they have access to $page)

    switch ($page) {
    case ‘home':
    include(‘mydir/home.php’);
    break;
    case ‘about':
    include(‘mydir/about.php’);
    break;
    case ‘contact':
    include(‘mydir/contact.php’);
    break;
    default:
    include(‘mydir/404.php’);
    break;
    }

    // include footer here

    # March 20, 2013 at 1:28 pm

    Yeah, I plan to do that before completion of the website. I have a 404 template that I use for all of my websites that I usually impliment before I put the site live. Thanks for everything!

Viewing 15 posts - 46 through 60 (of 70 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".