Session Variable for Shopping Cart

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TRB_NV

    Session Variable for Shopping Cart

    I'm losing information from my Session when I change pages or start the same
    page over again. I simplified the code so the example is really clear. The
    sample code that follows is supposed to generate a random number and put it
    into an Array and store it in the Session variable and then when it runs the
    next time, generate another random number and display the entire Array
    contents, by pulling the information out of the Session. I'd greatly
    appreciate someone with fresh eyes taking a look at this code. Thanks.

    <%@ LANGUAGE="VBSCR IPT" %>

    <%
    if Request.Form("c lear") = "true" then
    Session.Content s.Remove("Shopp ingCart")
    Session("ItemCo unt") = 0
    clearcart = ""
    end if

    dim cart(999,9)

    Randomize Timer
    r1 = Rnd
    r1 = r1 * 1000000
    intOrderID = 1000000 + Int (r1)

    intItemCount = Session("ItemCo unt")

    if intItemCount = 0 then
    intItemCount = 1
    Session("ItemCo unt") = intItemCount
    else
    intItemCount = intItemCount + 1
    Session("ItemCo unt") = intItemCount
    end if

    Session("ItemCo unt") = intItemCount

    cart(intItemCou nt,1)=intOrderI D
    Session("Shoppi ngCart")=cart

    xcart=Session(" ShoppingCart")

    for x = 1 to intItemCount
    response.write x & " " & xcart(x,1) & "<br>"
    next

    %>
    <form action="array.a sp" method="post">
    <input type="hidden" name="clear" value="true">
    <input type="Submit" name="Submit" value="Clear Shopping Cart">
    </form>
    <form action="array.a sp" method="post">
    <input type="hidden" name="clear" value="false">
    <input type="Submit" name="Submit" value="Reload">
    </form>


  • Anthony Jones

    #2
    Re: Session Variable for Shopping Cart


    "TRB_NV" <TRB_NV@hotmail .com> wrote in message
    news:O0bYg4gXGH A.196@TK2MSFTNG P04.phx.gbl...[color=blue]
    > I'm losing information from my Session when I change pages or start the[/color]
    same[color=blue]
    > page over again. I simplified the code so the example is really clear.[/color]
    The[color=blue]
    > sample code that follows is supposed to generate a random number and put[/color]
    it[color=blue]
    > into an Array and store it in the Session variable and then when it runs[/color]
    the[color=blue]
    > next time, generate another random number and display the entire Array
    > contents, by pulling the information out of the Session. I'd greatly
    > appreciate someone with fresh eyes taking a look at this code. Thanks.
    >
    > <%@ LANGUAGE="VBSCR IPT" %>
    >
    > <%
    > if Request.Form("c lear") = "true" then
    > Session.Content s.Remove("Shopp ingCart")
    > Session("ItemCo unt") = 0
    > clearcart = ""
    > end if
    >
    > dim cart(999,9)
    >
    > Randomize Timer
    > r1 = Rnd
    > r1 = r1 * 1000000
    > intOrderID = 1000000 + Int (r1)
    >
    > intItemCount = Session("ItemCo unt")
    >
    > if intItemCount = 0 then
    > intItemCount = 1
    > Session("ItemCo unt") = intItemCount
    > else
    > intItemCount = intItemCount + 1
    > Session("ItemCo unt") = intItemCount
    > end if
    >
    > Session("ItemCo unt") = intItemCount
    >
    > cart(intItemCou nt,1)=intOrderI D
    > Session("Shoppi ngCart")=cart
    >
    > xcart=Session(" ShoppingCart")
    >
    > for x = 1 to intItemCount
    > response.write x & " " & xcart(x,1) & "<br>"
    > next
    >
    > %>
    > <form action="array.a sp" method="post">
    > <input type="hidden" name="clear" value="true">
    > <input type="Submit" name="Submit" value="Clear Shopping Cart">
    > </form>
    > <form action="array.a sp" method="post">
    > <input type="hidden" name="clear" value="false">
    > <input type="Submit" name="Submit" value="Reload">
    > </form>
    >[/color]


    You have a hidden field whose value is always true. Hence the code at the
    top of your page that resets everything always runs.



    Comment

    • TRB_NV

      #3
      Re: Session Variable for Shopping Cart

      Remarked out the part about clearing the Session contents, but I'm still not
      getting the data from the Array I stored in it:

      'if Request.Form("c lear") = "true" then
      ' Session.Content s.Remove("Shopp ingCart")
      ' Session("ItemCo unt") = 0
      ' clearcart = ""
      'end if

      From everything I've read, it appears that I can store an Array in a Session
      variable and recall it. Some how it's not working properly.


      Comment

      • Anthony Jones

        #4
        Re: Session Variable for Shopping Cart


        "TRB_NV" <TRB_NV@hotmail .com> wrote in message
        news:%239%231t2 lXGHA.508@TK2MS FTNGP02.phx.gbl ...[color=blue]
        > Remarked out the part about clearing the Session contents, but I'm still[/color]
        not[color=blue]
        > getting the data from the Array I stored in it:
        >
        > 'if Request.Form("c lear") = "true" then
        > ' Session.Content s.Remove("Shopp ingCart")
        > ' Session("ItemCo unt") = 0
        > ' clearcart = ""
        > 'end if
        >
        > From everything I've read, it appears that I can store an Array in a[/color]
        Session[color=blue]
        > variable and recall it. Some how it's not working properly.
        >
        >[/color]

        You've got these lines in your code too

        Session("Shoppi ngCart")=cart

        xcart=Session(" ShoppingCart")

        Are you saying xcart does contain the same data as cart?

        What if you change this to:-

        xcart = cart does it still fail?




        Comment

        • Mike Brind

          #5
          Re: Session Variable for Shopping Cart


          TRB_NV wrote:[color=blue]
          > I'm losing information from my Session when I change pages or start the same
          > page over again. I simplified the code so the example is really clear. The
          > sample code that follows is supposed to generate a random number and put it
          > into an Array and store it in the Session variable and then when it runs the
          > next time, generate another random number and display the entire Array
          > contents, by pulling the information out of the Session. I'd greatly
          > appreciate someone with fresh eyes taking a look at this code. Thanks.
          >
          > <%@ LANGUAGE="VBSCR IPT" %>
          >
          > <%
          > if Request.Form("c lear") = "true" then
          > Session.Content s.Remove("Shopp ingCart")
          > Session("ItemCo unt") = 0
          > clearcart = ""
          > end if
          >
          > dim cart(999,9)
          >
          > Randomize Timer
          > r1 = Rnd
          > r1 = r1 * 1000000
          > intOrderID = 1000000 + Int (r1)
          >
          > intItemCount = Session("ItemCo unt")
          >
          > if intItemCount = 0 then
          > intItemCount = 1
          > Session("ItemCo unt") = intItemCount
          > else
          > intItemCount = intItemCount + 1
          > Session("ItemCo unt") = intItemCount
          > end if
          >
          > Session("ItemCo unt") = intItemCount
          >
          > cart(intItemCou nt,1)=intOrderI D
          > Session("Shoppi ngCart")=cart
          >
          > xcart=Session(" ShoppingCart")
          >
          > for x = 1 to intItemCount
          > response.write x & " " & xcart(x,1) & "<br>"
          > next
          >
          > %>
          > <form action="array.a sp" method="post">
          > <input type="hidden" name="clear" value="true">
          > <input type="Submit" name="Submit" value="Clear Shopping Cart">
          > </form>
          > <form action="array.a sp" method="post">
          > <input type="hidden" name="clear" value="false">
          > <input type="Submit" name="Submit" value="Reload">
          > </form>[/color]

          You're not preserving the array. Every time the page loads, you create
          an array called cart, with 10 columns and 100 rows. Arrays are empty
          when you first declare them. All you are doing is populating row
          intItemCount with a value. That's why, as you refresh the page, you
          see one (changing) value slowly crawling down the page.

          Declaring such a large array as you do, when you can only use 2 cols,
          is massively expensive on memory Each element in an array is a
          variant, and is 16 bytes. You have created a structure in memory that
          will consume 10*100*16 bytes - that's 15.625 kilobytes per session.

          It's much easier, and cheaper to store your order numbers in a comma
          delimited string in one session variable, along with a counter to tell
          you how many there are. Then you can use the split function to convert
          the string to a one-dimensional array and iterate through the elements.

          <%
          Dim orders, i, r1, intOrderID
          Randomize Timer
          r1 = Rnd
          r1 = r1 * 1000000
          intOrderID = 1000000 + Int (r1)

          If Session("Item") = "" Then
          Session("Item") = Session("Item") & intOrderID
          Session("ItemCo unt") = 1
          Else
          Session("ItemCo unt") = Session("ItemCo unt") + 1
          Session("Item") = Session("Item") & "," & intOrderID
          End If

          orders = split(Session(" Item"),",")
          For i = 0 to Session("ItemCo unt")-1
          response.write i + 1 & " " & orders(i) & "<br>"
          Next
          %>

          --
          Mike Brind

          Comment

          • Mike Brind

            #6
            Re: Session Variable for Shopping Cart


            Mike Brind wrote:[color=blue]
            > TRB_NV wrote:[color=green]
            > > I'm losing information from my Session when I change pages or start the same
            > > page over again. I simplified the code so the example is really clear. The
            > > sample code that follows is supposed to generate a random number and put it
            > > into an Array and store it in the Session variable and then when it runs the
            > > next time, generate another random number and display the entire Array
            > > contents, by pulling the information out of the Session. I'd greatly
            > > appreciate someone with fresh eyes taking a look at this code. Thanks.
            > >
            > > <%@ LANGUAGE="VBSCR IPT" %>
            > >
            > > <%
            > > if Request.Form("c lear") = "true" then
            > > Session.Content s.Remove("Shopp ingCart")
            > > Session("ItemCo unt") = 0
            > > clearcart = ""
            > > end if
            > >
            > > dim cart(999,9)
            > >
            > > Randomize Timer
            > > r1 = Rnd
            > > r1 = r1 * 1000000
            > > intOrderID = 1000000 + Int (r1)
            > >
            > > intItemCount = Session("ItemCo unt")
            > >
            > > if intItemCount = 0 then
            > > intItemCount = 1
            > > Session("ItemCo unt") = intItemCount
            > > else
            > > intItemCount = intItemCount + 1
            > > Session("ItemCo unt") = intItemCount
            > > end if
            > >
            > > Session("ItemCo unt") = intItemCount
            > >
            > > cart(intItemCou nt,1)=intOrderI D
            > > Session("Shoppi ngCart")=cart
            > >
            > > xcart=Session(" ShoppingCart")
            > >
            > > for x = 1 to intItemCount
            > > response.write x & " " & xcart(x,1) & "<br>"
            > > next
            > >
            > > %>
            > > <form action="array.a sp" method="post">
            > > <input type="hidden" name="clear" value="true">
            > > <input type="Submit" name="Submit" value="Clear Shopping Cart">
            > > </form>
            > > <form action="array.a sp" method="post">
            > > <input type="hidden" name="clear" value="false">
            > > <input type="Submit" name="Submit" value="Reload">
            > > </form>[/color]
            >
            > You're not preserving the array. Every time the page loads, you create
            > an array called cart, with 10 columns and 100 rows. Arrays are empty
            > when you first declare them. All you are doing is populating row
            > intItemCount with a value. That's why, as you refresh the page, you
            > see one (changing) value slowly crawling down the page.
            >
            > Declaring such a large array as you do, when you can only use 2 cols,
            > is massively expensive on memory Each element in an array is a
            > variant, and is 16 bytes. You have created a structure in memory that
            > will consume 10*100*16 bytes - that's 15.625 kilobytes per session.
            >[/color]

            Actually, the array you create is 10 cols by 1 thousand rows so it's
            156.25 kilobytes in memory! If you have a business plan that shows you
            will get 1000 orders per customer per session, lemme see it!

            ;-)

            --
            Mike Brind

            Comment

            Working...