The Shopping Cart and your new best friend...The Session: Part I

Submitted on: 1/5/2015 7:28:00 AM
By: Glenn Cook (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 3550
     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.


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 global variables.
    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 ""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 like.

<!--#INCLUDE file=""-->

Sub Application_OnStart                    
End Sub

Sub Application_OnEnd
End Sub

Sub Session_OnStart

ReDim The Cart(Attributes,TotalItems)
Session("Cart") =TheCart
Session("MaximumItems") = TotalItems

Session("ItemCount") = 0
End Sub

Sub Session_OnEnd
End Sub
Right 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 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'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:

ReDim TheCart(Attributes,TotalItems)

Session("Cart") =The Cart
Session("MaximumItems") = TotalItems

Session("ItemCount") = 0
End Sub

The elements of the array are defined by the constants in the "" file below.  Understanding the array here is the key to understanding how to make a shopping cart.

*You don't see anything about the SessionID in the code because ASP does that for you automatically!



const TotalItems = 5
const Attributes = 5

Const cartProductID = 1
Const cartProductName = 2
Const cartDescription     = 3
Const cartItemPrice = 4
Const cartItemQuantity = 5

The Array!

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:

Attributes>>> cartProductID cartProductName cartDescription cartItemPrice CartItemQuantity

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 database.

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 an array.

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 tutorial.

2.  I highly recommend Jim Hoffman's SQL tutorial.  It is incredible and free! Check it out.

Study this scenario very carefully with the info I've given you and if you beat me to a solution I'll publish it.

Other 7 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.