Arrays N stuff :(

# Thread: Arrays N stuff :(

1. Senior Member
Join Date
Dec 1969
Posts
286

## Arrays N stuff :(

Anyone any good with arrays ? <BR><BR>Its a bit hard to explain so forgive me if its not to clear<BR><BR>I need to count how many ITEM_USERID&#039;s are the same so lets say it holds 3 differnt USERID,s 1, 2 & 3 and we have 10 x 1&#039;s 4 x 2&#039;s and 20 x 3&#039;s <BR><BR>how would i find this out in the code below ?<BR><BR>############# Code ######################<BR><BR>Dim cartArray <BR>cartArray = Session("cartArray")<BR>CART_COLUMNS = 4<BR>CONST ITEM_NAME = 0<BR>CONST ITEM_PRODID = 1<BR>CONST ITEM_USERID = 2<BR>CONST ITEM_PRICE = 3<BR>CONST ITEM_QUANTITY = 4<BR>For iRowLoop = 0 to UBound(cartArray, 2)<BR>i = RowCount MOD 2<BR>If i &#060;&#062; 0 Then<BR>RowColor = "#E5E3E3"<BR>Else<BR>RowColor = "#CCCCCC"<BR>End If<BR>If cartArray(ITEM_QUANTITY, iRowLoop) &#062; 0 Then<BR><BR>&#039; Display Cart Contents HERE AND COUNT GROUPS OF ITEM_USERID<BR><BR>End If<BR>Next<BR>

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

## Ughhh...

The traditional way would be to first sort the array and then it&#039;s easy to do counts like this.<BR><BR>But 2D arrays are a pain to sort (and really really slow to do so, too).<BR><BR>It might be best to pull just that one column into its own array, sort that array, and then do the counting.<BR><BR>Another way would be to use a dictionary object.<BR><BR>How often do you need to do this?<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
286

## LOL That good hey Bill :)

Needs to be done often <BR><BR>You have been helping me with this same array for 2 years now & I still dont get it :)

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

## Maybe it's time...

...to redesign the whole thing?<BR><BR>This isn&#039;t *really* a shopping cart, is it?<BR><BR>Maybe if you explained what it is and what it&#039;s used for???<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
286

## RE: Maybe it's time...

Well I have used on several missions in the past as a shopping cart yes :) <BR><BR>This time round its a little differnt as its for mutiple sellers the plan is sellers sign up and adds there products set a delivery cost for 1 - 5 items 6 - 10 and 10+ then when the buyer has finished adding items to there cart I want to caculate the delivery costs since the cart can contain mutilple sellers products i need to group the sellerID&#039;s together and count how many products they have from each seller then caculate the delivery costs.<BR><BR>http://www.future-websites.com/frags/ (thats it in action well as far as I have got)

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

## Oh, but then it's only needed...

...when you go to calc the delivery costs? That&#039;s not so bad. I thought you meant you&#039;d need it on every page.<BR><BR>Okay, then, let&#039;s use brute force:<BR><BR>&#060;%<BR>Set groups = Server.CreateObject("Scripting.Dictionary")<BR><BR >For row = 0 To UBound( cartArray, 2 )<BR> itemid = CStr( cartArray( SOMECOLUMN, row ) )<BR> qty = cartArray( OTHERCOLUMN, row ) <BR> If groups.Exists(itemid) Then<BR> groups(itemid) = groups(itemid) + qty<BR> Else<BR> groups.Add itemid, qty<BR> End If<BR>Next<BR><BR>For Each itemid In groups<BR> Response.Write "Total quantity for " & itemid & " is " & groups(itemid) & "&#060;br/&#062;"<BR>Next<BR>%&#062;<BR><BR>UNTESTED.<BR><BR >

7. Senior Member
Join Date
Dec 1969
Posts
286

## :) Thanks Bill

Cheers Bill :) <BR><BR>your a Star

#### Posting Permissions

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