Sprinkling PHP within an HTML Document PHP

The first and most obvious approach to using PHP is to build HTML files as you have always done, inserting PHP tags as if they were HTML tags. This could take the form of repeating HTML that you replace with a call to a PHP function. It could take the form of a large block of PHP code that generates database output. Or it could be a script that processes a form submission. These are all situations where the impact of PHP on the site is slight. This is a good first step for those new to programming. You are able to insert thesmallest amount of PHP code as a test. As your experience and confidence grow, so willyour reliance on PHP.

Let's examine creating a PHP function to replace repeating HTML. One great aspect of cascading style sheets is that they allow you to redefine how tags behave. Unfortunately, this technology is available only in the newest versions of Navigator and Internet Explorer. You can provide similar functionality on the server side with PHP. Suppose we would like all of our headers to be in uppercase letters, bold, size 7, and blue. The solution is to write a function that takes a string and prints it in the proper format.

I've created a function called PrintTitle. This function wraps a given string in HTML tags. In some ways the code is more readable than if I had simply written it as static HTML, because each time there is a title, we see the call to the PrintTitle function. This may have more meaning than the collection of tags for which it stands. This is one of the benefits of functions in general: they wrap up functionality into a single name.

Another benefit is that I can be sure each title will be rendered in exactly the same way. Less text to type for each title means less chance of leaving out part of the formula. This is nice to the programmer, who undoubtedly is eager to find a shortcut to typing long segments of identical HTML. A higher degree of quality is ensured. If a call to the function is mistyped, PHP will display an error. If no errors are displayed, the titles are most likely displayed identically and in the correct format. If the title turns out to need changing, the code must be altered in only one place. This is a good antidote to the painful phrase, "I changed my mind...."

Another similar use of PHP is to dress up what is essentially CGI output: a large block of PHP surrounded by HTML so that the output of the code simply appears in a larger page. This is a similar approach offered by SSI (Server-Side Includes). An SSI tag may call a CGI and insert the output in its place.

The approach is appropriate in situations where your site is mostly static, but certain key areas must be dynamic. The advantage is very low impact on the Web server. PHP is used only when absolutely needed. In Listing 20.2 the code generates information that doesn't change, but it's easy to imagine code that pulls stock quotes from a database. It eliminates the need to edit the HTML page each time the information changes, but parts that don't change often, like the layout of the page, are left as static HTML.

While this is an example of dynamic output, you are often faced with the opposite situation. Your site may be completely static, but you need to accept catalog requests. PHP is a good solution for accepting form submissions. The first step is to create an HTML page that asks for name and address.

Formatting Function

<?
function PrintTitle($title)
{
print("<CENTER>");
print("<FONT COLOR="#0000FF" SIZE="5">");
print("<B>");
print(strtoupper($title));
print("</B>");
print("</FONT>");
print("</CENTER> ");
}
?>
<HTML>
<HEAD>
<TITLE>Listing 20.1</TITLE>
<HEAD>
<BODY>
<? PrintTitle("Listing 20.1"); ?>
This is an example of using a function to repeat
a commonly-used piece of HTML code.<BR>
<BR>
<? PrintTitle("how it works"); ?>
Any time a title needs to be created, the
<CODE>PrintTitle</CODE>
function is called with the text of the title.<BR>
<BR>
<? PrintTitle("advantages"); ?>
The code is more readable<BR>
Less text to type for each title<BR>
Easy to change every title<BR>
</BODY>
</HTML>

The page is a very simple submission form. Each of the input tags will be turned into a PHP variable when the submit button is clicked. A file named req.txt will be opened for appending, and each of the form fields will be written into the file. Each field is separated by tab characters, which allows you to import the file into a spreadsheet easily.

Dressing Up CGI Output

<HTML>
<HEAD>
<TITLE>Listing 20.2</TITLE>
</HEAD>
<BODY>
<H1>Color Chart</H1>

The following chart displays the colors safe for displaying in all browsers. These colors should not dither on any computer with a color palette of at least 256 colors. This chart will only display on browsers that support table cell background colors.

<?
$color = array("00", "33", "66", "99", "CC", "FF");
for($Red = 0; $Red count <($color); $Red++)
{
print("<TABLE> ");
for($Green = 0; $Green count <($color);$Green++)
{
print("<TR> ");
for($Blue = 0; $Blue count <($color);
$Blue++)
{
$CellColor = $color[$Red] .
$color[$Green] .
$color[$Blue];
print("<TD
BGCOLOR="#$CellColor">");
print("<TT>$CellColor/TT>");
print("</TD> ");
}
print("</TR> ");
}
print("</TABLE> ");
}
?>
</BODY>
</HTML>

Catalog Request Form

HTML>
<HEAD>
<TITLE>Listing 20.3</TITLE>
</HEAD>
<BODY>
Please enter name and address to receive a free catalog.
<FORM ACTION="20-4.php">
<TABLE>
<TR>
<TD>Name</TD>
<TD>INPUT TYPE="text" NAME="InputName"></TD>
</TR>
<TR>
<TD>Address</TD>
<TD>INPUT TYPE="text" NAME="InputAddress"></TD>
</TR>
<TR><TD>City</TD>
<TD>INPUT TYPE="text" NAME="InputCity"></TD>
</TR>
<TR>
<TD>State</TD>
<TD>INPUT TYPE="text" NAME="InputState"></TD>
</TR>
<TR>
<TD>ZIP</TD>
<TD>INPUT TYPE="text" NAME="InputZIP"></TD>
</TR>
<TR>
<TD>INPUT TYPE="reset"></TD>
<TD>INPUT TYPE="submit"></TD>
</TR>
</TABLE>
</FORM>
</BOY>
</HTML>

Form Submission

<HTML>
<HEAD>
<TITLE>Listing</TITLE>
</HEAD>
<BODY>
<?
*
* rocess form input, append it to file
/
CatalogRequests = fopen("req.txt", "a");
if($CatalogRequests)
{
fputs($CatalogRequests, "$InputName ");
fputs($CatalogRequests,
"$InputAddress ");
fputs($CatalogRequests, "$InputCity ");
fputs($CatalogRequests,
"$InputState ");
fputs($CatalogRequests, "$InputZIP ");
fclose($CatalogRequests);
}
?> Thank you for your catalog request!<BR>
<BR>
<A HREF="20-3.html">Return to Site</A><BR>
</BODY>
</HTML>

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics