Shopping Basket / Sessions

Discussion in 'ASP General' started by Rob Meade, Jun 8, 2006.

  1. Rob Meade

    Rob Meade Guest

    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
     
    Rob Meade, Jun 8, 2006
    #1
    1. Advertising

  2. 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.
     
    Bob Barrows [MVP], Jun 8, 2006
    #2
    1. Advertising

  3. Rob Meade

    Rob Meade Guest

    "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 :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
     
    Rob Meade, Jun 8, 2006
    #3
  4. Rob Meade

    Rob Meade Guest

    Think I got it!!!

    <%

    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
     
    Rob Meade, Jun 8, 2006
    #4
  5. Re: Think I got it!!!

    "Rob Meade" <> wrote in message
    news:Yj1ig.83123$...
    > <%
    >
    > 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
    >


    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.
     
    Anthony Jones, Jun 9, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: BEST WAY TO implement shopping basket

    Kevin Spencer, Aug 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    400
    Kevin Spencer
    Aug 5, 2003
  2. Samuel Shulman

    Shopping Basket

    Samuel Shulman, Sep 11, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    465
    Grant Merwitz
    Sep 12, 2006
  3. Jamie Allison

    Shopping Basket

    Jamie Allison, Mar 5, 2007, in forum: HTML
    Replies:
    3
    Views:
    344
    Adrienne Boswell
    Mar 7, 2007
  4. sam.thescorpions
    Replies:
    1
    Views:
    379
    shawpnendu
    May 17, 2009
  5. Richard Pain
    Replies:
    9
    Views:
    153
    Roland Hall
    Apr 8, 2004
Loading...

Share This Page