[Tutorial] Switching Pages With Variables

Example of PHP code.

Moderator: Moderators

[Tutorial] Switching Pages With Variables

Postby Phantom on Wed May 11, 2005 9:14 pm

Side-Note: This small code utilizes the switch function and the break function in a control structure.

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;
}
?>
Last edited by Phantom on Wed May 11, 2005 9:22 pm, edited 1 time in total.
"Best of the best of the best, sir! With honors!" -- MIB
[ jphantom.com ]
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am

Postby PostBot on Wed May 11, 2005 9:21 pm

For php5 compatibility I suggest to use $_GET instead of $HTTP_GET_VARS
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby Phantom on Wed May 11, 2005 9:23 pm

[quote user="CyberAlien" post="33364"]For php5 compatibility I suggest to use $_GET instead of $HTTP_GET_VARS[/quote]
Thanks, I fixed it.
"Best of the best of the best, sir! With honors!" -- MIB
[ jphantom.com ]
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am

Re: [Tutorial] Switching Pages With Variables

Postby evans11 on Thu Mar 12, 2009 1:02 pm

Thanks for sharing this information.

:)
evans11
Registered User
Registered User
 
Posts: 9
Joined: Fri Feb 06, 2009 9:01 pm


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron