shopping cart

1. Bob
Senior Member
Join Date
Dec 1969
Posts
778

## shopping cart

I&#039;m trying to learn how Visual Basic shopping carts work. <BR>Could someone tell me SPECIFICALLY what the 19,1 represent here?<BR><BR>&#060;%<BR>If IsArray(Session("cart")) = false Then<BR> Dim mycart(19,1) <BR> Session("cart") = mycart<BR> response.redirect("error.asp?msg=" & Server.URLEncode("Your shopping cart is empty."))<BR>end if<BR>%&#062;<BR><BR><BR>This was taken from a cart using Access97 database which contains 19 records in the "products" Table, and 9 fields.<BR>Is the code saying, "create a variable called "mycart" with 18 rows, and 0 columns??<BR><BR>Thankyou!<BR><BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
898

## RE: shopping cart

The variable would hold and array of 20 rows and 2 columns since we start counting from 0. So: 19,1 = 10 and 2 as <BR>10,4 = 11 rows 5 columns. Great article on Shopping Carts here:<BR>http://www.saccinc.com/techtalk/articles/art1.htm

3. Senior Member
Join Date
Dec 1969
Posts
898

## error type 19,1= 20,2

The variable would hold and array of 20 rows and 2 columns since we start counting from 0. So: 19,1 = 20rows and 2columns as <BR>10,4 = 11 rows 5 columns. Great article on Shopping Carts here:<BR>http://www.saccinc.com/techtalk/articles/art1.htm

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## A *BIG* mistake, anyway...

As VBScript sees it, that *ACTUALLY* says you have an array of<BR><BR>20 COLUMNS<BR>and<BR>2 ROWS.<BR><BR>Remember VBS starts *all* dimensions at zero, so that says give me an array where the first dimension goes from 0 to 19 (20 columns) and the second dimension goes from 0 to 1 (2 rows).<BR><BR>NOW...<BR><BR>You *can* use that to mean 20 rows by 2 columns, but it&#039;s a big mistake.<BR><BR>REASON: Suppose you needed to add another row to the array? If you read that as 20 Columns, 2 Rows, then you simply do:<BR><BR>&#060;%<BR>ReDim Preserve myCart( 19, 2 )<BR>%&#062;<BR><BR>and now you have 20 columns, 3 rows, with the 3rd row full of blank columns.<BR><BR>THERE IS NO WAY to change the value of the first dimension of the array, so you can NOT do<BR><BR>&#060;%<BR>ReDim Preserve myCart( 20, 1 )<BR>%&#062;<BR><BR>VBS will *NOT* let you do this! <BR><BR>You should look at the ASPFAQs (link at top right of this page), category "Arrays" for more on this, including a special FAQ that I wrote that shows *exactly* how to use a 2D array in VBS as a Session variable value (and, in fact, I use a shopping cart as the example).<BR><BR>FINALLY...<BR><BR>If you do <BR><BR>&#060;%<BR>myCart = someRecordSet.GetRows( )<BR>%&#062;<BR><BR>you will *automatically* get a 2D array with the right number of columns and rows, in the order I specified. Look into the specs for ADODB.RecordSet.GetRows.<BR><BR>Suffice to say, I don&#039;t think must of the code for the "cart" you are using. The author obviously didn&#039;t understand VBS arrays well, I would say. He provides no room, at all, for growth of the "products" table *and* he doesn&#039;t seem to understand that VBS uses the zero element of an array (or maybe he does? else why the ",1" for the second dimension?). Well...in any case, I hate to see fixed size arrays used in Session variables. See the FAQ to learn how to make them dynamic.<BR><BR><BR><BR>

5. peterjl@austec.net.au Guest

## RE: A *BIG* mistake, anyway...

Doesn&#039;t "Dim myCart(19,1)" actually create an array with 19 cols and 1 row?<BR><BR>When you use a progam to reference the elements you go from 0 to 18:<BR><BR>For I=0 to 18<BR>&nbsp;&nbsp;&nbsp;Response.Write MyCart(I,1) & "&lt;br&#062;"<BR>Next<BR><BR>However I agree that it would be ridiculous to use a fixed array, and the cart code that Bob is using is a very bad example for as beginner to be using.

6. Senior Member
Join Date
Dec 1969
Posts
898

## Bill, Peter, check it out...

From the article at: http://www.saccinc.com/techtalk/articles/art1.htm<BR><BR>"Remember earlier I mentioned that we would use a constant length multidimensional array, as opposed to a more complex variable length one. A lot of people would now ask if that is the case, why not use the ReDim Preserve function to dynamically increase the Array variable size?<BR><BR>The answer resides in the VBScript 5.1 Protocols – and here is the excerpt from that document:<BR><BR>If you use the Preserve keyword, you can resize only the last array dimension, and you can&#039;t change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array ."<BR><BR>opinions???<BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Nope, you are thinking of JS...

With VBScript, the DIM you use is the UPPER BOUND of the array, *not* the size of the dimension.<BR><BR>With JavaScript, Java, C, and C++, you do something like<BR>&nbsp; &nbsp; int intArray[ 20 ];<BR>or<BR>&nbsp; &nbsp; var theArray = new Array( 20 );<BR>and you get an array that goes from 0 to 19. But not true of VBScript.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## So the guy isn't very clever...

What the guy is *REALLY* saying is that since *HE* could not figure out the right way to organize the array he couldn&#039;t resize it! DOH!<BR><BR>Did you see his "solution" to the problem:<BR><BR>*** QUOTE ***<BR>And the solution? A little clumsy, declare two identical arrays with an equal upper bound limit:<BR>*** END QUOTE ***<BR><BR>Look at the ASPFAQ in Arrays. It&#039;s truly, truly easy to redim a Cart array *if* you just put the columns as the first dimension and the rows as the second. <BR><BR>Of course, this guy is publishing a site to the web that allows full directory browsing, so I&#039;m not sure I&#039;d trust him too much, anyway. Try these URLs: http://www.saccinc.com and http://www.saccinc.com/techtalk/ and http://www.saccinc.com/techtalk/articles/ just for examples. I *really* don&#039;t think I want these people building any sites for ME!!!<BR><BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Proof...if you need it...

&#060;%<BR>Dim ar(4,1)<BR>For row = LBound(ar,2) To UBound(ar,2)<BR>&nbsp; &nbsp; For col = LBound(ar,1) To UBound(ar,1)<BR>&nbsp; &nbsp; &nbsp; &nbsp; Response.Write "Row " & row & ", Column " & col & "&lt;BR&#062;"<BR>&nbsp; &nbsp; Next<BR>Next<BR>%&#062;<BR><BR>Produces the output:<BR>Row 0, Column 0<BR>Row 0, Column 1<BR>Row 0, Column 2<BR>Row 0, Column 3<BR>Row 0, Column 4<BR>Row 1, Column 0<BR>Row 1, Column 1<BR>Row 1, Column 2<BR>Row 1, Column 3<BR>Row 1, Column 4<BR><BR>**************<BR><BR>LBound isn&#039;t really needed...it&#039;s a holdover from Visual Basic. In VBS, the LBound is *always* zero. (And, incidentally, this explains why DIM specifies the upper bound and not the size: In Visual Basic, you can specify that a one-dimension array has bounds from (say) -10 to +10. A size designation thus doesn&#039;t make sense; a bound designation does.<BR><BR>

10. peterjl@austec.net.au Guest

## RE: Proof...if you need it...

Thanks Bill, you are always a fountain of knowledge. This is a point I have never picked up on. This is probably because I rarely use Dim to create arrays, and more rarely ReDim. I usually create arrays from the GetRows method or the Array function, and I destroy arrays as soon as I am finished with them, to conserve memory. I have not seen a real need for creating a fixed array. Anyway you would need to make the array bigger than the biggest array you imagine for the application. This sounds wasteful.<BR><BR>Anyway I will test it out, just for my own knowledge, but I will probably not use it.

#### Posting Permissions

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