Delving deeper into Webzine authoring and delivery
As a language for building dynamic Web pages, PHP offers a simplified method for constructing complex and powerful Web-related programs. Step by step, Erik demonstrates the fundamental principles of PHP in an original real-world Web site example. In Part 2 of this series, he shows you how the delivery module presents a menu of stories to the reader, and how the authoring module permits authors to submit stories to a Webzine.
If you're new to PHP, you might be pleasantly surprised by how easy it is in practice. My intention is for you to come away with a good feel for what it's like to work in PHP; after that, you'll be able to decide whether or not it's right for you.
In Part 1 of this article, I covered the delivery portion of a simple PHP Webzine application. Although it's only about 3K of code, it packs a lot of functionality. You got a chance to try out the application and begin looking under the hood. I explained how the application displays category menus to users, and presents stories upon selection. I also gave a feel for how PHP applications work and how they accept parameters from the calling page.
Here in Part 2, you'll see how the delivery module presents a menu of stories to the reader, and then take a quick look at the authoring module, which permits authors to submit a story to the Webzine.
Menu of stories
To illustrate, a topic menu file (TradeShow.txt) containing just three stories might look like the following:
The menu driver takes this information and presents a story overview to the reader. It works like this:
- First, the driver copies the appropriate number of entries from the topic menu file into an array $stories . Using the above example, $stories contains the first line (story number 33), $stories contains story number 12, and $stories contains story number 5. You also need to count the number of elements in the array and use a variable $numstories to contain the result.
- Next, the driver presents the information to the user as follows:
split function copies the contents of the story information
from a string into an array. Note that the caret becomes "escaped" by placing
a backslash in front of it. The program transfers the array nodes into
more descriptive variable names, making the code easier to follow. Then,
construction of the URL occurs. The
if/else structure handles
the first 10 stories differently from the rest. The first 10 have
a dividing line, use the H2 tag, and include a photo if available.
The rest use the H3 tag, with no dividing line and no photo. This process
highlights the newest stories, and maintains older ones for those
readers who want them. Note the right-alignment of the
odd-numbered images and the left-alignment of the even-numbered images.
To wind up this example, the resulting HTML source for theTradeShow.txt file above might appear as follows:
<hr><p><img align=left src="/images/proddemo.jpg"></p>
<h2><a href="index.php3?topic=TradeShow&story=33">Great New Products This Year</a><br></h2>
<p>Thursday's product demo included a couple of exciting new
surprises from competing firms in the industry.</p>