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

Webzine driver

Passing parameters

$topic = "TradeShow";
$story = 33;

If you omit the parameters, the $topic and $story variables will simply not exist. You can test for them explicitly or rely on PHP to return a default empty value when you reference them.

Note: If this feature doesn't seem to be working on your system, look at php.ini and make sure that register_globals = On.

Web page title

Let's start by looking at a common technique in many PHP applications: placing certain kinds of information in variable assignment statements at the top of the program. This allows for ease of maintenance and updating later on.

Listing 2: Variable assignment


<?php
   <span class="rboldcode">$title = "PHP Demo Webzine"; 
  $slogan = "Illustrating the coolness of PHP since September 2000";</span>

?>
<html>
 <head>
  <title><?php <span class="rboldcode">echo($title)</span> ?></title>

 </head>
 <body>
  <h1><?php <span class="rboldcode">echo($title)</span> ?></h1>

  <p><i><?php <span class="rboldcode">echo($slogan)</span> ?></i></p>

Again, note the PHP boundary tags: <?php to take you out of HTML mode into PHP mode, and ?> to switch back into HTML mode. You can switch back and forth as many times as you like. Some things are just easier to do in HTML mode, and others are easier in PHP mode. All you're doing is defining two variables at the top of the program and then slipping into HTML mode. Whenever we need to use one of the variables, you pop back into PHP mode and issue an echo statement to write the variable's value directly into the Web page text.

Category menu

You'll get three topic menu files: Main.txt, Politics.txt and Technology.txt. The driver will appear as follows upon selection of the "Main" topic:

Main
Politics
Technology

<table border="1">
 <tr><td bgcolor="pink"><center>

  <b> Main </b></center></td></tr>
 <tr><td bgcolor="silver"><center>
  <b> 
   <a href="index.php3?topic=Politics">Politics</a>   </b></center></td></tr>

 <tr><td bgcolor="silver"><center>
  <b>
    <a href="index.php3?topic=Technology">Technology</a>   </b></center></td></tr>

</table>
$cats
<?php
   <span class="rboldcode">$cats = file("category.txt"); 
   $elems = count($cats);</span>
?>

The file function just copies the file into an array. So $cats[0] will equal "Main," $cats[1] equals "Politics" and $cats[2] equals "Technology". The file function makes it a snap to import a short ASCII text file, but don't use it on very large files. The count function counts the number of elements in an array, so you would expect $elems to equal 3 in the example.

Here's how to create the above HTML table from that array:

Listing 3: Creating the HTML table


<table border="1">

<?php
   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>
<table border="1">
<?php
for
   for ($i=0; $i<$elems; $i++) {
trimfile
      $item = trim($cats[$i]);
$item$ifile
      $ifile = ereg_replace(" ","",$item);
$color?:test?truevalue:falsevalue$color
      $color = ($ifile == $topic) ? "pink" : "silver";
index.php?topic=Politics$ifile$ifile
      $url = "index.php3?topic=$ifile";
?:<a href="index.php?topic=Politics>Politics</a>Politics
      $anchor = " " . ($item != $topic ? "<a href=\"$url\">$item</a>" : "$item") . " ";
echo\"$color$anchor
      echo(" <tr><td bgcolor=\"$color\"><center><b>$anchor</b></center></td></tr>\n");
for
   }
?>
</table>

And that's it!

Story presentation

$story
      $storyfile = fopen("s$story.txt","r");
      fpassthru($storyfile);

In the above example, the fopen function opens a file, returning a handle to the file that is then placed in the variable $storyfile by the assignment operator. The fpassthru function copies the contents of the file to the current output device (the output HTML file), and automatically closes the file for you.

What's next

Part 1 of this article presents the first part of a simple PHP Webzine application. I've covered a few small code examples in detail. (This part of the application is only 2K worth of code, so there weren't many large code examples to choose from!) This should give you a good taste of the power of PHP, but there's a lot more to come.

Part 2 is about the same length as Part 1. I'll complete the discussion of the delivery module by showing how presentation of the menu of stories appears to the reader. I'll then cover the authoring module that permits authors to submit stories. Although the authoring module is a lot larger, we will not cover it in as much detail: Only the interesting concepts that are different from the delivery module need an explanation.



View PHP by example: Part 1 Discussion

Page:  1 2 3 4 Next Page: Resources

First published by IBM developerWorks


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