Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > PHP BY EXAMPLE: PART 1


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

PHP by example: Part 1
By Erik Zoltan - 2004-06-24 Page:  1 2 3 4

A special PHP technique

Before I get into the actual Webzine code, let's start with a simple example that nicely illustrates the character of PHP. The syntax of PHP allows you to intermix HTML and PHP statements at will. This means that the HTML statements can appear inside the context of loops, if/else statements, functions, and so on. I do this in the Webzine program, but the following code sample boils it down in a simpler way.

Suppose we have the two arrays, $names and $days, containing information about the months of the year, so $days[0] = 31 and $names[0] = "January", $days[1] equals 28 and $names[1] equals "February", and so on. Here's a technique to create a table of day and month names:

Listing 1: Creating a table of day and month names


<table border="2">
<tr><th>Name</th><th>Days</th></tr>

<?php
   for($i=0; $i<12; $i++) { // Start of loop.
?>
<tr><td><?php
 echo($names[$i]) 
?></td>
    <td><?php
 echo($days[$i]) 


?></td></tr>
<?php
   } // End of for loop.
?>
</table>    

For clarity, the PHP statements above are in red; the HTML statements are in black. Note that the special tag <?php switches from HTML into PHP, and ?> switches back to HTML.

The important thing to note is that you can switch into PHP, start a for loop (or if/else, or switch, or whatever), then pop back into HTML and now the HTML commands you're entering will be part of the looping structure so they repeat along with the loop. You can go into and out of PHP mode as necessary (to issue echo statements that output variables into the Web page as I've done above, for example). Then, when you pop back into PHP and put an ending curly brace, the loop is complete, just as you'd expect.

Here's what the actual table would look like:

NameDays
January31
February28
March31
April30
May31
June30
July31
August31
September30
October31
November30
December31

If this behavior seems confusing, just think of it in the following way: The PHP interpreter will replace each line in HTML mode with an echo statement to put that line into the output stream. If that echo statement occurs in an if/else structure, it will be conditional. If it occurs within a loop (as above), then it repeats.

Application overview

The Webzine driver, index.php3, has three major components (see here for a complete listing of the code for the driver): subject menu, story list, and full story presentation. There is also some default text that appears if the reader selects a topic that doesn't have any stories. The authoring page (view listing) is more complex. It contains a form to accept user input, a feedback message to inform the author of problems that need correcting, and a confirmation message to show the author what they submitted. It also knows how to validate the story submitted, make changes to ensure that it doesn't contain any unauthorized HTML, save it in a story file, and update the appropriate menu files that go with the story.

This application has three kinds of data files: Category.txt contains a simple list of the topics where the organization of stories occurs. Each topic associates with a topic menu file. The first topic must be "Main," and associates with a topic menu file Main.txt. If the second topic is "The Arts," then it associates with a topic menu file called TheArts.txt. The menu files contain information about each story on a separate line: a story number, title, category, brief synopsis and, optionally, an image URL. Finally, the story file contains the actual body text of one story. The file s1.txt would contain the first submitted story, s2.txt the second, and so on. If you know the number of a story (say number 26), its filename is easy to determine (s26.txt).

Try it out!

Before you look at this application in detail, try it out.

Try the Webzine Driver. It presents you with a list of subjects on the left, and a list of stories on the right. You can select a topic, or "Main" to see all of the stories. A list of the most recent stories appears first, and images display for the first couple of stories that have images. When you click on the title of a story, it takes you to a page featuring the full text of that story.

Try the Authoring Page (or you can try it from within the Webzine itself). It presents you with a form that allows you to submit a story. Use common sense and good taste when submitting content to the Webzine. If you type something the program doesn't like, it will give you an error message. Upon acceptance of your story, you can return to the Webzine to see how it appears to the reader.

Now that you've tried out the application, continue reading to learn about its creation.



View PHP by example: Part 1 Discussion

Page:  1 2 3 4 Next Page: Webzine driver

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.