Fall Semester 2002


Lecture Notes Fourteen: PHP Arrays, Functions, Sessions

We discussed variables, basic parsing, and basic PHP last time.

Today we want to take a look at

We'll use a pattern that you've seen before, so I hope you find it familiar.

Here's the first example.

Here's the code for it (we call it one.php).

<html>
  <head><title>One</title></head>
  <body bgcolor=white>
    <h1>Hello!</h1>
    <img src="http://www.cs.indiana.edu/dept/img/lh08.gif">
  </body>
</html>
Now let's make a change (and call it two.php).

<html>
  <head><title>One</title></head>
  <? $name = "lh08.gif"; ?>
  <body bgcolor=white>
    <h1>Hello!</h1>
    <img src="http://www.cs.indiana.edu/dept/img/<?=$name?>">
  </body>
</html>
The change is conceptual, as you already know.

Here now is the next step (called three.php).

<html>
  <head><title>One</title></head>
  <? $images = array("lh08.gif", "lh07.gif", "lh09.gif", "lh01.gif"); 
     $index = 0; 
     $name = $images[$index]; 
  ?>
  <body bgcolor=white>
    <h1>Hello!</h1>
    <img src="http://www.cs.indiana.edu/dept/img/<?=$name?>">
  </body>
</html>
We now need this (and include it below).

<html>
  <head><title>One</title></head>
  <? $images = array("lh08.gif", "lh07.gif", "lh09.gif", "lh01.gif"); 
     $index = rand(0, sizeof($images) - 1); 
     $name = $images[$index]; 
  ?>
  <body bgcolor=white>
    <h1>Hello!</h1>

    The image below has index <?=$index?>. <p> 

    Click <a href="<?=$SCRIPT_NAME?>">here</a> for a new random image. <p> 

    <img src="http://www.cs.indiana.edu/dept/img/<?=$name?>"> 
    
  </body>
</html>
Here's the result (called four.php).

<html>
  <head><title>One</title></head>
  <? $images = array("lh08.gif", "lh07.gif", "lh09.gif", "lh01.gif"); 
     $index = rand(0, sizeof($images) - 1); 
     $name = $images[$index]; 
  ?>
  <body bgcolor=white>
    <h1>Hello!</h1>

    The image below has index <?=$index?>. <p>

    Click <a href="<?=$SCRIPT_NAME?>">here</a> for a new random image. <p> 

    <img src="http://www.cs.indiana.edu/dept/img/<?=$name?>"> 
    
  </body>
</html>
The major difference here is that you can feed data into the program as early as two.php.

(I hope you see why: the parsing is automatic, and places data in variables with canonical names).

We now build a shopping cart, show the date, ask a question.

(We're getting closer to showing a second sample project, implemented in PHP).

Here's how you show the date in PHP.

We'll use this in the mini-shopping cart we will develop below, today.

Say we have five books to sell:

Let's set up a rudimentary shopping cart with PHP.

This will help us become familiar with the ideas behind it.

Here's a basic picture in terms of screens the user will see.

Here's the first of the screens (viewCat.php):

<html>
  <head><title>View Catalog</title></head>
  <body bgcolor="white">
  Hello, this is the View Catalog screen. <p> 

  We sell the following books: <p> 

  <table>

  <tr> <th> Title <th> Author <th> Price <th> 

  <tr> <td> Professional Apache
       <td> Peter Wainwright 
       <td> $32.56
       <td> <a href="viewCart.php?add=pa">Add to cart</a>

  <tr> <td> The Perl Cookbook 
       <td> Tom Christiansen 
       <td> $29.12
       <td> <a href="viewCart.php?add=pc">Add to cart</a>

  <tr> <td> How to Set up and Maintain a Web Site
       <td> Lincoln D. Stein 
       <td> $42.70 
       <td> <a href="viewCart.php?add=ls">Add to cart</a>

  <tr> <td> Webmaster in a Nutshell 
       <td> Spainhour, Eckstein 
       <td> $10.23
       <td> <a href="viewCart.php?add=wm">Add to cart</a>

  <tr> <td> Learning Perl  
       <td> Randal Schwartz 
       <td> $18.95 
       <td> <a href="viewCart.php?add=lp">Add to cart</a>

  </table>

  <p> 
    <img src="http://www.cs.indiana.edu/classes/a113-dger/left.gif"> 
    <a href="viewCart.php?show">View Shopping Cart</a> <p> 

  </body>
</html>
Notice that it allows context-sensitive transitions to the second one.

Try the first screen here, then let's move to implementing the other two.

The second one (viewCart.php) is only basic now:

<html>
  <head><title>View Shopping Cart</title></head>
  <body bgcolor="white">
  Hello, this is the View Shopping Cart screen. <p> 
  </body>
</html>
And here's the last one (placeOrder.php) just as basic at this stage in the development.

<html>
  <head><title>Place Order</title></head>
  <body bgcolor="white">
  Hello, this is the Place Order screen. <p> 
  </body>
</html>
Let's work on the second screen and make it distinguish what we want of it.

<html>
  <head><title>View Shopping Cart</title></head>
  <body bgcolor="white">
  Hello, this is the View Shopping Cart screen. <p> 

  <? if ($add) { 
       echo "You're trying to add a book."; 
     } else { 
       echo "You want to view the cart."; 
     }
   ?>

  </body>
</html>
Notice how the two screens share a convention (add, view).

Let's extend the second screen to be able to detect and report more.

<html>
  <head><title>View Shopping Cart</title></head>
  <body bgcolor="white">
  <? 

   $cat = array("pa" => "Professional Apache", 
                "pc" => "The Perl Cookbook",
                "ls" => "How to Set up and Maintain a Website", 
                "wm" => "Webmaster in a Nutshell",
                "lp" => "Learning Perl"); 
   ?>

  Hello, this is the View Shopping Cart screen. <p> 

  <? if ($add) { $book = $cat[$add]; ?> 

     You are trying to add <?=$book?>  

  <? } else { 
       echo "You want to view the cart."; 
     }
   ?>

  </body>
</html>
You have just seen a hashtable (associative array).

Let's use such a variable to keep track of the books we buy.

<? session_start(); 

   if (session_is_registered("cart")) {
       
   } else {
     session_register("cart"); 
       
   } 
 ?> 

<html>
  <head><title>View Catalog</title></head>
  <body bgcolor="white">
  Hello, this is the View Catalog screen. <p> 

  We sell the following books: <p> 

  <table>

  <tr> <th> Title <th> Author <th> Price <th> 

  <tr> <td> Professional Apache
       <td> Peter Wainwright 
       <td> $32.56
       <td> <a href="viewCart.php?add=pa">Add to cart</a>

  <tr> <td> The Perl Cookbook 
       <td> Tom Christiansen 
       <td> $29.12
       <td> <a href="viewCart.php?add=pc">Add to cart</a>

  <tr> <td> How to Set up and Maintain a Web Site
       <td> Lincoln D. Stein 
       <td> $42.70 
       <td> <a href="viewCart.php?add=ls">Add to cart</a>

  <tr> <td> Webmaster in a Nutshell 
       <td> Spainhour, Eckstein 
       <td> $10.23
       <td> <a href="viewCart.php?add=wm">Add to cart</a>

  <tr> <td> Learning Perl  
       <td> Randal Schwartz 
       <td> $18.95 
       <td> <a href="viewCart.php?add=lp">Add to cart</a>

  </table>

  <p> 
    <img src="http://www.cs.indiana.edu/classes/a113-dger/left.gif"> 
    <a href="viewCart.php?show">View Shopping Cart</a> <p> 

  </body>
</html>
The only new part is at the beginning, where we manage our session.

Notice we have no idea what kind of variable cart is.

The variable is only registered here, and only if needed.

The actual management of the variable is done in the second screen.

<? session_start(); 

 ?> 

<html>
  <head><title>View Shopping Cart</title></head>
  <body bgcolor="white">
  <? 

   $cat = array("pa" => "Professional Apache", 
                "pc" => "The Perl Cookbook",
                "ls" => "How to Set up and Maintain a Website", 
                "wm" => "Webmaster in a Nutshell",
                "lp" => "Learning Perl"); 
   ?>

  Hello, this is the View Shopping Cart screen. <p> 

  <? if ($add) { $book = $cat[$add]; 
                 $cart[$add] += 1; 

   ?> 

     You are trying to add <?=$book?> 


  <? } else { 
       echo "You want to view the cart."; 
     }

   ?>

   <p> Your current shopping cart: <p> <center>

   <table width=80%>
     <tr><th bgcolor=lightgrey> Title <th bgcolor=lightgrey> Qty 

   <?

     while ($element = each($cart)) {
       $product  = $element["key"];
       $quantity = $element["value"]; 

       echo "<tr><td align=center>", $cat[$product], 
                "<td align=center>", $quantity; 
     }

     echo "</table></center>"; 

   ?>

  </body>
</html>
This screen does the reporting also, as you have seen.

Let's improve on it:

<? session_start(); 

   function show_cart() {

     global $cart, $cat; 

     while (list($product, $quantity) = each($cart)) {

       echo "<tr><td align=center>", $cat[$product], 
                "<td align=center>", $quantity, 
                "<td align=center>", drop($product); 
     }

     // may need reset($cart) later, don't forget!

   }

   function drop($code) {

     global $SCRIPT_NAME; 
     ?>
        <a href="<?=$SCRIPT_NAME?>?drop=<?=$code?>">Drop from cart</a>
     <?
   }

 ?> 

<html>
  <head><title>View Shopping Cart</title></head>
  <body bgcolor="white">
  <? 

   $cat = array("pa" => "Professional Apache", 
                "pc" => "The Perl Cookbook",
                "ls" => "How to Set up and Maintain a Website", 
                "wm" => "Webmaster in a Nutshell",
                "lp" => "Learning Perl"); 
   ?>

  Hello, this is the View Shopping Cart screen. <p> 

  <? if ($add) { 
       $book = $cat[$add]; 
       $cart[$add] += 1; 
   ?> 

     You are trying to add <?=$book?>

  <? } elseif ($drop) { 
       $book = $cat[$drop]; 
       $cart[$drop] -= 1;        
     } else { 
       echo "You want to view the cart."; 
     }

   ?>

   <p> Your current shopping cart: <p> <center>

   <table width=80%>
     <tr><th bgcolor=lightgrey> Title <th bgcolor=lightgrey> Qty 

   <? show_cart(); 
      echo "</table></center>"; 

   ?>

  </body>
</html>
This version could be tested here.

It has a number of drawbacks, but it is able to maintain a cart.

One can easily improve on this, and we will do that in the project example for this section.

The basic goal of the script above was to illustrate:

and I hope it did that.

Next time we need to get into serious application development.


Last updated: Oct 8, 2002 by Adrian German for A348/A548