Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Shopping Basket / Sessions

Reply
Thread Tools

Shopping Basket / Sessions

 
 
Rob Meade
Guest
Posts: n/a
 
      06-08-2006
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


 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-08-2006
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.


 
Reply With Quote
 
 
 
 
Rob Meade
Guest
Posts: n/a
 
      06-08-2006
"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 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


 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      06-08-2006
<%

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


 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      06-09-2006

"Rob Meade" <(E-Mail Removed)> wrote in message
news:Yj1ig.83123$(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Static Variables for storing Shopping Basket Items sam.thescorpions ASP .Net 1 05-17-2009 04:39 AM
Shopping Basket Jamie Allison HTML 3 03-07-2007 01:03 AM
Shopping Basket Samuel Shulman ASP .Net 3 09-12-2006 10:15 AM
Convert html page or database shopping basket to an email Richard Pain ASP General 9 04-08-2004 09:56 AM
Re: BEST WAY TO implement shopping basket Kevin Spencer ASP .Net 0 08-05-2003 04:23 PM



Advertisments