Specifications

if(is_array($cart))
{
foreach($cart as $isbn => $qty)
{
$items += $qty;
}
}
return $items;
}
The calculate_items() function is simplerit just goes through the cart and adds up the
quantities of each item to get the total number of items.
Saving the Updated Cart
If we have come to the show_cart.php script from clicking the Save Changes button, the
process is a little different. In this case, we have come via a form submission. If you look
closely at the code, you will see that the Save Changes button is the submit button for a form.
This form contains the hidden variable save. If this variable is set, we know that we have come
to this script from the Save Changes button. This means that the user has presumably edited
the quantity values in the cart, and we need to update them.
If you look back at the text boxes in the Save Changes form part of the script, you will see that
they are named after the ISBN of the item that they represent, as follows:
echo “<input type = text name = \”$isbn\” value = $qty size = 3>”;
Now look at the part of the script that saves the changes:
if($save)
{
foreach ($cart as $isbn => $qty)
{
if($$isbn==”0”)
unset($cart[$isbn]);
else
$cart[$isbn] = $$isbn;
}
$total_price = calculate_price($cart);
$items = calculate_items($cart);
}
You can see that we are working our way through the shopping cart, and for each $isbn in the
cart, we are checking the variable with that name, using the variable variable notation
$$isbn.
Building a Shopping Cart
C
HAPTER 25
25
B
UILDING A
SHOPPING CART
565
LISTING 25.12 Continued
31 7842 CH25 3/6/01 3:39 PM Page 565