This shopping cart programming excersise
is designed to help beginning programmers with some common programming
concepts as well as provide more experienced programmers information on ASP's
powerful programming environment and how to set up global arrays for web
applications. The heart of a shopping cart is:
- The Session Object
- Global Variables and Constants
- Two-dimensional Arrays
If you are a real newbie, the first thing every
new ASP developer should realize is that ASP compiles your code and treats the
ASP pages within your site like a running program. It's not just feeding
requested HTML files to your browser, it's actually compiling code at the server
before it sends your browser anything. In this way it reacts to user input
and feeds the user pages based on that input- making it dynamic!
The Almighty Session
ASP's dynamic state is created
and maintained by ASP's "Session" object. It's always
watching the user (Microsoft likes this), and in my opinion, is what make ASP so
incredible. It takes the "stateless" HTTP protocol and through
the use of a teeny cookie, makes a session state so the developer can create
When a user requests an ASP page the server writes a
cookie to the user's machine and assignes them a unique session ID. To create
global variables all you have to do is to create some Session() variables that
are bound to that session ID. If you're getting confused at this point let
me review the process in another way. Here's what happened when you
requested this page:
You type "www.activeserverpages.com/glenncook/index.asp"and
Data Return's server says, "Hey, they want an ASP page and it looks like
the person doesn't have a cookie with a session id so I'm going to send a new
unique ID. Before I send this page let me see if the global.asa file
wants me to do anything special, like connect to a database, create some global
variables whatever. Oh, Glenn wants us to create a couple of global
variables which will be unique to that user's Session.SessionID.
The ID I'm giving this user session is:" 729929829 " (By the way
that's really the user ID Data Return's server gave you. If you don't believe me
close your browser and come back here, you'll see a whole new session ID.
When you close your browser your session cookie also expires. Cool, huh?!)
The Session object starts and ends in the
"Global.asa" file. What is the global.asa file? Very simply,
it's the file that allows the developer to create global variables using ASP's
Session object. The global.asa file is kind of like your config.sys and
autoexec.bat files when you load DOS. The first thing DOS does is look to
these files to free up memory space, start drivers, and execute programs.
ASP looks to the global.asa file in the same way for configuration information
for that user session. Ok, let's see what the global.asa's guts looks
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
ReDim The Cart(Attributes,TotalItems)
Session("MaximumItems") = TotalItems
Session("ItemCount") = 0
here we're using the global.asa's Session_OnStart event to create some
of those global variables I was talking about. But you'll notice
I 'included' some file called cart.inc. INC files are great because ASP
will stick the code from that file right into your ASP page for you.
All I'm doing is creating some constants(like VB) that I assign to the
Session's global variables that I made. I could just as easily
put cart.inc's code into the Session_OnStart event and it would work
just as well to create these variables but I want to be able to call the
array I made in this file in other pages. Oh yeah, the array?!
Don't worry, I'll get to that in a second and IT IS extremely important.
I want you to see that the array here is made with this code:
Session("MaximumItems") = TotalItems
Session("ItemCount") = 0
The elements of the array
are defined by the constants in the "cart.inc" file below.
Understanding the array here is the key to understanding how to make a
*You don't see anything
about the SessionID in the code because ASP does that for you
const TotalItems = 5
const Attributes = 5
Const cartProductID = 1
Const cartProductName = 2
Const cartDescription = 3
Const cartItemPrice = 4
Const cartItemQuantity = 5
OK, this is your
two-dimensional array inc file. To understand 2X arrays think
about Microsoft Excel - it's exactly like a table! This is the
array I just created- right now all the cells are empty because we
haven't put anything in there yet:
There is space for five
"Items" because I made the array/table only have slots for
five items. Most shopping cart programs will resize the
"TotalItems" variable as they add more items to the table but
there's no sense in taking up a lot of memory space if it's not
necessary. Remember, variables are nothing more than spaces in
memory for you to stick information. If I made 1,000 slots for
items in this array it would just slow things down unnecessarily.
If you are new to
programming this is one of the first little bumps in your learning
curve. These types of arrays are very common and you will definitely see
them again and again. The array is usually defined using a Do While
statement and it looks like: sideshowbob(i,1), sideshowbob(i,2) . The i
defines the number of rows(or the number of items) and the number in
parentheses tells you which column to stick the information into.
It the same as telling Excel to stick the info into cell 1A, 1B, 1C etc.
Now, all we have to do is
fill those cells with information. A shopping cart allows a user
to pick items from a database of products/items. Naturally, our
database will have 5 attributes for each product/item, just like the
array, and as the user selects these items for their order, they will
get added to this array. Eventually this array is written to our
Remember that this array is
"alive" only while the user is visting the site because it's
bound to the user's sessionID. If the user ends the session, the
cookie and the global variables are destroyed.
*Note: Sessions are usually
assigned a 20 minute timout by the server administrator. So if
you add items to the global array and leave your browser open while you
go to lunch, after 20 minutes of inactivity ASP will automatically
destroy your session cookie.
Ok, so now you know the secret to a shopping
cart- global variables and a cookie. But you still don't know how to
implement them in an ASP application. Well, until I get a little more time I'm
going to have to leave you hanging. Here's a mini lesson in the meantime
to help you in the right direction.
- Session("Cart"), should hold the
individual productID information (The first part of the array).
- Session("ItemCount"), will
hold the number of items currently in the array (The second part of the
array). Hey, but you said that TotalItems was the other part of the array!
Well, yeah I did, but that array is the table that actually holds the
information you're sticking into the array- the productID(s) and the total
number of products currently in the cart. It's kind of like an array within
The other part of this puzzle is sticking these
items into the array and extracting them when necessary.
1. Go read Charles Carol's database
2. I highly recommend Jim Hoffman's SQL
tutorial. It is incredible and free! Check
Study this scenario very carefully with the info
I've given you and if you beat me to a solution I'll publish it.