Spring Semester 2003


Lecture Notes Sixteen: PHP Shopping Cart
Here's the starting point.
<? 

   // 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):

So let's assume all of this has been installed on your site.

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-dger/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 (again).

<?

  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-dger/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 part of your lab assignment.

<?
   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, which will be done next lecture.

It will be based on what we discussed already, a few days ago.


Last updated: Mar 10, 2003 by Adrian German for A348/A548