studyanywhere.ca - Online Web Development Courses
Published on studyanywhere.ca - Online Web Development Courses (https://www.studyanywhere.ca)

Home > Chapter 11 Exercises

Chapter 11 Exercises [1]

Exercise 3: A Simple Shopping Cart

In this exercise you will create PHP scripts:

  • a product catalogue, which will allow a site visitor to add items to their shopping cart, and
  • a checkout page, which will display the contents of the user's shopping cart for confirmation.

From the checkout page, in actual practice, the order could then be submitted to a processing system that would handle the details of payment acceptance and shipping arrangements. Such a system is beyond the scope of this course, but if you would like to try one, we recommend using PayPal [2], which is quite easy to set up.

We will look at various important fragments of code first, after which you will see the full script. So don't bother typing any of the following snippets into your editor.

Start with the list of items for sale in the online store:

Script 11-2a: catalog.php (excerpt)

Now, instead of hard-coding the items, place them in a PHP array and generate the page dynamically. Arrays are created with the built-in PHP function array by listing the elements to be included in the array as the parameters of the function. Items and their prices would normally be stored in a database, but we are using this method so we can focus on sessions. You should already know all you need to, for creating a database-driven product catalogue, so we will leave that to you.

Below is the code for the dynamically generated product catalogue:

Script 11-2b: catalog.php (excerpt)

This code produces the HTML we saw above. The table row for each item is created using a for loop that counts through the $items array (the function count returns the number of items in the array). We covered for loops in Chapter 3: Getting Started with PHP ~ Refresher [3], and then in Chapter 6: A Content Management System [4] we saw how to use them with the count function. We also use PHP's built-in number_format function to display the prices with two digits after the decimal point. (See the PHP Manual [5] for more information about this function.)

Now, we are going to store the list of items the user placed in the shopping cart in yet another array. Because we will need this variable to persist throughout the user's visit to your site, we will store it using PHP sessions. Here is the code:

Script 11-2c: catalog.php (excerpt)

session_start either starts a new session (and sets the session ID cookie) or restores the variables registered in the existing session, if one exists. The code then checks if $_SESSION['cart'] exists, and if it does not, initializes it to an empty array to represent the empty cart.

We can now print out the number of items in the user's shopping cart:

Script 11-2d: catalog.php (excerpt)

Now, the user's shopping cart is going to stay very empty if we do not provide a way to add items to it, so modify the for loop that displays the table of items to provide a Buy link on the end of each row:

Script 11-2e: catalog.php (excerpt)

Now each product in the catalogue has a link back to the catalogue with buy=n in the query string, where n is the index of the item that is to be added to the shopping cart in the $items array. We can then watch for the $_GET['buy'] variable in the script. When it occurs, we will add the item to the $_SESSION['cart'] array before redirecting the browser back to the same page, but without a query string, thereby ensuring that refreshing the page does not repeatedly add the item to the cart.

Script 11-2f: catalog.php (excerpt)

This works just fine if the user has cookies enabled, but when cookies are disabled, PHP's automatic link adjustment does not affect the header function, so the session ID gets lost at this point.

Fortunately, if PHP identifies that cookies are disabled, it creates a constant named SID (a string of the form PHPSESSID=somevalue), which will pass on the session ID. We can make our code compatible with disabled cookie support as follows:

Script 11-2g: catalog.php (excerpt)

You just need to add a View your cart link to the appropriate page (cart.php), and you will have completed the catalog script. Script 11-2h shows the complete code for this page:

Script 11-2h: catalog.php


The code for cart.php is very similar to the product catalogue. All it does is take the $_SESSION['cart'] variable (after loading the session) and use the numbers stored there to print out the corresponding items from the $items array. We also provide a link that loads the page with query string empty=1 and causes the script to unset the $_SESSION['cart'] variable, which results in a new, empty shopping cart. Script 11-3 shows the complete code for this page:

Script 11-3: cart.php


← Return to Chapter 11, Assignments Page [6]

  • Home
  • About Us
  • Programs
  • Opportunities
  • Eligibility
  • Privacy
  • Accessibility
  • Workshops
  • Blog
  • Land Acknowledgement

Copyright © 2005-2025 Make A Change Canada/Faire un Changement Canada, All Rights Reserved. Designed with accessibility in mind.

(Make A Change Canada is a certified educational institution under the Income Tax Act [PTIB Exemption; ESDC Certification File No. 7009/13579].)

contact us.

-->


Source URL:https://www.studyanywhere.ca/advanced-courses/mysql-and-php/assignments/chapter-11-refresher-on-cookies-and-session/exercise-3

Links
[1] https://www.studyanywhere.ca/advanced-courses/mysql-and-php/assignments/chapter-11-refresher-on-cookies-and-session/exercise-3 [2] https://developer.paypal.com/ [3] http://www.ibde.ca/advanced-courses/mysql-and-php/assignments/chapter-3-refresher-getting-started-with-php [4] http://www.ibde.ca/advanced-courses/mysql-and-php/assignments/chapter-6-a-content-management-system [5] http://www.php.net/number_format [6] http://www.ibde.ca/advanced-courses/mysql-and-php/assignments/chapter-11-refresher-on-cookies-and-session