Lecture Notes Nineteen: Homework Four, DHTML, Homework Five

Since we spent so much time with the shopping cart I would like to make it the default group project for this version of this class. I describe Homework Four below (related to Lab Assignment Nine) and I start the DHTML chapter in anticipation of the lab tomorrow.

The lecture on Thursday will be on Homework Five. The due dates will be announced shortly. Let me now describe the parameters (basic idea and overall organization) of the default group project.

I will guide you through al the stages, in the same way as in lab nine and in homework four. You need to implement everything as posted and take an exam on the project (which will be like a homework assignment). Due dates and questions should be posted on the web site by November 20, 2001.

Now the details about homework four. We started last time from:

<? 

   // top level file, called index.php

   session_start(); 
   do_html_header('Welcome to the On-line Bookstore');  
   $cat_array = get_categories(); 
   display_categories($cat_array); 
   do_html_footer(); 

 ?>  
At this point things look like this (in terms of screens):

We're done implementing index.php and we move to show_cat.php.

Note one thing though: You need to be very careful from here on.

For example: only two books have been entered in dgerman_php_books.

Does it matter? Is this relevant any time soon? That's for you to decide.

Let me just say that those two books belong to category: Physics.

That, for the functionality of it. Now, here's what's missing:

The second screen is like this:

<? session_start();
   $name = get_category_name($catid);
   do_html_header("Category: <font color=\"brown\">" . $name . "</font>"); 
   $book_array = get_books($catid); 
   display_books($book_array); 
   $codebase = "http://www.cs.indiana.edu/classes/a348/fall2001/bookstore/"; 
 ?>
   <center>
<?
   display_button("index.php", $codebase . "continue", "Keep Shopping"); 
 ?>
   </center>
<?
   do_html_footer(); 
 ?>
The things you need are listed below, in order.

Here's one of the functions:

<? function get_category_name($catid) {
     $conn = db_connect(); 
     $query = "select catname
               from dgerman_php_categories
               where catid = $catid";
     $result = @mysql_query($query); 
     if (! $result) return false; 
     $num_cats = @mysql_num_rows($result); 
     if ($num_cats == 0) return false;
     $result = mysql_result($result, 0, "catname"); 
     return $result; 
   }
 ?>
It uses a function you should already have.

The same is true of the actual screen, as well (in several places).

Here's a second needed function:

<? 
   function get_books($catid) {

     if (! $catid || $catid == "") return false; 

     $conn = db_connect(); 

     $query = "select distinct dgerman_php_catboo.isbn, title, author
               from dgerman_php_catboo, dgerman_php_books
               where catid = $catid
                 and dgerman_php_catboo.isbn = dgerman_php_books.isbn"; 

     $result = @mysql_query($query);

     if (! $result) return false; 

     $num_books = @mysql_num_rows($result); 
     if ($num_books == 0) return false; 
     $result = db_result_to_array($result);

     return $result; 

   }
 ?>
Which needs this function right away:
<? function db_result_to_array($result) {
     $res_array = array(); 
     for ($count = 0; $row = mysql_fetch_array($result); $count++) 
       $res_array[$count] = $row; 
     return $res_array; 
   }
 ?>
Another needed function is used to display the books one by one:
<? function display_books($book_array) {

     $codebase = "http://www.cs.indiana.edu/classes/a114/fall2001/lectures/nine"; 
     if (! is_array($book_array)) {
       echo "<p>No books currently available in this category.<p>"; 
     } else { 
       echo "<table width=\"100%\" border=0>";
       foreach ($book_array as $row) {
         $url = "show_book.php?isbn=" . ($row["isbn"]); 
         echo "<tr><td align=center>"; 

         $title = "<img src=\"$codebase/" . ($row["isbn"]) . ".jpg\" border=0>"; 

         do_html_url($url, $title); 
         $hand = "http://www.cs.indiana.edu/classes/a113-dger/left.gif"; 
         echo "</td><td valign=top>"; 
         echo $row["title"] . " <br> by " . $row["author"]; 
         echo "<p> <img src=\"$hand\"> Click"; 
         do_html_url($url, "here"); 
         echo " for details. </td></tr>"; 
       }
       echo "</table>"; 
     }
     echo "<hr>"; 
   }
 ?>
That's all there's needed, except you might find there's more (that you have) that is used here too.

So you'll have to find a way to incorporate them in here.

The picture now is this:

Let's now concentrate on the red part.

<?

  session_start();

  $book = get_book_details($isbn);

  do_html_header('Book Title: <font color="brown">' . 
                 $book["title"] . '</brown>');
  display_book_details($book);
  $target = "index.php";
  if($book["isbn"])
  {
    $target = "index.php"; 
    $codebase = "http://www.cs.indiana.edu/classes/a348/fall2001/bookstore/";
    echo "<center>"; 
    display_button("show_cart.php?new=$isbn", $codebase . "addToCart", 
                   "Add ".$book["title"]." To My Shopping Cart"); 
    echo "</center><hr><center>"; 
    display_button($target, $codebase . "continue", "Continue Shopping");  
    echo "</center>"; 
  } else { echo "<hr>"; } 
  do_html_footer();

?>
That was the basic screen. You have some helpers already.

<?
   function get_book_details($isbn) {
     if (!$isbn || $isbn=="") return false;
     $conn = db_connect();
     $query = "select * from dgerman_php_books where isbn='$isbn'";
     $result = @mysql_query($query);
     if (!$result) return false;
     $result = @mysql_fetch_array($result);
     return $result;
   }
 ?>
Now here is the second new function.
<?

   function display_book_details($book) {

     if (is_array($book)) {

       $codebase = "http://www.cs.indiana.edu/classes/a114/fall2001/lectures/nine";

       echo "<table><tr>"; 

       echo "<td valign=up><img src=\"$codebase/".$book["isbn"].".jpg\" border=0></td>";
       echo "<td><table>";
       echo "<tr><td> <font color=\"brown\">Author:</font> </td><td> ";
       echo $book["author"];
       echo "</td></tr><tr><td> <font color=\"brown\">ISBN:</font> </td><td> ";
       echo $book["isbn"];
       echo "</td></tr><tr><td> <font color=\"brown\">Our Price:</font> </td><td> $";
       echo number_format($book["price"], 2);
       echo "</td></tr><tr><td colspan=2><font color=\"brown\">Description:</font> ";
       echo $book["description"];
       echo "</td></tr><tr><td> <font color=\"brown\">Categories:</font> </td> <td> "; 
       get_book_cats($book["isbn"]); 
       echo "</td> </tr> </table> </td></tr></table>"; 

     } else
       echo "The details of this book cannot be displayed at this time.";

   }
   

 ?>
And, what follows is your homework four.

<?
   function get_book_cats($isbn) {
     echo "Write this."; 
   } 
 ?>
Here's a working version (choose Physics first).

The situation at this point is:

So our next stop would be show_cart, for the project.


Last updated: Oct 2, 2001 by Adrian German for A348/A548