[php] load a default case if none given?

Discussion in 'Web Design & Coding' started by sean.ferguson, Aug 4, 2005.

  1. sean.ferguson

    sean.ferguson Moderator Folding Team

    Messages:
    1,693
    Location:
    Fife; Scotland
    I have the following code to handle my pages and content:

    PHP:
    <?php 
    switch($_GET[p]) { 
      case 
    test
      include (
    "test.html"); break; 
      default: 
    // Error checking 
      
    print "<b>ERROR</b> 
      <br />\"
    $p\" Does not exist. 
      <br /> 
      <br />You may have entered an incorrect URL. 
      <br />Please refer back to the <a href=\"?p=test\">Index</a> and try again. 
    "

    break; 

    ?>
    But this will only work correctly if I give a ?p=something after the initial index.php. I know that there is a statement that gets put in the header of the php file so that if no ?p= statement is given it loads a default, in this case index.php?p=test. I got this code a while back from SPeedY_B so perhaps he will be able to help, but any is appreciated :)
     
  2. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    PHP:
    <?php
    if (!empty($_GET['p']))
    {
        switch (
    $_GET['p'])
        {
            case 
    'test': include "test.html"; break;
            case 
    'test2': include "test2.html"; break;
            default: 
    $out "<strong>Error</strong>\n";
                
    $out .= "<br />\"$p\" Does not exist. \n";
                
    $out .= "<br />\n";
                
    $out .= "<br />You may have entered an incorrect URL. \n";
                
    $out .= "<br />Please refer back to the <a href=\"?p=test\">Index</a> and try again.";
                print 
    $out;
                break;
        }
    } else {
        include 
    "default.html";
        
    // or you could
        //print "<b>ERROR</b> 
        //  <br />You have entered an URL which does not exist 
        //  <br />Please refer back to the <a href=\"?p=test\">Index</a> and try again."; 
        // again so that they know they should use something else
    }
    ?>
     
    sean.ferguson likes this.
  3. sean.ferguson

    sean.ferguson Moderator Folding Team

    Messages:
    1,693
    Location:
    Fife; Scotland
    that works a treat feggy, thanks :) actually makes sense to me too which is rare :p
     
  4. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    yay
     
  5. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    Thief!!! :p :D

    Personally, I just use a NULL case.
    PHP:
    <?php
    switch ($_GET['p']) {
        case 
    moose
        include 
    "moose.html"
        break;

        case 
    NULL
        include 
    "default.html";
        break;

        default: 
        echo 
    "OH NOES! \n
        <br />You broke it!"
    ;
        break;
    }
     
  6. sean.ferguson

    sean.ferguson Moderator Folding Team

    Messages:
    1,693
    Location:
    Fife; Scotland
    t'was you who gave it :rolleyes: but that also is helpful, thanks :)