Handling two dimensional arrays within session

Results 1 to 2 of 2

Thread: Handling two dimensional arrays within session

  1. #1
    Join Date
    Dec 1969

    Default Handling two dimensional arrays within session

    I&#039m making a _very_ basic shopping cart solution, and I need to store information about the chosen products throughout the session. I suppose I need a session variable to do that. <BR>I also suppose the best way of inserting the data into that variable would be through the use of a two dimensional array, like this:<BR><BR>---------------------------------<BR>Dim Product(0,1)<BR><BR> Product(0,0) = "Product"<BR> Product(0,1) = "Price"<BR><BR>Session("ProductList") = Product<BR>---------------------------------<BR><BR>Right?<BR><BR>Well, how can I later add data to that array? Again, I suppose I have to extract the array from the session variable, ReDim it, and add the new data. What would be the easiest way to do this?<BR><BR>Any help/links appreciated. :)<BR><BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Handling two dimensional arrays within session

    The "trick" to using a multidimensional dynamic array as a session value is really no different than what you do to use it in a single page.<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;CONST numberOfColumns = 2 &#039 or whatever you need<BR>&nbsp;&nbsp;&nbsp;&nbsp;initialNumberOfRow s = 10 &#039 or whatever<BR>&nbsp;&nbsp;&nbsp;&nbsp;rowsInUse = 0 &#039 none so far<BR>&nbsp;&nbsp;&nbsp;&nbsp;Dim cartContents( )<BR>&nbsp;&nbsp;&nbsp;&nbsp;ReDim cartContents( numberOfColumns, initialNumberOfRows )<BR><BR>Now you can store the needed info in the session:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Session("I nUse") = rowsInUse<BR>&nbsp;&nbsp;&nbsp;&nbsp;Session("Cart Rows") = cartContents<BR><BR>In the next page, you recover the data:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;rowsInUse = Session("InUse") = rowsInUse<BR>&nbsp;&nbsp;&nbsp;&nbsp;cartContents = ("CartRows") <BR><BR>And in either the original page or in any other page, you add to the "cart" thus:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 Ready to add a row to the cart<BR>&nbsp;&nbsp;&nbsp;&nbsp;rowsInUse = rowsInUse + 1 &#039 bump to next row<BR>&nbsp;&nbsp;&nbsp;&nbsp;If rowsInUse &#062; UBound( cartContents, 2 ) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&#039 array is full...make it bigger<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;ReDim Preserve cartContents( UBound(cartContents,1), rowsInUse+10 ) &#039 10 rows bigger<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;cartContents(0,rowsI nUse) = ...<BR>&nbsp;&nbsp;&nbsp;&nbsp;cartContents(1,rows InUse) = ...<BR>&nbsp;&nbsp;&nbsp;&nbsp;... etc. ...<BR><BR>*****************************<BR><BR>Si mple as that. The only tricks:<BR><BR>(1) You *must* Dim to no particular size first and then ReDim to the initial size.<BR>(2) When you ReDim Preserve, *only* the *last* dimension of the array can be changed. That&#039s why we put the number of rows, which can change, last and the number of columns, presumably a constant, first.<BR><BR>I show bumping the size of the array by 10 and using a separate variable to keep track of how many rows are actually in use. You don&#039t *have* to do this. In fact, if you will never add more than one row per ASP page, there is little point in doing so. But I like to allow users to purchase several items on a page before hitting a submit and messing up their screen, hence my "bump it by 10 when needed" scheme.<BR><BR>Okay?<BR><BR>--------------------------------<BR>Bill Wilkinson<BR>http://www.ChiliSoft.com<BR>"Pioneering Cross-Platform Active Server Pages"<BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts