by Kevin Yank of SitePoint.com
Multi-Purpose Pages
Let's say you wanted to construct your site so that it showed the visitor's name at the top of every page. With our custom welcome message example above, we're halfway there already. Here are the problems we'll need to overcome to extend the example into what we need:
The first problem isn't too hard to overcome. Once we have the user's name in a variable on one page, we can pass it with any request to another page by adding the name to the query string of all links:
<A HREF="newpage.php?name=<?php echo(urlencode($name)); ?>"> A link </A>
Notice that we've embedded PHP code right in the middle of an HTML tag. This is perfectly legal, and will work just fine. We're familiar with the echo
function, but urlencode
is new. What this does is take any special characters in the string (spaces, for example) and converts them to the special codes needed for them to appear in the query string. For example, if the $name
variable had a value of "Kevin Yank"
, then since spaces are not allowed in the query string, the output of urlencode
(and thus the string output by echo) would be "Kevin+Yank"
, which would then be automatically converted back when creating the $name
variable in newpage.php
.
Okay, so we've got the user's name being passed with every link in our site. Now all we need is to get that name in the first place. In our welcome message example, we had a special HTML page with a form in it that prompted the user for his or her name. The problem with this (identified by the second point above) is that we can't -- nor would we wish to -- force the user to enter our Web site by that page every time he or she visits our site.
The solution is to have every page of our site check to see if a name has been specified, and prompt the user for a name if necessary. This means that every page of our site will either display its content or a prompt to enter a name depending on whether the $name
variable is found to have a value. If this is beginning to sound to you like a good place for an if-else
statement, you're a quick study!
We shall refer to pages that are capable of displaying completely different content depending on some condition "multi-purpose pages". The code of a multi-purpose page looks something like this:
<HTML>
<HEAD>
<TITLE> Multi-Purpose Page Outline </TITLE>
</HEAD>
<BODY>
<?php if (<condition>) { ?>
<!-- HTML content to display if <condition> is true -->
<?php } else { ?>
<!-- HTML content to display if <condition> is false -->
<?php } ?>
</BODY>
</HTML>
This may look confusing at first, but in fact this is just a normal if-else
statement with sections of HTML code depending on the condition instead of PHP statements. This example illustrates one of the big selling points of PHP: that you can switch in and out of "PHP mode" whenever you like. Think of <?php
as the command to switching into "PHP mode", and ?>
as the command to go back into "normal HTML mode", and the above example should make perfect sense.
There is an alternate form of the if-else
statement that can make your code more readable in situations like this. Here's the outline for a multi-purpose page using the alternate if-else
form:
<HTML>
<HEAD>
<TITLE> Multi-Purpose Page Outline </TITLE>
</HEAD>
<BODY>
<?php if (<condition>): ?>
<!-- HTML content to display if <condition> is true -->
<?php else: ?>
<!-- HTML content to display if <condition> is false -->
<?php endif; ?>
</BODY>
</HTML>
Okay, so with all the tools we need in hand, let's look at a sample page of our site:
<HTML>
<HEAD>
<TITLE> Sample Page </TITLE>
</HEAD>
<BODY>
<?php if ( isset($name) ): ?>
<P>Your name: <?php echo($name); ?></P>
<P>This paragraph contains a
<A HREF="newpage.php?name=<?php echo(urlencode
($name)); ?>">link</A> that passes the
name variable on to the next document.</P>
<?php else: ?>
<!-- No name has been provided, so we
prompt the user for one. -->
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="GET">
Please enter your name: <INPUT TYPE="TEXT" NAME="name">
<INPUT TYPE="SUBMIT" VALUE="GO">
</FORM>
<?php endif; ?>
</BODY>
</HTML>
There are two new tricks in the above code, but overall you should be pretty comfortable with the way it works. First of all, we are using a new function called isset
in the condition. This function returns (outputs) a value of true if the variable it is given has been assigned a value (i.e. if a name has been provided), and false if the variable does not exist (i.e. if a name has not yet been given). The second new trick is the use of the variable $PHP_SELF
to specify the ACTION
attribute of the FORM
tag. This variable is one of several that PHP always gives a value to automatically. In particular, $PHP_SELF
will always be set to the URL of the current page. This gives us an easy way to create a form that, when submitted, will load the very same page, but this time with the $name
variable specified.
By structuring all the pages on our site in this way, visitors will be prompted for their name by the first page they attempt to view, whichever page this happens to be. Upon entering their name and clicking "GO", they will be presented with the exact page they requested. The name they entered is then passed in the query string of every link from that point onward, ensuring that they are prompted only the once.
Wrap-up
This week, we've gotten a taste of the PHP server-side scripting language by exploring all the basic language features: statements, variables, operators, and control structures. The sample applications we've seen have been pretty simple, but don't let that dissuade you. The real power of PHP is in the hundreds of built-in functions that let you do everything from accessing data in a MySQL database to sending e-mail, and from dynamically generating images to creating Adobe Acrobat PDF files on the fly.
In Part Four, we'll delve into the MySQL functions to publish the joke database that we created last week on the Web!
Part 4: Publishing MySQL Data on the Web |
SitePoint.com is a fast growing Web Developer Community. Kevin Yank is the Editor of the SitePoint TechTimes, a fresh, technically oriented newsletter for the serious Webmaster. |