Building an Online Shop's Product Detail Page / Page 2 | WebReference

Building an Online Shop's Product Detail Page / Page 2


Building an Online Shop's Product Detail Page [con't]

Creating a shopping cart Mechanism – The Database

So far, we have created a fully functioning shop front for our bookshop. A user can currently view the books in their various genres, as well as select an individual book for more detailed viewing. What the user can't do is to actually buy a particular book…and this is exactly the kind of functionality that we want to give our application in the next couple of articles. In this section, we are going to look at how to create the tables that we will need for the shopping cart of our online shop.


So what does a shopping cart actually do? A shopping cart must fulfill these basic requirements:

  • Allow the customer to add items to the cart
  • Allow for different quantities of each item
  • Allow the customer to alter the quantities of an item
  • Allow the customer to remove items from the cart

These are only the basic requirements of a typical shopping cart, you might add more depending on your needs.

The Database Tables

We already created a database in the previous article, so in this section we will not be creating a new database, because we actually want to link this shopping cart to the shop front application that we created earlier. First of all, we need a table that will hold information about the items that a particular user bought. We need a table that will:

  • Somehow identify the user
  • Hold the name and other information of the books that the user wants to buy
  • Hold the quantity of the books that the user wants to buy.
  • Store the date on which the user intends to buy a book.

Now, because I did not create a login script for this online shop, we need to find another way to identify a user of our online shop. The only other way, in the absence of a login script, is to use sessions. Sessions are started simply by calling the session_start() function and when you call this function a session ID is generated. The sessionID has about 32 alphanumeric characters. This id lasts throughout the execution time of a particular script. So far, we have worked out that we need:

  • To identify the user that is currently browsing the books
  • Provide the names of the books that the user wants to buy
  • Store the quantity of books that a user wants to buy

This should give us some indication of what kind of table we should create to track a user's purchases. It should go something like this:


While the table structure above meets the requirements for tracking a user, it needs some optimization. For example, we know that databases work faster when dealing with numbers instead of text, therefore, if we remove all the information about a book (i.e the fields named books_*) that the user intends to buy and simply add the book id to the table, it will work equally effective but faster. Once we have the book id, we can simply run a query and retrieve all the information about the book from the books table. Therefore, the revised table should have the following fields:


Why the "date_added" field? This is just to strengthen the method of identifying the user. If we use the sessionID and date, we can identify the user more accurately. Now, copy and paste the following to create the cart_track table:

With the following sample data:

The second requirement for a shopping cart is when the user decides to checkout, at which point you should take the users name, mailing address and credit card details. We need a table that will hold a record of all of the data (except credit card numbers). A rough outline of the table would look something like this:

NOTE: In this application, we are not going to deal with actual payment of the order. See the Payments section below.

The outline above more than adequately meets the requirements; but please feel free to add more fields to suit. Our final table will look something like this:

Some sample data: