Creating an Online Shopping Cart Mechanism in PHP | 2 | WebReference

Creating an Online Shopping Cart Mechanism in PHP | 2


Creating an Online Shopping Cart Mechanism in PHP [con't]

In this last section, we are going to look at the remaining three scripts of the shopping cart:

  • Showcart.php
  • Orders.php
  • Delete.php

On the previous page, we looked at the addtocart.php script, which basically connected the shopping front with the shopping cart. The addtocart.php script is mainly responsible for collecting items that a user wants to buy. It is closely connected with the showcart script, which shows the contents of the shopping cart. To recap, here's the code for the addtocart script:

Towards the end of the script, you can clearly see where the connection with the showcart script appears. Basically, the script inserts the form values that it has been sent and then immediately redirects the user to the showcart script.

Here's the code for the showcart script:

First off, the purpose of the showcart page is to show the contents of the shopping cart. To do this, it needs the session ID and the current date. Looking at the code, the very first line in the script calls the connect script that also contains code to start the session:

This is very important because the session ID is critical to identifying the user; if the session is not started, we of course will not be able to identify the user. The showcart page provides two buttons for the user: one is an option to continue shopping and the other is to check out. Both these options are dealt with in the following piece of code:

Now, we need to retrieve all the items that the user intends to buy, but there is a slight issue here. Information about books is kept in different tables so we cannot just run a straightforward query that gets all its data from the books table. So what I did here was to use a more complex query to retrieve book data:

The query uses the INNER JOIN construct to retrieve different pieces of information from two tables, and it uses the session ID and the day's date to identify the user. For a more accurate user identification, it is best to use a login script. The $td variable is stored in the connect.php script. If the query fails, then the $err variable is set to "true" and the MySQL error is stored. For security reasons, this error is not echoed. It is safer to write the errors to a log or text file:

Otherwise, the returned rows are stored in a variable:

The reason for this is that these results will be used to create a dynamic table in which the shopping cart contents related to this user will be displayed. So let's get on to the HTML code.

Within the body tags of the page, the table headers are created:

Then before creating the dynamic rows of the table, a variable called $gtotal is initialized. This variable will be the running total of the total cost of the books:

Then the dynamic rows are created:

Look at the following block of code very carefully. This is were the grand total is worked out:

What basically happens is that the quantity and price of a book is worked out and stored in the $total variable. Then the number is formatted using the number_format() function. Next, the $ctotal is added to the $gtotal variable, and then the $gtotal is stored in the session variable. These steps are taken with every iteration. The following code simply provides an option to delete an item from the cart:

If no matches are found, the user has not selected anything to buy, so an appropriate message is displayed:

Then the value of the total shopping cart is shown, together with a form that provides the user with an option to check out or continue shopping:

If an error occurred, then nothing else but that error message will be shown:

The next script we are going to look at is orders.php. The link below shows a screenshot of the checkout screen.

The Checkout Screen

Here is the code that makes the above possible:

On the orders page, a form is displayed that collects information for the orders table. Basically, all the fields on the form will match those in the database. When the user has completed the form and submitted it, the PHP code immediately escapes them using the mysql_real_escape_string() function and then adds them to the orders table.

The delete script simply takes the session ID and cart ID, removes the records from the cart_track table, and then returns the user to the showcart page:


With a functioning shopping cart on your e-commerce application, you can easily start selling goods online using payment services such as PayPal, which offers a cheap payment transfer service. In upcoming articles, I will discuss how to maintain an online shop.

Read the previous articles in this series to get a complete instruction set for building an entire online store in PHP: