Specifications

echo “<p>There are no items in your cart”;
echo “<hr>”;
}
$target = “index.php”;
// if we have just added an item to the cart,
// continue shopping in that category
if($new)
{
$details = get_book_details($new);
if($details[“catid”])
$target = “show_cat.php?catid=”.$details[“catid”];
}
display_button($target, “continue-shopping”, “Continue Shopping”);
$path = $PHP_SELF;
$path = str_replace(“show_cart.php”, “”, $path);
display_button(“https://”.$SERVER_NAME.$path.”checkout.php”,
“go-to-checkout”, “Go To Checkout”);
do_html_footer();
?>
There are three main parts to this script: displaying the cart, adding items to the cart and saving
changes to the cart. Well cover these in the next three sections.
Viewing the Cart
No matter which page we have come from, we will display the contents of the cart. In the base
case, when a user has just clicked View Cart, this is the only part of the code that will be exe-
cuted, as follows:
if($cart&&array_count_values($cart))
display_cart($cart);
else
{
echo “<p>There are no items in your cart”;
echo “<hr>”;
}
As you can see from this code, if we have a cart with some contents, we will call the
display_cart() function. If the cart is empty, well give the user a message to that effect.
The display_cart() function just prints the contents of the cart as a readable HTML format,
as you can see in Figures 25.6 and 25.7. The code for this function can be found in
Building Practical PHP and MySQL Projects
P
ART V
560
LISTING 25.9 Continued
31 7842 CH25 3/6/01 3:39 PM Page 560