Building A Persistent Shopping Cart With PHP and MySQL
If you take a look around any PHP resource site, you'll notice an exorbitant amount of shopping cart scripts. The fact of the matter is that shopping cart scripts aren't hard to develop. In this article Mitchell shows us how to build a simple shopping cart that persists across multiple browser sessions. He uses PHP, MySQL, and JavaScript to implement each part of the shopping cart.
Building A Persistent Shopping Cart With PHP and MySQL - Displaying the items (Page 3 of 6 )
As I'm sure you can guess, displaying the list of items from our items table is fairly easy. Because we will be displaying items on one page, and showing a shopping cart on another page, we will create a file that will store the connection details of the database and two functions to connect to and work with the database. Create a file called db.php and enter the following code into it:
<?php
// This page contains the connection routine for the // database as well as getting the ID of the cart, etc
function ConnectToDb($server, $user, $pass, $database) { // Connect to the database and return // true/false depending on whether or // not a connection could be made.
function GetCartId() { // This function will generate an encrypted string and // will set it as a cookie using set_cookie. This will // also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"])) { return $_COOKIE["cartId"]; } else { // There is no cookie set. We will set the cookie // and return the value of the users session ID
First off, we define four variables to hold the details of our MySQL database's server, username, password, and database name respectively. Next, we have the ConnectToDb function, which uses our database variables and connects to our MySQL database:
Notice how the calls to mysql_connect and mysql_select_db are prepended with the @ symbol. This tells PHP not to spit any errors if the connect fails. If either of the connection or database selection functions fail, then ConnectToDb returns false. Otherwise it returns true, indicating a successful connection.
The GetCartId function makes use of one cookie variable to track a user across multiple sessions. It starts by checking if the cartId cookie variable is set. If not, it grabs the users session ID and sets it as a cookie value which expires in 30 days (the expiry date of the setcookie function is specified in seconds, so (3600 * 24) * 30 means 3600 seconds ( 1 hour) * 24 (1 day) * 30 (1 month)). The GetCartId function is used in combination with the MySQL cart table to track which user has added which items to their cart.
Db.php is included by both our item listing page and cart. The item listing page is called products.php, and starts off like this:
<?php
// This page will list all of the items // from the items table. Each item will have // a link to add it to the cart
include("db.php");
// Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); $result = mysql_query("select * from items order by itemName asc"); ?>
As you can see, we include db.php and call the ConnectToDb function to attain a connection to our MySQL database. Next, we grab the entire list of items from the items table, and store the result in the $result variable.
Each item will be displayed as part of a table. We output some HTML tags and then the beginning row of our table before starting a loop through each item, like this:
Loading up products.php in my browser, I received the following page:
The "Add Item" link for each item links to cart.php. Let's now take a look at the cart.php page, which lets us add, update and delete items from our shopping cart.