In this chapter, we introduce the shopping cart developed for the online winestore. The shopping cart is typical of those used in online stores: the user can add items to the cart and manage the quantities of the different items. The solution we outline is scalable and practical. The cart data is stored in the winestore database tables, and only one session variable per user is required to track the cart's identity.
This chapter is the second of four that outline the complete winestore application. As discussed in Chapter 10, the descriptions of the scripts aren't comprehensive, and we assume you've read Chapter 2 to Chapter 9 as background. Also, we encourage you to install a local copy of the application and to view, edit, and use the scripts while reading this chapter.
We present here the four scripts that manage the shopping cart, a fifth script that produces the home page that includes the Hot New Wines panel, and a sixth script that manages redirection to other pages when the user clicks on buttons.
The scripts in this chapter perform the following functions:
As with the other modules in the winestore, the shopping cart isn't a production system. The scripts presented here illustrate the practice of developing a web database application. Techniques shown include database interactivity, concurrency management, using sessions, and one- and two-component querying.
Example 11-1 shows the final implementation of the winestore home page containing the Hot New Wines panel. The Hot New Wines panel is discussed in more detail in Chapter 4, and the one-component functionality for adding one or a dozen bottles of wine to the cart is discussed in Chapter 5. We discuss how clicks on the add-to-cart links are managed later in Section 11.2.
The Hot New Wines panel is based on scripts presented in Chapter 4 and is encapsulated in the function showPanel( ) in Example 11-1. The functions showVarieties( ), which displays the varieties of a specific wine, and showPricing( ), which shows the per-bottle and the per-case price of a wine, are part of the include.inc file discussed in Chapter 10.
The main body of the script presents the front page using a mixture of HTML and calls to functions. The function showCart( ) displays an embedded link cart icon and the dollar total and number of items in the cart. The function showMessage( ) displays any message registered in the session variable message, and the showLogin( ) function displays the user's login status. The function loginButtons( ) shows the user different buttons depending on whether or not she is currently logged in. All these functions are part of include.inc and discussed in Chapter 10.
The following code fragment inserts the file disclaimer into the body of the HTML:
require 'disclaimer';
The file is a text message that alerts the user that our system doesn't really sell wines and that the scripts are covered by the GNU public license.
<?php // This is the script that shows the user a list of // wines, and allows them to select wines to add to // their shopping cart include 'include.inc'; set_error_handler("errorHandler"); function showPanel($query, $connection) { // Run the query on the database through // the connection if (!($result = @ mysql_query ($query, $connection))) showerror( ); echo "<table border=0>\n"; // Process the three new wines while ($row = @ mysql_fetch_array($result)) { // Begin a heading for the wine echo "<tr>\n\t<td bgcolor=\"maroon\">" . "<b><font color=\"white\">" . $row["year"] . " " . $row["winery_name"] . " " . $row["wine_name"] . " "; // Print the varieties for this wine echo showVarieties($connection, $row["wine_id"]); // Finish the first row heading echo "</font></b></td>\n</tr>\n"; // Print the wine review if (!empty($row["description"])) echo "<tr>\n\t<td bgcolor=\"silver\">" . "<b>Review: </b>" . $row["description"]; "</td>\n</tr>\n"; // Print the pricing information echo "<tr>\n\t<td bgcolor=\"gray\">"; // Print out the pricing information showPricing($connection, $row["wine_id"]); echo "</td>\n</tr>\n"; // Show the single-bottle add to cart link echo "<tr>\n\t<td align=\"right\">" . "<a href=\"example.cart.3.php?" . "qty=1&wineId=" . $row["wine_id"] . "\">Add a bottle to the cart</a>"; // Show the dozen add to cart link echo " " . "<a href=\"example.cart.3.php?qty=12&wineId=" . $row["wine_id"] . "\">Add a dozen</a></td>\n"; echo "</tr>\n"; // Blank row for presentation echo "\n<tr>\n\t<td></td>\n</tr>\n"; } echo "</table>\n"; } // --------- // Initialize a session. This call either creates // a new session or re-establishes an existing one. session_start( ); // Open a connection to the DBMS if (!($connection = @ mysql_connect($hostName, $username, $password))) showerror( ); if (!mysql_select_db($databaseName, $connection)) showerror( ); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <head> <title>Hugh and Dave's Online Wines</title> </head> <body bgcolor="white"> <?php // Show the user login status showLogin( ); // Show the dollar and item total of the cart showCart($connection); ?> <h1>Here are some Hot New Wines!</h1> <?php // Display any messages to the user showMessage( ); // Show the "Hot New Wines" $query = "SELECT wi.winery_name, w.year, w.wine_name, w.wine_id, w.description FROM wine w, winery wi, inventory i WHERE w.winery_id = wi.winery_id AND w.wine_id = i.wine_id AND w.description IS NOT NULL GROUP BY w.wine_id ORDER BY i.date_added DESC LIMIT 3"; // Include our disclaimer require 'disclaimer'; // Show the user the "Hot New Wines" panel showPanel($query, $connection); echo "<form action=\"example.cart.5.php\"" . " method=\"GET\">\n"; echo "<table>\n<tr>\n"; // If the cart has contents, offer the opportunity // to view the cart or empty the cart. if (session_is_registered("order_no")) { echo "\t<td><input type=\"submit\" " . "name=\"empty\" value=\"Empty Cart\"></td>\n"; echo "\t<td><input type=\"submit\" " . "name=\"view\" value=\"View Cart\"></td>\n"; } // Show the user the search screen button echo "\t<td><input type=\"submit\" " . "name=\"search\" value=\"Search\"></td>\n"; // Show the user either a login or logout button loginButtons( ); echo "\n</tr>\n</table>\n"; echo "</form>\n"; ?> <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>
Copyright © 2003 O'Reilly & Associates. All rights reserved.