By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,501 Members | 2,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,501 IT Pros & Developers. It's quick & easy.

Shopping Basket / Sessions

P: n/a
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
Jun 8 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Rob Meade wrote:
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.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jun 8 '06 #2

P: n/a
"Bob Barrows [MVP]" wrote ...
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 :oD

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
Jun 8 '06 #3

P: n/a
<%

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/trainin...product=a2km01

Rob
Jun 8 '06 #4

P: n/a

"Rob Meade" <te*********************@edaem.bbor> wrote in message
news:Yj*****************@text.news.blueyonder.co.u k...
<%

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/trainin...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.
Jun 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.