PDA

View Full Version : simple shopping cart with asp?


quadrant6
11-24-2002, 04:13 AM
Beginner with asp here wanting to build a simple shopping cart.
[ add item, remove item, update quantity etc. ]


I'm just wondering if I can get some advice in terms of what to use i.e sessions, cookies database table? simple and effective would be nice. able to work on all browsers 4+ is a must. I see from some asp sites that sessions are sometimes very frowned upon, I also don't like the idea of a database table filling up with junk becuase of abandoned orders.


:confused:

whammy
11-24-2002, 12:36 PM
I'd use a database if it was a client's site - you can always clean up "incomplete" orders after a certain amount of time (if you're using SQL server you could run a job to do it for you).

Not to mention that way the "incomplete" orders would even be tracked, and perhaps be followed up with an email, i.e. "why didn't you complete your order?".

However if you want to start simply, there's nothing wrong with using sessions, just check first to make sure that cookies are enabled, since they are necessary for sessions in classic ASP (as opposed to ASP.NET).

One of the ways in which sessions/cookies can be dangerous if you rely on them to update data in a database, because of the fact that they might not exist (I know a developer that updated about 50,000 records with one guy's information because he had cookies disabled, and he was using cookie information in a WHERE clause. ;))

Hope this helps.

quadrant6
11-24-2002, 08:53 PM
I was looking at a tutorial [ http://it.maconstate.edu/tutorials/ASP/ASP06/asp06-08.asp ]
It generates a random number for each user. But there is the very slight chance that it could create the same number twice so you would as it mentions, have to check if the number just created has been created before and the only way i can think of doing that is if you store every number that has been generated in a database table itself which seems to me a waste of space and time.

If I just used a database then why not use the ID [ primary key ] as the identifier (order number) for each customer? it is guaranteed to be unique.


Is there a basic example anywhere of this type of database? I am a bit confused over how to go about this. A user comes to the site, clicks 'add to cart' now this inserts a new row in the 'cart' table with what information though? obviously the product but what else? how do you 'connect' so to speak this entry to that user without using sessions to identify them? and you would enter a new row for each product added? what about later when they fill out the payment detail
:confused:

I find this very confusing, any info would be appreciated.

whammy
11-24-2002, 08:57 PM
Well, when you add their record to the database (like when they start the shopping cart), then you can retrieve the record id that was just entered by them (I'd use the exact time and ip address in combination, for example - write a blank record or whatever, and then retrieve the id since the variables will be on the same page).

Once you have that, if you want to avoid using sessions, just pass the variable using in a hidden form field whenever they do anything to update the cart.

Anytime an order is completed, just don't pass the variable (redirect, etc.)

A few ideas...

quadrant6
11-25-2002, 02:42 AM
o.k in the case of using sessions to handle the cart. I would need the session variable to hold 2 things?:

1. ItemID
2. Quantity (default of 1)

When I click the 'add to cart' link next to an item it inserts a new entry into the session variable containing that ItemID and quantity of 1.

so the session variable would be an array, and each item 'added to cart' would insert a new 2 part value (itemID+quantity default of 1)

Would i first do something like: (really not sure on code) :confused:

--------------------------------------------------------------global.asa-------
Sub Session_OnStart

..?make new session variable -> Session("cartItems") .. which can hold 2 things for each item = the ItemID and quantity.. ?

-----------------------------------------------------------------------------------



-----------------------------------------------------------'add to cart' link----

<a href="cart.asp?ItemID=45">add to cart</a>

-----------------------------------------------------------------------------------



-------------------------------------------------------------------cart.asp------
ItemID = Request.QueryString("ItemID")

...?insert this new item, ItemID and default quantity of 1 into the Session("cartItems") array....?

------------------------------------------------------------------------------------



If I actually knew the code, this would work wouldn't it? at least for adding items.

whammy
11-25-2002, 11:42 PM
Hmm... there are a number of ways to do this, you might want to check out Scripting.DictionaryObject on:

http://www.w3schools.com/asp

That site is awesome. :)