![]() |
Site Archive (Complete) | |||
|
ABOUT US |
CONTACT |
ADVERTISE |
SUBSCRIBE |
SOURCE CODE |
CURRENT PRINT ISSUE |
NEWSLETTERS
|
RESOURCES
|
BLOGS
|
PODCASTS
|
CAREERS
|
||||
January 01, 2002
Template-Based Web Sites as Easy as P-H-PJason Gilmore
PHP makes our lives easier. We began our exploration into the good life with What You Should Know About PHP. In that article I focused mainly on PHP concepts with a little script thrown in for fun. Now we'll delve deeper into the power of PHP and cover several strategies that will help you quickly set up a site that's easy to maintain and update.
To keep things interesting, we'll create a hypothetical movie database to serve as our example. This database will have an introductory screen displaying a list of movies. Each list item will be PHP-powered so that a simple click on any title will reveal detailed information about the movie. What's beautiful about this example is that the movie information is actually being inserted into a template that acts in part to preserve a uniform environment, and in part to allow the developer to quickly modify the look and feel of the site when necessary. Let's begin by listing the files that we will use throughout the example:
header.incheader.inc will provide the head information for the page being served. This includes the opening HTML tags, the page title, and BODY information. The extension '.inc' is used to specify files that are included within our template. This is also the file in which you could designate global variables and create PHP functions that will be used later on.
<? // file: header.inc // purpose: supplies head information to documents making up our site. // created: February 11, 2000 ?> <HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY BACKGROUND="" BGCOLOR="#ffffff" TEXT="#000000" LINK="#808000" VLINK="#000000" ALINK="#606060"> footer.incfooter.inc will provide the end information for the page being served. This includes the copyright information and closing HTML tags.
<?
// file: footer.inc
// purpose: supplies footer information to documents making up our site.
// created: February 11, 2000
?>
Copyright © 2000 | <A href = 'mailto:derrick@webreview.com'>Contact us</A>
| <A href = 'template.php3'>Home</A>
</BODY>
</HTML>
Our sample movie entriesWe'll use these three sample movie entries to practice with our PHP script: Scarface.incMovie Name: Scarface<BR> Year: 1983<BR> Starring: Al Pacino, Michelle Pfeiffer, Mary Elizabeth Mastrantonio<BR> Summary: The rise of Cuban immigrant Tony Montana.Staying_Alive.inc Movie Name: Staying Alive<BR> Year: 1983<BR> Starring: John Travolta<BR> Summary: Dancer Tony Manero tries to make it on Broadway.Saturday_Night_Fever.inc Movie Name: Saturday Night Fever<BR> Year: 1977<BR> Starring: John Travolta<BR> Summary: Teenager Tony Manero struts his stuff on the dance floor. template.php3template.php3 acts as the brains of our site. As you will see, we will pass variables into this script through hyperlinks, controlling the information that will be viewed by the reader. This template is a very basic example, but will more than suffice to show the general mechanics of template-driven sites.<? // file: template.php3 // purpose: central brains for the site // created: February 11, 2000 // include header information INCLUDE "header.inc"; print " <table width = 100% border = 0> <tr>"; // The following cell will hold the move list for the site. print "<td valign=top> Choose your movie:<BR> <UL> <LI><A HREF = 'template.php3?topic=Saturday_Night_Fever'>Saturday Night Fever</A> <LI><A HREF = 'template.php3?topic=Staying_Alive'>Staying Alive</A> <LI><A HREF = 'template.php3?topic=Scarface'>Scarface</A> </UL> </td>"; // The following cell will include the information requested by the user. print "<td valign = top>"; // If no topic selected, display the default content. IF ($topic == '') : print "Welcome to WebReview's movie database."; // Else the user clicked on a link. // Include content related to that link. ELSE : INCLUDE "$topic.inc"; ENDIF; print "</td></tr></table>"; // Include footer information INCLUDE "footer.inc"; ?>
Even easier...
template.php3 (revised)To make this work correctly, place the following code in between the <UL>...</UL> statements in the original template.php3 file.
// Open a link to the current directory.
$dir_handle = opendir('.');
// While still files in directory left to look at
while ($file = readdir($dir_handle)) :
// Make sure the file is a movie review
if ($file != "." && $file != ".." &&
$file != "header.inc" &&
$file != "footer.inc" &&
$file != "template.php3") :
// Replace each underscore ('_') with a blank space.
$name = ereg_replace("_", " ", $file);
// Strip off the .inc from the file name.
$name = ereg_replace(".inc", "", $name);
// Strip off the .inc from the file.
$file = ereg_replace(".inc", "", $file);
// Print link and file name.
print "<LI>
<A HREF = 'template.php3?topic=$file'>$name</A>";
endif;
endwhile;
// close the directory
closedir($dir_handle);
This script may seem a little intense at first, but it's really quite straightforward. Let's break it down step-by-step:
Again, the beauty of this script is that any addition/deletion of movie reviews is automatically recognized by the script the next time it is loaded to the browser. This prevents the possibility of broken links, thereby freeing you to concentrate on content for the site!
I hope you have time to play with these scripts, as doing actual coding is the best way to learn a language quickly. Furthermore, the examples provided within this article are just one way to accomplish this task. It is important to remember that PHP is very flexible; there are almost always several ways to accomplish the same thing, so experiment, read the manual, and above all, have fun doing it!
|
|
|||||||||||||||||||||||||||||
|
|