Suttree.com: Casual Games, Social Software

Read more at the weblog »

PHP Shopping Cart

This is a very simple shopping cart application. There is code in here to create a cart (cart), add to cart (add_item), remove from cart (remove_item), display (display_cart) and destroy the cart (destroy_cart).Cart is built using PHP, MySQL and sessions, providing your installation supoprts those features, this will work just fine.

Usage
There are three files that handle all of the cart functionality. These are;

update_cart.php - this is responsible for listing all the goods for sales (in a very basic table layout)
display_cart.php - this is responsible for displaying the contents of the cart
cart.php - this is the main class that contains all of the logic needed for this application.

There are two auxilliary files, that handle the layout of the pages. These are;
include/top.inc - which contains the HTML for the page header, and
include/bottom.inc - which contains the HTML for the page footer.

So, once the files are in place, you can browse to http://yourwebserver/update_cart.php and you should be presented with a list of good available to purchase. From there on, everything else should be fairly self-explanatory.

Installation
First, decompress the file cart.tar.gz to a directory in your webroot. You will need to create some MySQL database tables, and you can use the following code to do just that.

# Table 'basket'
CREATE TABLE basket (
ID int(4) NOT NULL auto_increment,
product_id int(4) NOT NULL DEFAULT '0' ,
title varchar(100) ,
description varchar(255) ,
price int(4) ,
in_stock int(1) ,
selected_quantity int(4) NOT NULL DEFAULT '0' ,
session_id varchar(100) ,
PRIMARY KEY (ID)
);

# Table 'expired_basket'
CREATE TABLE expired_basket (
ID int(4) NOT NULL auto_increment,
product_id int(4) NOT NULL DEFAULT '0' ,
title varchar(100) ,
description varchar(255) ,
price int(4) ,
in_stock int(1) ,
selected_quantity int(4) NOT NULL DEFAULT '0' ,
session_id varchar(100) ,
expiry_time int(25) ,
PRIMARY KEY (ID)
);

# Table 'sessions'
CREATE TABLE sessions (
ID int(4) NOT NULL auto_increment,
session_id varchar(100) ,
ip_address varchar(15) ,
referrer varchar(255) ,
page varchar(255) ,
last_active int(25) ,
PRIMARY KEY (ID)
);

# Table 'expired_sessions'
CREATE TABLE expired_sessions (
ID int(4) NOT NULL auto_increment,
session_id varchar(100) ,
ip_address varchar(15) ,
referrer varchar(255) ,
page varchar(255) ,
last_active int(25) ,
expiry_time int(25) ,
PRIMARY KEY (ID)
);

# Table 'products'
CREATE TABLE products (
ID int(4) NOT NULL auto_increment,
title varchar(100) ,
description varchar(255) ,
price varchar(8) NOT NULL DEFAULT '0' ,
quantity int(4) ,
in_stock int(1) ,
on_sale int(1) ,
PRIMARY KEY (ID)
);

# Dummy data for table 'products'
INSERT INTO products VALUES("1","Golf clubs","Lovely gold lame, fur-lined golf clubs","25.00","100","1","1");
INSERT INTO products VALUES("2","Golf balls","Pack of 5 golf balls","5.00","200","1","1");
The file 'cart.php' expects to find a directory called 'include' that contains both 'top.inc' and 'bottom.inc', as well as the files 'update_cart.php' and 'display_cart.php' in the same directory as itself.

Download
PHP Shopping Cart can be downloaded from here;
Download PHP Shopping Cart

Version 2
Version 2 isn't planned, but flames, feedback and questions can be directed to me, .

Suttree Beatniks with better clothing, brought to you by Duncan Gough. About Archives Casual Game Dev Code Disclaimer EcoLocal Music Knows PMOG Tumblee
Links for Tuesday, October 14th, 2008