Specifications
Implementing the Shopping Cart
The shopping cart functionality all revolves around a session variable called $cart. This is an
associative array that has ISBNs as keys and quantities as values. For example, if I add a single
copy of this book to my shopping cart, the array would contain
0672317842 => 1
That is, one copy of the book with the ISBN 0672317842. When we add items to the cart, they
will be added to the array. When we view the cart, we will use the $cart array to look up the
full details of the items in the database.
We also use two other session variables to control the display in the header that shows Total
Items and Total Price. These variables are called
$items and $total_price, respectively.
Using the show_cart.php Script
Let’s begin looking at how the shopping cart code is implemented by looking at the
show_cart.php script. This is the script that displays the page we will visit if we click on any
View Cart or Add to Cart links. If we call show_cart.php without any parameters, we will get
to see the contents of it. If we call it with an ISBN as parameter, the item with that ISBN will
be added to the cart.
To understand this fully, look first at Figure 25.6.
Building a Shopping Cart
C
HAPTER 25
25
B
UILDING A
SHOPPING CART
557
FIGURE 25.6
The show_cart.php script with no parameters just shows us the contents of our cart.
31 7842 CH25 3/6/01 3:38 PM Page 557










