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


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

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

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.

Introduction

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:

Great New Products This Year^/images/proddemo.jpg^Thursday's product demo ...
12^Opening Event Well Attended^/images/opnfoto.jpg^Ticket sales to the ...
5^Trade Show Opens^/images/tradelogo.gif^The Fourth Annual Trade Show ...

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[0] contains the first line (story number 33), $stories[1] contains story number 12, and $stories[2] 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:

for ($i=0; $i<$numstories; $i++) {
   $storyinfo = split("\^", $stories[$i]);
   $storynum = $storyinfo[0];   // Story number (e.g. 33)
   $storydesc = $storyinfo[1];  // Story Title (e.g. "Great New Products This Year").
   $storyimg = $storyinfo[2];   // Image URL (e.g. "/images/proddemo.jpg").
   $dtext = $storyinfo[3];      // Story Synopsis (longer text description).
   $url = "<a href=\"index.php3?topic=$topic&story=$storynum\">$storydesc</a><br>";
   if ($i<10) {
      $url = "<h2>" . $url . "</h2>";
      if ($storyimg != "") {
         $url = "<p><img align=" . ($i%2==1 ? "right" : "left")
              . " src=\"$storyimg\"></p>\n"
              . $url;
      }
      $url = "<hr>" . $url;
   } else {
      $url = "<h3>" . $url . "</h3>";
   }
   echo("$url\n");
   echo("<p>$dtext</p>\n");
}

The 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>

<hr><p><img align=right src="/images/opnfoto.jpg"></p> <h2><a href="index.php3?topic=TradeShow&story=12">Opening Event Well Attended</a><br></h2> <p>Ticket sales to the Trade Show opening event were up 15% from last year's show.</p>

<hr><p><img align=left src="/images/tradelogo.gif"></p> <h2><a href="index.php3?topic=TradeShow&story=5">Trade Show Opens</a><br></h2> <p>The Fourth Annual Trade Show opened 10/11/2000 to an enthusiastic reception from attendees.</p>


View PHP by example: Part 2 Discussion

Page:  1 2 3 Next Page: Authoring page "author.php3"

First published by IBM developerWorks


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