Well first, you must start off with the following script:
- Code: Select all
<?PHP
if(isset($_GET['page'])) {
$p = $_GET['page'];
} else {
$p = 'index';
} switch($p) {
Did you understand all that? If not, let me explain: Basically, on line 2, it asks the server whether or not you asked for the variable "page". This can be displayed in your address bar as this: testpage.php?page=. The third line is assigning the "GET" variable to another variable $p. This is basically making it easier instead of having to retype the same long thing over again. On line 4, it tells the server that if the situation does not match the one defined on line 2, it should follow the script below it. In this case, it is line 5 and 6. Line 5 assigns it to a different variable. Then on line 6, it ends the "else" condition and tells it to switch the $p variable, which we have already found out is defined on line 3.
Now that we got the scripty part of the code done, let's continue on defining the "GET" variable "page". Using the script above, you can assign a certain case to it. This is shown below:
- Code: Select all
case 'news':
Pretty basic, it just tells the server that if page= is defined as "news", it should display the following code.
Now we could put something like this there:
- Code: Select all
print "This is the news page";
This code tells the server to display the following text. It's basically a function used to display HTML.
You can put as many lines of code there as long as a semi-colon ( ; ) is present at the end of every line.
Now, to end that case we simply type the following:
- Code: Select all
break;
That tells the server to stop reading, or else it will read the rest of your cases. Now that you know how to setup a case you can add as many as you want. We could also put something like this:
- Code: Select all
case 'contacts':
print "This is the contacts page<br />You can e-mail Phantom if <a href=\"mailto:phantom@trickingit.com\">you click here</a>.";
break;
Think you got the hang of it? Good, let's continue.
Now that we have learned all that, we still have one more thing to cover. If we ended that script with a } and ?>, it would still work. Although, if you only typed testpage.php without a variable, nothing would show up where you would have your content. This can be solved with this next defining function:
- Code: Select all
default:
Very easy, it just tells the server that if none of the cases match any defined, it should read this one. This would be your index page. You could put something like this there:
- Code: Select all
print "<a href=\"testpage.php?page=news\">News Page</a><br /><a href=\"testpage.php?page=contacts\">Contacts Page</a>";
break;
Those links will link to the specified cases that we added above. That ends the coding part of the page, and we can finish our script by typing the following:
- Code: Select all
}
?>
Congratulations! You've just learned how to switch a page's content using a variable in the URL. If we put all the lines of code that we posted above, it would turn out to a full working script:
- Code: Select all
<?PHP
if(isset($_GET['page'])) {
$p = $_GET['page'];
} else {
$p = 'index';
} switch($p) {
case 'news':
print "This is the news page";
break;
case 'contacts':
print "This is the contacts page<br />You can e-mail Phantom if <a href=\"mailto:phantom@trickingit.com\">you click here</a>.";
break;
default:
print "<a href=\"testpage.php?page=news\">News Page</a><br /><a href=\"testpage.php?page=contacts\">Contacts Page</a>";
break;
}
?>



