displayCart( )
checks if the cart has contents by testing for the presence of the session variable order_no
. If order_no
is registered, its value is the order_id
associated with the shopping cart, and the following query is executed:
$cartQuery = "SELECT qty, price, wine_id, item_id FROM items WHERE cust_id = -1 AND order_id = $order_no";
The query retrieves the items in the user's cart, and the items are then displayed in an HTML <table>
environment. The quantities of each item are displayed within the <table>
as <input>
elements of a <form>
. Each element has an associated name
attribute that is set to the item_id
of the item, and the value
of the attribute is set to the quantity of wine in the cart. For example, consider the following HTML fragment that represents the second item in a user's cart:
<tr> <td><input type="text" size=3 name="2" value="13"></td> <td>1982 Grehan's Vineyard Galti Cabernet Sauvignon</td> <td>$20.86</td> <td>$271.18</td> </tr>
When rendered in a browser, this item displays a quantity of 13 bottles that can be edited by the user. If the user changes the quantity and clicks on the Update Quantities button, a request is made for the cart.6 script to update the quantities. The request includes the item_id
of 2 as the GET
method attribute and the new quantity as its value. We discuss the cart.6 script later in this section.
Example 11-2. cart.2 displays the contents of the user's shopping cart
<?php // This script shows the user the contents of // their shopping cart include 'include.inc'; set_error_handler("errorHandler"); // Show the user the contents of their cart function displayCart($connection) { global $order_no; // If the user has added items to their cart, then // the variable order_no will be registered if (session_is_registered("order_no")) { $cartQuery = "SELECT qty, price, wine_id, item_id FROM items WHERE cust_id = -1 AND order_id = $order_no"; // Retrieve the item details of the cart items if (!($result = @ mysql_query($cartQuery, $connection))) showerror( ); $cartAmount = 0; $cartCount = 0; // Create some headings for the cart echo "<table border=\"0\" " . "cellpadding=\"0\" cellspacing=\"5\">"; echo "\n<tr>"; echo "\n\t<th>Quantity </th>"; echo "\n\t<th>Wine</th>"; echo "\n\t<th>Unit Price</th>"; echo "\n\t<th>Total</th>"; echo "\n</tr>"; // Go through each of the wines in the cart while ($row = @ mysql_fetch_array($result)) { // Keep a running total of the number of items // and dollar-value of the items in the cart $cartCount += $row["qty"]; $lineTotal = $row["price"] * $row["qty"]; $cartAmount += $lineTotal; // Show the quantity of this item in a text // input widget. The user can alter the quantity // and update it echo "\n<tr>"; echo "\n\t<td>" . "<input type=\"text\" size=3 name=\"" . $row["item_id"] . "\" value = \"" . $row["qty"] . "\"></td>"; // Show the wine details of the item echo "\n\t<td>"; echo showWine($row["wine_id"], $connection); echo "</td>"; // Show the per-bottle price printf("\n\t<td>$%.2f</td>", $row["price"]); // Show the total price of this item printf("\n\t<td>$%.2f</td>", $lineTotal); echo "\n</tr>"; } echo "\n<tr></tr>"; // Show the user the total number of bottles // and the total cost of the items in the cart printf("\n<tr>\n\t<td><b>%d items</b></td>", $cartCount); echo "\n\t<td></td>\n\t<td></td>"; printf("\n\t<td><b>$%.2f</b></td>\n</tr>", $cartAmount); echo "\n</table>"; } else { // The session variable $order_no is not // registered. Therefore, the user has not // put anything in the cart echo "<h3><font color=\"red\">" . "Your cart is empty</font></h3>"; } } // --------- // Open a connection to the DBMS if (!($connection = @ mysql_pconnect($hostName, $username, $password))) showerror( ); if (!mysql_select_db($databaseName, $connection)) showerror( ); // Initialize a session. This call either creates // a new session or re-establishes an existing one. session_start( ); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <head> <title>Alexa and Dave's Online Wines</title> </head> <body bgcolor="white"> <?php // Show the user login status showLogin( ); ?> <h1>Your Shopping Cart</h1> <?php // Show the user any messages showMessage( ); ?> <form action="example.cart.5.php" method="GET"> <?php // Show the contents of the shopping cart displayCart($connection); ?> <table> <tr> <td><input type="submit" name="home" value="Home"></td> <?php // If the user has items in their cart, offer the // chance to update quantities or empty the cart or // finalize the purchase (if they're logged in) if (session_is_registered("order_no")) { echo "\n\t<td><input type=\"submit\" " . "name=\"update\" value=\"Update Quantities\"></td>"; echo "\n\t<td><input type=\"submit\" " . "name=\"empty\" value=\"Empty Cart\"></td>"; if (session_is_registered("loginUsername")) echo "\n\t<td><input type=\"submit\" " . "name=\"buy\" value=\"Make Purchase\"></td>"; } // Show the user the search screen button echo "\t<td><input type=\"submit\" " . "name=\"search\" value=\"Search\"></td>\n"; // Show login or logout button loginButtons( ); ?> </tr> </table> </form> <br><a href="http://validator.w3.org/check/referer"> <img src="http://www.w3.org/Icons/valid-html401" height="31" width="88" align="right" border="0" alt="Valid HTML 4.01!"></a> </body> </html>