Specifications

2. Provide an image for each book, if one exists. We use the HTML image height and width
tags to resize the image a little smaller here. This means that the images will be a little
distorted, but they are small enough that this isnt much of a problem. (If it bothers you,
you can always resize the images using the gd library discussed in Chapter 19,
Generating Images, or manually generate different size images for each product)
3. Make each cart entry a link to the appropriate book, that is, to show_book.php with the
ISBN as a parameter.
4. If we are calling the function with the $change parameter set to true (or not setit
defaults to true), show the boxes with the quantities in them as a form with the Save
Changes button at the end. (When we reuse this function after checking out, we wont
want the user to be able to change her order.)
Nothing is terribly complicated in this function, but it does quite a lot of work, so you might
find it useful to read it through carefully.
Adding Items to the Cart
If a user has come to the show_cart.php page by clicking an Add To Cart button, we have to
do some work before we can show her the contents of her cart. Specifically, we need to add the
appropriate item to the cart, as follows.
First, if the user has not put any items in her cart before, she will not have a cart, so we need to
create one:
if(!session_is_registered(“cart”))
{
$cart = array();
session_register(“cart”);
$items = 0;
session_register(“items”);
$total_price = “0.00”;
session_register(“total_price”);
}
To begin with, the cart is empty.
Second, after we know that a cart is set up, we can add the item to it:
if($cart[$new])
$cart[$new]++;
else
$cart[$new] = 1;
Here, we are checking whether the items already in the cart. If it is, we increment the quantity
of that item in the cart by one. If not, we add the new item to the cart.
Building a Shopping Cart
C
HAPTER 25
25
B
UILDING A
SHOPPING CART
563
31 7842 CH25 3/6/01 3:39 PM Page 563