[php] load a default case if none given?

sean.ferguson

OSNN Veteran Addict
Joined
4 Jul 2003
Messages
1,693
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 :)
 
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
}
?>
 
that works a treat feggy, thanks :) actually makes sense to me too which is rare :p
 
nix said:
I got this code a while back from SPeedY_B...
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;
}
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back