• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[php] load a default case if none given?

#1
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
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
}
?>
 

SPeedY_B

I may actually be insane.
#5
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 posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,013
Latest member
Pdawgintown