Results 1 to 3 of 3

Thread: Quick question regarding "case" in PHP.

  1. #1
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Quick question regarding "case" in PHP.

    Hello all,

    I've built a small PHP script for my website which consolidates about 10 different pages into one. Let me explain.

    The code structure is a little like this:

    HTML Code for page up until content is required
    PHP inserts content here depending on which page was requested
    Footer HTML

    I've done this using the CASE function in PHP, and at present it looks like this:

    PHP Code:
    <?php
    // Start PHP script.
    switch($_SERVER['QUERY_STRING']) {

    case 
    'pageone':
    echo 
    "content for page one";
    break;

    case 
    'pagetwo':
    echo 
    "content for page two";
    break;

    default:
    echo 
    "normal index";
    }
    So a request for the page looks like:
    index.php?pageone
    And if there's any sort of typing error, such as
    index.php?page one
    It will serve up the "default" page.


    However, this isn't exactly what I want to happen.
    I'd prefer that when a typo is hit, that some content is served stating that there was an error in the request, rather than just serving the default page.

    Is this possible? I'm rather new to PHP.

    (My current PHP is www.gerryhyh.f2s.com/paul/content/index.php, and the code can be found at http://www.gerryhyh.f2s.com/paul/content/php.txt (which I know is completely uncompliant. I'm working on that )

    Regards,
    cr3ative
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  2. #2
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I've made a semi-bodged workaround for now:

    I've set case '': to be my index page, and default: to be an error page.

    cr3ative
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by cr3ative
    I've made a semi-bodged workaround for now:

    I've set case '': to be my index page, and default: to be an error page.
    That's all I was going to suggest. You've defined clauses for all expected cases, and treat other cases as errors. Seems reasonable to me.

    Mike

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
  •