SimpleshoPHP

Detailed Analysis - Part 5

bookbag.php

As usual, we start by making sure there is an existing session -- if not, go back to the "main.php" page. If there is one, we connect to it with "session_start();" and then hook up the database.

<?
require_once("startme.inc");
if (! isset($_SESSION["xvalid"])) {
        header("Location: main.php");
        exit;
}

Here's the start of the good stuff:

$sql = "SELECT books.sku, title, author, format, price, bag.qty
FROM books, bag WHERE books.sku = bag.sku AND sess = '$PHPSESSID'";

$sql_result = mysql_query($sql,$connection) 
	or die ("Couldn't get Bookbag");

Let's count the number of rows returned by our "join" command. You'll see why in just a moment.

$num_rows = mysql_num_rows($sql_result);

?>

As described in the Overview, we're doing a very simple join on our two tables -- bag has all the items that our customer has picked, associated with her Session ID; the books table lets us recover all the details based on the unique SKU, without having to copy all those details into the cart. If there are five rows in the bag, we will display five rows of details. The code to display our results is a bit lengthy but it is no more complex than what you've already seen.

The boilerplate section looks a lot like the index page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<link rel="stylesheet" href="simple.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Silk Purse Books - silkpursebooks.com</title>
</head>
<body bgcolor="#ffffff" >

<? $location="Your Bookbag"; include("top.inc");?>

Again, we know whether we came from browsing a Department or a Title/Author search, so we adjust our breadcrumb accordingly:

<!--- left --->
<tr>
<td>
<?php if ($dept) {
	echo "<a href=\"main.php\">Main: Departments:</a> <em>$dept</em>";
}
else {
	echo "<a href=\"main.php\">Back to Main</a>";
}

Now, here's where we make use of "$num_rows" captured above:

if ($num_rows < 1) {
echo "<strong>Your Book Bag is empty.</strong><br>";
exit;
}
?>

We could just display an empty book bag -- a title and a zero total; but it's a little cleaner to tell the customer her book bag is empty. If we find ourselves holding an empty book bag at checkout time, we're going to be a bit more forceful about it, and send her back to the main page.

It's not just for silly people who click on "Book Bag" before selecting any items -- it also works if our customer picks items and puts them back. We display the book bag after each update, whether adding or deleting items. If the bag is empty, we'll hit this test and display the "empty book bag" message.

Here's the rest of the good stuff -- we display the results of the "join" query. We are re-using a lot of the display code from the flypage, too:

<!--- main --->
<td>
<center>
Your Bookbag&nbsp;&nbsp;-&nbsp;&nbsp;  
<a href=checkout.php>Check Out</a>&nbsp;&nbsp;-&nbsp;&nbsp;
<a href=aboutus.php>About Us</a>
</td>

Please note that while the link to checkout.php is a plain link here, in production we would make it an https link, one using SSL, like so:

<a href="https://secure.example.com/checkout.php?PHPSESSID=$PHPSESSID">

</tr>
</table>
<table width="90%" align=center border=0 cellpadding=3>
<tr bgcolor="#e7f0ff">
<td><b>TITLE</b><td><b>AUTHOR</b><td><b>FORMAT</b>
<td><b>PRICE</b><td>QTY<td>Remove</tr>
<?

Set up our odd/even counter so we can shade the rows in alternating colors, then start up our old familiar "while" loop:

$i = 0; $tot = 0; $tqty = 0;
while ($row = mysql_fetch_array($sql_result)) {
	$sku = $row["sku"];
	$title = $row["title"];
	$author = $row["author"];
	$format = $row["format"];
	$qty = $row["qty"]; $tqty += $qty;
	$price = sprintf("%0.2f",$row["price"]); $tot += ($price * $qty);
	if ($i++ % 2)
            $BGC="#e7f0ff";
	else
            $BGC="#ffffff";
	echo "
	<tr bgcolor=$BGC>
	<td>$title<td>$author<td>$format<td>$price<td>$qty
<td><a href=\"rmfrbag.php?sku=$sku\">[-]</a></tr>
	";
}
$ptot = sprintf("\$%0.2f",$tot);
if ($i++ % 2)
   $BGC="#e7f0ff";
else
   $BGC="#ffffff";
echo "
<tr bgcolor=$BGC><td>&nbsp;<td>&nbsp;<td>Total:
<td>$ptot<td>$tqty<td>&nbsp;";
?>
</table>

Did you notice that we calculated a total cost for all the items? We're going to reuse most of this code when we get to the checkout page, too.

<!--- bottom --->
<? include("bottom.inc"); ?>
</body></html>

One other thing -- note the link to "rmfmbag.php". We have to provide a way for the customer to change her mind, after all. It looks just like "addtobag.php" except that it removes the entry from the bag table instead of inserting it, and it adds to the "stock" column in the books table instead of subtracting from it.


Next stop: checkout.php!


The Technical Walkthrough

Summary

Detailed descriptions

SimpleshoPHP Home


You are invited to post comments or questions on the SimpleshoPHP forum at SourceForge.net.

SourceForge.net Logo


Copyright 2003-2005, Kevin Martin, dba Brass Cannon Consulting.
The project "SimpleshoPHP" is Free Software, distributed
under the LGPL as described at opensource.org