IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
      
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks > Web architecture >
developerWorks
PHP by example, Part I
e-mail it!
Subscriptions:
dW newsletters
dW Subscription
(CDs and downloads)
Effortless Webzine authoring and delivery

Erik Zoltan (erik@zoltan.org)
Advanced Systems Engineer, EDS
01 Dec 2000

This sample code is from the article which offers the basics of PHP and features a Webzine that includes an author's page where content providers can enter the text of articles, as well as a front end for presenting this content to the world.

<?php
   // Initializations and default values.
   $title = "PHP Demo Webzine";
   $slogan = "Illustrating the coolness of PHP since September 2000.";
   $cats = file("category.txt");
   $elems = count($cats);
   if (!isset($topic)) $topic="Main";
   $topicfile = "$topic.txt";
   if (!isset($base)) $base=0;
   if (!isset($numdesc)) $numdesc= ($topic=="Main") ? 10 : 5;
   if (!isset($numlist)) $numlist=40;
   if (!isset($window)) $window = $base < $numdesc ? $numlist-($base*4) : $numlist;
?>
<html>
 <head>
  <title><?php echo($title) ?></title>
 </head>
 <body bgcolor=white vlink="#000080" link="#000080">
  <h1><?php echo($title) ?></h1>
  <p><i><?php echo($slogan) ?></i></p>
  <h3>
   <a href="author.php3">Submit a Story!</a>
  </h3>
  <table><tr><td valign=top>
    <table border=1>
<?php
     // Menu items on left of page.
     for ($i=0; $i<$elems; $i++) {
        $item = trim($cats[$i]);
        $ifile = ereg_replace(" ","",$item);
        $color = ($ifile == $topic) ? "pink" : "silver";
        $url = "index.php3?topic=$ifile";
        $anchor = " " . ($item != $topic ? "<a href=\"$url\">$item</a>" : "$item") . " ";
        echo ("     <tr><td bgcolor=\"$color\"><center><b>$anchor</b></center></td></tr>\n");
     }
?>
    </table>
   </td><td><p>
    <?php
    if (!isset($story)) {
       // No story selected, show them the list in this category.
       if (file_exists($topicfile)) {
          $stories = file($topicfile);
          $numstories = count($stories);
       } else {
          $stories[0] = "0^Submit a Story^^No stories have been written in this "
                      . "topic yet.  Click \"Submit a Story\" to learn more.";
          $numstories = 1;
       }
       if ($numstories>($numlist+$base))
          $numstories = $window+$base;
       for ($i=$base; $i<$numstories; $i++) {
          $storyinfo = split("\^", $stories[$i]);
          $storynum = $storyinfo[0];
          $storydesc = $storyinfo[1];
          $storyimg = $storyinfo[2];
          $dtext = $storyinfo[3];
          $url = "<a href=\"index.php3?topic=$topic&story=$storynum\">$storydesc</a><br>";
          if ($i<$numdesc) {
             $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<p>");
       }
    } else {
       // Story selected, show them the story.
       $storyfile = fopen("s$story.txt","r");
       fpassthru($storyfile);
       //echo ("</p>");
    }
    ?>
  </p></td></tr></table>
 </body>
</html>



e-mail it!

developerWorks > Web architecture >
developerWorks
  About IBM  |  Privacy  |  Terms of use  |  Contact