Shopping Cart with Arrays & Structures/E-Commerce - Part 1
Shopping Cart/E-Commerce Tutorial
Using Arrays & Structures
-Part 1 of 3-
By Michael Bamberger

This tutorial uses arrays and structures to develop a shopping cart system. This tutorial also contains web-front order processing (e-commerce) information that most of you will probably find very helpful alongside the shopping cart. If you have not yet read CJ's arrays and structures tutorials, I recommend you read them before reading this one (Part 1 Part 2 Part 3).

This tutorial comes from an eclectic group of sources. I utilized Macromedia's LiveDoc's, CJ's tutorials, as well as many other tutorial information when I first built a full shopping cart system. With that in mind, some of my coding may seem familiar from elsewhere, and I want to acknowledge that I took parts of my coding from other sources when I initially wrote all of this coding. At this point I cannot cite exactly which parts are from elsewhere, but just know that some parts are.

Additionally, this tutorial utilizes a processing system in which people who shop must sign up as members. The coding is written so it is not a nuisance for customers to sign up as they check out. The code should be easily understood and editable so customers do not have to sign up before purchasing. I did this because I have found it more convenient for customers to store their information, and its more secure as it only uses one address for both billing and shipping.

Be prepared that this is a very long tutorial, and it contains A LOT of information. If you are only interested in the concepts of an array and structure driven shopping cart, browse to the Main Shopping Cart and Add to Cart sections. I would recommend reading the entire tutorial. You will most likely discover some very useful tools that you can use when dealing with shopping carts and in other coding areas. I apologize, but brevity and e-commerce are by definition enemies. Don't let the length scare you away. It is very easy once you look at the coding and read the explanations. Even though it seems a laborious task, read this tutorial if you really want to understand shopping carts and the way in which virtually all ColdFusion e-commerce sites are written.

After reading all of that, and with a decent knowledge of arrays and structures, you can now move on to the real tutorial. Feel free to browse using the index to go to the parts that interest you.

Tutorial Index
#Section Title
1. Database Layout
2. Member Sign Up
3. *Main Shopping Cart Page
4. *Product Display - "Add to Cart"
5. Checkout - Order Processing
6. Supplemental Coding - Navigation Include

This part of the tutorial deals with setting up your database and signing up members.

Section 1 - Database Layout
For the sake of making this tutorial pretty comprehensive, I am going to give a fairly large database example. There are some extraneous columns in the product table that aren't necessary for the shopping cart system to work, but it will make your site be organized in many ways.
The database I'm describing here is a Microsoft Access 2000 database with 3 tables: LCustomers, LProducts, and LOrders.

Below is the LCustomers table, which holds all the customer information so they can log-in and place orders.
LCustomers
#Column NameTypeDescription
1. c_id AutoNumber Unique id number
2. c_fname Text First name
3. c_lname Text Last name
4. c_street Text Street address
5. c_city Text City
6. c_state Text State
7. c_zip Number Zip/Postal Code
8. c_phone Text Phone number
9. c_email Text E-mail address
10. username Text Username (I set it to equal the e-mail)
11. password Text Selected password
12. cc_type Text Credit Card Type (AmEx, Visa, etc.)
12. cc_num Number or Text 15-digit Credit Card Number
13. cc_verify Number Credit Card Verification Code (CVV2) - usually needed
14. cc_expir_m Number Expiration month as a number
15. cc_expir_y Number Expiration year as a number

Please note: you should not use "username" and "password" as database fields, but for a matter of simplicity I used them here.

Below is the LProducts table, which holds all of the products that will be for sale on your site.
LProducts
#Column NameTypeDescription
1. p_id AutoNumber Unique id number
2. p_name Text Product name
3. p_type Text Type of product. This would be something like category (i.e. Electronics).
4. p_brand Text Brand name of the product
5. p_price Currency The price of the item


Below is the LOrders table, which holds all of the order information when it is processed by your site.
LOrders
#Column NameTypeDescription
1. o_id AutoNumber Unique id number
2. o_cust Number Customer number - Unique ID number from LCustomers table
3. o_items Memo All of the items added in an order - inserted as a list
4. o_date Date/Time Date and time of order - put Now() as default value for this field
5. o_total Currency The total price of the order


Ok, now that you're database is all set up (the easy part), we can move onto the actual coding of the site, starting with the Member sign up page. If you don't plan on using the member system, you can skip through here.

Section 2 - Member Sign Up
These are the scripts to sign up the members to your site. This will be expanded on in the Checkout section of the tutorial, but for now you just need the scripting.

new_member.cfm - Form page to sign up the user
<table width="500" border="0" align="center" cellspacing="0" cellpadding="5" style="font-weight:bold;border:2px solid #DDDDDD;">
<tr style="background-color:#FFFFFF;">
<td>First Name:</td>
<td><input type="text" name="c_fname" value=""></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>Last Name:</td>
<td><input type="text" name="c_lname" value=""></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>Address:</td>
<td><input type="text" name="c_street" value=""></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>City:</td>
<td><input type="text" name="c_city" value=""></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>State:</td>
<td><input type="text" name="c_state value=""></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>Zip:</td>
<td><input type="text" name="c_zip" value="" OnKeyPress="if(((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;"></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>Phone:</td>
<td><input type="text" name="c_phone" value=""></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>E-mail:<br><div style="font-weight:normal;font-size:8pt;">Will Be Your User Name</div></td>
<td><input type="text" name="c_email" value=""></td>
</tr>
<tr style="background-color:#FFFFFF;">
<td>Password:</td>
<td><input type="password" name="password" value=""></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td colspan="2" style="font-weight:normal;font-size:9pt;border-bottom:1px solid #DDDDDD;">Optional Credit Card Information</td>
</tr>
<tr style="background-color:#EEEEEE;">
<td>Type:</td>
<td><select name="cc_type">
<option selected value="">Select Credit Card</option>
<option value="American Express">American Express</option>
<option value="Visa">Visa</option>
<option value="MasterCard">MasterCard</option>
</select>
</td>
</tr>
<tr style="background-color:#EEEEEE;">
<td>Number:</td>
<td><input type="text" name="cc_num" value="" OnKeyPress="if(((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;" maxlength="15"></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td>CVV2</td>
<td><input type="text" name="cc_verify" value="" OnKeyPress="if(((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;"></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td>Expiration:<div style="font-weight:normal;font-size:8pt;">(mm/yy)</td>
<td><input type="text" name="cc_expir_m" size="4" value="" maxlength="2" OnKeyPress="if(((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;"> <input type="text" name="cc_expir_y" size="4" value="" maxlength="4" OnKeyPress="if(((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Sign Up"> <input type="reset" value="Clear"></td>
</tr>
</table>
</form>

I'm rushing through this part. This is nowhere near the crux of the tutorial, and, if you're reading this tutorial and can comprehend it, this should all be pretty self-explanatory to you. It is just the form coding with some javascript to prevent unwanted entries (numbers and letters).

Below is the coding to add the customer into the database.
new_member_process.cfm - Action page to enter the user into the database.
<cfif form.c_fname EQ "">
<script>
alert("Please enter your first name.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.c_lname EQ "">
<script>
alert("Please enter your last name.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.c_street EQ "">
<script>
alert("Please enter your street address.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.c_city EQ "">
<script>
alert("Please enter your city.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.c_state EQ "">
<script>
alert("Please select your state.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.c_zip EQ "">
<script>
alert("Please enter your zip code.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.c_phone EQ "">
<script>
alert("Please enter your phone number.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.c_email EQ "">
<script>
alert("Please enter your e-mail address.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>
<cfif form.password EQ "">
<script>
alert("Please enter a password.");
self.location="Javascript:history.go(-1)";
</script>
</cfif>

<cfif form.c_fname NEQ "" and form.c_lname NEQ "" and form.c_street NEQ "" and form.c_city NEQ "" and form.c_state NEQ "" and form.c_zip NEQ "" and form.c_phone NEQ "" and form.c_email NEQ "" and form.password NEQ "">
<cfquery name="DupCheck" datasource="test">
SELECT c_email
FROM LCustomers
WHERE c_email = '#form.c_email#'
</cfquery>
<cfif DupCheck.recordCount GT "0">
<script>
alert("That e-mail address is already registered.");
self.location="Javascript:history.go(-1)";
</script>
<cfelse>
<cfif Not IsDefined('form.cc_type')><cfset form.cc_type EQ ""></cfif>
<cfif Not IsDefined('form.cc_num')><cfset form.cc_num EQ ""></cfif>
<cfif Not IsDefined('form.cc_expir_m')><cfset form.cc_expir_m EQ ""></cfif>
<cfif Not IsDefined('form.cc_expir_y')><cfset form.cc_expir_y EQ ""></cfif>
<cfif Not IsDefined('form.cc_verify')><cfset form.cc_verify EQ ""></cfif>

<cfquery name="AddMember" datasource="test">
INSERT INTO LCustomers (c_fname, c_lname, c_street, c_city, c_state, c_zip, c_email, username, password, cc_type, cc_num, cc_verify, cc_expir_m, cc_expir_y, c_phone)
VALUES ('#form.c_fname#','#form.c_lname#', '#form.c_street#', '#form.c_city#', '#form.c_state#', '#form.c_zip#', '#form.c_email#', '#form.c_email#', '#form.password#', '#form.cc_type#', '#form.cc_num#', '#form.cc_verify#','#form.cc_expir_m#', '#form.cc_expir_y#', '#form.c_phone#')
</cfquery>

<!--- Mail Information to User --->
<cfmail
from="newmember@yoursite.com"
to="#form.c_email#"
subject="Thank you for signing up." type="html">
<html>
<head>
<style type="text/css">
Body, table, td {font-family:Arial;font-size:9pt;color;##000000;}
</style>
</head>
<body>
Thank You For Signing Up<br>
Dear #form.c_fname# #form.c_lname#,<br>
Thank you for signing up with Your Company.
Your username and password are below. Be sure to write them down for future reference.<p>
<b>Username:</b> #form.c_email#<br>
<b>Password:</b> #form.password#<p>
Thank you for signing up and we sincerely appreciate your business.
</body>
</html>
</cfmail>

Thank you for signing up. Your details have been e-mailed to you.

</cfif>
</cfif>


This too is pretty self explanatory for an intermediate programmer. It error checks at the beginning to make sure the required fields are entered. If the fields that aren't required are blank it sets them to "". It then adds the user information into the database. Notice that it sets the username to be the e-mail address. Finally, the new user has their information e-mailed to them.

Ok, now we get to the fun part. This is the meat and potatoes of this tutorial - what I'm sure you've all been waiting for. And most of you probably skipped down to that part in the first place. Anyway, pay special attention as you move on to Part 2.


If you have any questions or comments feel free to e-mail me at any time.
Part 1 - Part 2 - Part 3
All ColdFusion Tutorials By Author: Michael Bamberger
Download the EasyCFM.COM Browser Toolbar!