Shopping Basket / Sessions

R

Rob Meade

Hi all,

I need to implement a simplistic shopping basket/checkout thingy...

I kinda threw something together, but its not keeping more than one item in
the session...

I'm basically loading the page, checking to see if there's an "addToBasket"
value in the querystring, if there is then I grab the product code (also in
the querystring) - now - here's where it seems to be going wrong...

I figured to keep a multitude of product codes in the session variable I'll
need to store an array...so, I first check to see if the
session("BasketItems") is "" / nothing - if so, I dim in an array, if not
then I set a variable to equal the session("BasketItems")...

I then get the UBound of the array, add 1 to it, and do a ReDim Preserve,
and add the product code from the querstring to the array at that
position...

Everytime the page loads I only ever have one item in it..

Anyone got any ideas? I must be doing something stupid here...

Any info appreciated..

Regards

Rob
 
B

Bob Barrows [MVP]

Rob said:
Hi all,

I need to implement a simplistic shopping basket/checkout thingy...

I kinda threw something together, but its not keeping more than one
item in the session...

I'm basically loading the page, checking to see if there's an
"addToBasket" value in the querystring, if there is then I grab the
product code (also in the querystring) - now - here's where it seems
to be going wrong...

I figured to keep a multitude of product codes in the session
variable I'll need to store an array...so, I first check to see if the
session("BasketItems") is "" / nothing - if so, I dim in an array, if
not then I set a variable to equal the session("BasketItems")...

I then get the UBound of the array, add 1 to it, and do a ReDim
Preserve, and add the product code from the querstring to the array
at that position...

Everytime the page loads I only ever have one item in it..

Anyone got any ideas? I must be doing something stupid here...

Any info appreciated..

Regards
How was the array initially Dim'ed? Only dynamic arrays, ie those dim'ed
with no dimensions in the parentheses will preserve data in redims.
 
R

Rob Meade

...
How was the array initially Dim'ed? Only dynamic arrays, ie those dim'ed
with no dimensions in the parentheses will preserve data in redims.

Hi Bob,

Only seems like yesterday you were solving my problems - turns out it was a
couple of days ago :eek:D

Ok - I've made a little progress since posting, although not a lot....I
currently seem to lose the first item added, although the array size looks
ok...

Here's the code:

If Request.QueryString("action") = "addToBasket" Then

Dim arrBasket

arrBasket = Session("BasketItems")

BasketItems = UBound(arrBasket) ' this line typically fails on the first
load of the page if there is nothing in the basket already, ie no array in
the session variable - but no error is displayed

If BasketItems = "" Then

BasketItems = 1
ReDim arrBasket(1) ' size it up as we need to add an item

Else

ReDim Preserve arrBasket(BasketItems+1) ' size it up again based on what
it is plus 1
Response.Write UBound(arrBasket)

End If

arrBasket(BasketItems-1) = Request.QueryString("product") ' need to deduct
one now so that hte first item goes into arrBasket(0) etc

Session("BasketItems") = arrBasket

End If
 
R

Rob Meade

<%

If Request.QueryString("action") = "addToBasket" Then

Dim arrBasket

arrBasket = Session("BasketItems")

BasketItems = UBound(arrBasket)

If BasketItems = "" Then

BasketItems = 0
ReDim arrBasket(BasketItems+1)

Else

ReDim Preserve arrBasket(BasketItems+1)
Response.Write UBound(arrBasket)

End If

arrBasket(BasketItems) = Request.QueryString("product")

Session("BasketItems") = arrBasket

End If

%>

I now get the items added correctly to the array...

Example here - there's 3 links on the page in the main area, each has a
different product code (for testing purposes) - the "basket" is displayed at
the bottom of the main content area for now (again, for testing)....seems ok
right?

http://82.46.23.70/parasolit/training/default.asp?pageid=168&product=a2km01

Rob
 
A

Anthony Jones

Rob Meade said:
<%

If Request.QueryString("action") = "addToBasket" Then

Dim arrBasket

arrBasket = Session("BasketItems")

BasketItems = UBound(arrBasket)

If BasketItems = "" Then

BasketItems = 0
ReDim arrBasket(BasketItems+1)

Else

ReDim Preserve arrBasket(BasketItems+1)
Response.Write UBound(arrBasket)

End If

arrBasket(BasketItems) = Request.QueryString("product")

Session("BasketItems") = arrBasket

End If

%>

I now get the items added correctly to the array...

Example here - there's 3 links on the page in the main area, each has a
different product code (for testing purposes) - the "basket" is displayed at
the bottom of the main content area for now (again, for testing)....seems ok
http://82.46.23.70/parasolit/training/default.asp?pageid=168&product=a2km01

Rob

Rob,

Your use of variable names is confusing. The variable BasketItems is a
count of items in the basket but the session variable key for the array is
also called BasketItems.

Also you are making the array one element bigger but then place the product
in the element previous to the one added, is this intentional?

In cases such as this I tend to ignore the 0 element of an array and treat
it as one based, things tend to make more sense that way.

Try this refactoring:-

<%

If Request.QueryString("action") = "addToBasket" Then

Dim arrBasket
Dim lItemCount

arrBasket = Session("BasketItems")

If IsEmpty(arrBasket) Then ReDim arrBasket(0)

lItemCount = UBound(arrBasket) + 1

Redim Preserve arrBasket(lItemCount)

arrBasket(lItemCount) = Request.QueryString("product")

Session("BasketItems") = arrBasket

End If

%>

Just remember that when enumerating the array use For i = 1 To
UBound(arrBasket)

One advantage is if there are no products in the array then the array has
UBound = 0.

Anthony.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top