PDA

View Full Version : Help With An Array


Thatguy2001au
08-07-2003, 09:40 AM
Hey Guys

Ok, so here is the problem. At the moment, i'm not very good with arrays in asp but i'm trying to learn. I have a shopping cart program i've created with asp and sql server 2000. Everything works fine, adding products, viewing the cart, updating etc. The problem comes when using the Checkout button. When a user hits Checkout on the cart form, I want to store all the items in the cart into an Array and then place that Array into a Session variable called Session("cart"). The following are the items I want to store in the array:

Product_id
Product_name
Product_quantity
Product_saleprice
Product_subtotal

The cart contents are stored in the database for each user and when they view the cart, it just gets them from the table and does a loop for however many products the user has in their cart.

I need to the Array to loop for however many items there are in the cart.

I also have hidden fields in the cart which store the items above that i need in the array which are also in the loop. The names are the same as above but with 'hid' at the beginning, ie hidProduct_id.

How would i create the Array in asp so that it stores every product with in the cart with the above headings? This needs to be put into a Session("cart") afterwards so i can display the array on another page.

On another page, how would i display the Array so that it shows all the products that are stored in the array under the headings listed above???

All help will be greatly appreciated.

Thatguy

raf
08-07-2003, 11:30 AM
Hmm. Why make it so hard on yourself? If you have the items in the db, why do you want them in the session-variable? Databasedriven webapp's are meant to be 'driven' with data from the db. If you have a lott of sessions, then storing and retrieving that from the session-object will place quite some load on the webserver + loading the complete recordset (= all items from his cart) will be slightly slower then only loading 1 record or some agregated data (like the number of items in his cart) or just 1 field for each item, from the db.
It's faster (if you ever notice it) to get the data from the session-object, but it will slow down your app if you have lots of simultanious users.
If you don't have lotts of simultanious users, then there is no reason to not use a db, since you'll get the data from the db-verry quickly and you only need to select and display the data you need on a specific page.

So if you store the cartID or whatever inside a cookie or hidden formfield or the querystring or a session-variable, and you use that to select the records you need, then you have inough info to make your selects.

Just my opinion.

If you do want the array-thing, then i believe you best use GetRows()
http://www.devguru.com/Technologies/ado/quickref/recordset_getrows.html

never used it myself, but it returns a 2 dimensional array. Presto ! No extra code needed. Just
array=rsRecordsetname.GetRows()
or
session("cartarray")=rsRecordsetname.GetRows()
To get the values, you can then use for instance
array (0,0) or session("cartarray(0,0)")
to get the Product ID from the first record.
array(1,0)
to get the Product Name from the first records
array(0,1) to get the Product ID from the second record etc

Easy, but i'd really recommend using the db.

Thatguy2001au
08-07-2003, 12:19 PM
Thanks Raf

There are a couple of reasons as to why i wanted to place the cart contents into an array. The first being, is when checking out and then going to a secure server or a payment gateway, i am not sure whether or not i still have access to my database to get the users details or cart contents and prices etc. Maybe i don't even need to access my database then and instead, the secure server or payment gateway people give me a script which allows me to attach the users infor and cart info to some sort of variable which gets sent to them when a user clicks on checkout. I'm a little fuzzy on how this process works.

The second reason was just to learn and see how it's done. The website u recommended helped and allowed me to get it to work using the getrows function.

Thanks alot for your help. Just one other thing, if you really need to use sessions in a website, is it better use Sessions or just write the data into cookies which expire when the user's session expires?

Thanks

Thatguy

Spudhead
08-07-2003, 01:00 PM
Use sessions variables. It's easier. But DO NOT store arrays in session variables - this is very bad practice and WILL cause you no end of hassles.

Thatguy2001au
08-07-2003, 01:07 PM
Hey Spudhead

You say not to store arrays into session variables, why is this??? I've gotten it to work, but if u think it will only cause me troubles and that there are disadvantages to it, then I will stop doing it.

If i don't put the array into a session variable, how would i pass that array to another page so i can read the contents of the array etc on a different page to the one that created the array.

That's the main reason i put the array into a session variable so that I can access it from any page as long as the session is active. But if there is another way i don't know of, then please let me know. I'm in the learning process and i don't know how else to do it.

Your help will be appreciated

Thanks

Thatguy

raf
08-07-2003, 02:38 PM
but if u think it will only cause me troubles and that there are disadvantages to it, then I will stop doing it.
So if spudhead tells you it's a no-go, then you concider dropping it :)

The deal is that it will indeed work, but try running that sort a settup when you have 100 simultanious users... Then it will slow down your machine + after a while it becommes hard to code since these arrays aren't associative (no variabelnames) so your working with (1,0) etc and sooner or later, you'll make mistakes.

I prefer sessions over cookies. (sessions rely on cookies somehow but i mean application defined cookies here) I like to do everything serversided. I never use cookies (not everyone has them enabled). But inside session-variabels, you only need to store stuff you use on multiple pages, and preferably just references. Like the userID or cartID or so. This way, the instances of the session-object only contain a few variables with smal values.
I believe it's better to run 5 additional (smaller) querys then to return one big recordset and store that in a sessionobject.

I'd inform with the secure server people on how you can deliver the info and what info they need. It's kinda unclear to me what settup you got there, but if this secure server is another machine, then i can't imagen you can still access the session-object on your webserver.