Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't wrap my head around this array/string problem...

Status
Not open for further replies.

andrewEmbassy

Programmer
Aug 2, 2005
15
US
Okay, so here's my issue. I'm working on a shopping cart and we've got it set up so the users cart is a cookie kept on their machine, with they key names being the itemId's and the values being the number of each they've purchased.

I'm writing the receipt page that'll have "thank you! your order went through fine, here's what you've purchased... etc."

Now, we're using a third party credit card verification system, and the receipt page is hosted on their server so the cookie from our domain won't work. Also, there's very limited variables we can pass to the third party server about the purchase- aside from the amount and billing/shipping info, there's not much else the server will take.

It seems like we've got two options (without throwing the whole cart system out the window and creating carts on the database, something that I'm still contemplating).

One: I can figure out how to set multiple domains on the cookie. I've been told this is kind of a difficult thing to do and that I may run into browser issues along with security issues. Is there any way I can write a cookie so it can be read by multiple domains?

two: I can take my cart, convert it into a string, post that string in the x_description variable they give me, and then when it's echoed to the receipt page compile that string into an array and use it to populate the info.

This option seems like it would be the simplest, but while I could use split() to make a one dimensional array, I need to use it to make a multi-dimensional array. Ideas? I can write the string fine, right now I've got it something like this:

SM150=>1,SM344=>3,SM250A=>2 Where SM150 is the key and 1 is the value.

How do I get that into a multi-dimensional array?

Thanks in advance...
 
i think splitting the string you got and using arrays would be the easiest option here...

so first you need to split at "," and then you want to split it at "=>"

-DNG
 
why not just do this:

Code:
mystring="SM150=>1,SM344=>3,SM250A=>2"
mystring=Replace(mystring,"=>",",")

myarray=split(mystring,",")
for i=0 to UBOUND(myarray)
response.write myarray(i)
response.write "<br/>"
next

all even indices are keys and all odd indices are values...

-DNG
 
thanks for the replies- I wish I was more on it but I'm not even sure how I would go about utilizing the info once I got it in second post...

The way I had been doing it is

Code:
'_____________Getting info from shopping cart
if Request.Cookies("cart").HasKeys then
    for each cartItem in Request.Cookies("cart")
    	if Request.Cookies("cart")(cartItem) >= "1" then
		cartItems = "'" & cartItem & "'," & cartItems
		end if
	next
	minusOne = len(cartItems)-1
	cartItems = left(cartItems,minusOne)

	set rsNum = server.createobject("adodb.recordset") ' open recordset for number of items in cart
	rsNum.ActiveConnection = dbc
	rsNum.Open "Select count(*) tItems From itemtable WHERE itemID IN (" & cartItems & ")"
	rsNum.Close
	set rsNum = nothing
Then writing out a table that way, using Request.Cookies(cart) (currentItem) to find out how many of the current item was in the cart...

I think part of the problem is that I don't really have a complete grasp on Arrays- especially multi dimensional ones- so I'm not entirely sure how to go about constructing and using it...

I split at , first, and that'll give me an array like this:
Code:
0,SM236=>1
1,SM150=>2
2,SM250A=>3
right?
Then I split again to form a multi dimensional array? or just an array of arrays?
Code:
for each i in cartArray
split(i , "=>")
loop

Am I way off?
 
Yeah, I did, that's when I said "I wish I was more on it but I'm not even sure how I would go about utilizing the info once I got it in second post...
 
can you show us the code where you tried splitting it into arrays...

-DNG
 
Sure thing-

Code:
x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"

arCart = split(x_description,",")

for each item in arCart
   arCartFinal = split(arCart(item),"-") ' I get an error on this line...
next

Then I was thinking I'd get a list of the different items (for the db query) like this:

Code:
for each i in arCartFinal
	cartItems = items & arCartFinal(0)(i) & ","
next
CIL = len(cartItems) - 1
cartItems = left(cartItems, CIL)

Response.Write(cartItems)

Of course it doesnt work- I get a type mismatch error on that first one...
 
ok lets try this:

Code:
x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"

x_description = Replace(x_description,"-",",")

arCart = split(x_description,",")

for each item in arCart
   arCartFinal = Split(arCart(item),",")
next

now when we do this...

we will have

arCartFinal(0)=SM346
arCartFinal(1)=0
arCartFinal(2)=SM236PM
arCartFinal(3)=1
and so on...

now use this array correctly to do your db query...try it out and post back if you encounter any problems...

-DNG
 
Awesome- thank's a lot-

So to compile a list of the different items, I'd want to go something like
Code:
for each i in arCartFinal
	if i mod 2 = 0
	cartItems = "'" & arCartFinal(i) & "'," & cartItems
	end if
next

Though I'm getting a type mismatch error- on this previous line-
Code:
for each item in arCart
   arCartFinal = Split(arCart(item),",")  'Type mismatch: 'SM346'
next

I wonder what I'm doing wrong...
 
Yes i mod 2 =0 would perfectly work fine...

oops...Now coming to the next problem...

Code:
x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"

[red]x_description = Replace(x_description,"-",",")[/red]

[blue]arCart = split(x_description,",")[/blue]

'we already have our array now and there is no more of splitting...

for each item in arCart
   if i mod 2 = 0
    cartItems = "'" & arCart(i) & "'," & cartItems
    end if
next

-DNG
 
I got it to work by going:
Code:
i = 0
for each item in arCart
   if i mod 2 = 0 then
    cartItems = "'" & arCart(i) & "'," & cartItems
   end if
   i = i+1
next

But shouldn't there be a more automatic way of doing it?
when I had it as:
Code:
i = 0
for each i in arCart
   if i mod 2 = 0 then
    cartItems = "'" & arCart(i) & "'," & cartItems ' error
   end if
next
I got "Type mismatch: '[string: "SM346"]'" on the fourth line there.
 
glad it is working for you...

the problem with your second piece of code is that you are not incrementing the value of i...

how about this:

Code:
for each item in arCart
   if item mod 2 = 0
    cartItems = "'" & arCart(item) & "'," & cartItems
    end if
next

-DNG

 
Code:
for each item in arCart
   arCartFinal = Split(arCart(item),",")  'Type mismatch: 'SM346'
next

The 'item' variable is populated by the for each loop by each entity in the collection - so the variable 'item' evaluates to 'SM346' - or the first value in the array. The array is not expecting this as an index reference, it is expecting a number.. thus the type mismatch error.

So, either:
Code:
for each item in arCart
   arCartFinal = split(item,",")
next
or
Code:
for i = lbound(arCart) to ubound(arCart)
   arCartFinal = Split(arCart(i),",")
next

Although.. the code in bold below..
Code:
x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"

[b]x_description = Replace(x_description,"-",",")[/b]

arCart = split(x_description,",")

for each item in arCart
   arCartFinal = Split(arCart(item),",")
next
.. will make the string look like: "SM346,0,SM236PM,1,SM250A,1,HC6,0,SM150,1"
So the second split is unnecessary.

Using this approach the code would be better like this:

Code:
x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"

x_description = Replace(x_description,"-",",")

arCart = split(x_description,",")

for i = lbound(arCart) to ubound(arCart) step 2
    response.write "PRD:" & arCart(i) & "<br />" & vbcrlf
    response.write "QTY:" & arCart(i+1) & "<br /><br />" & vbcrlf
next

You can leave out the replace function and do a second split if you prefer, but you will need to use redim preserve to prevent overwriting (which is what would happen in the code you posted earlier). This is likely to be less efficient though.

Though this is not a multi-dimensional array - will it do what you want it to ?

If not here are two methods, the first is a multidimensional array, the second is an array of arrays.
Code:
  dim aCartItems, i, aCI(), sItemList, x_description

  x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"
  aCartItems = split(replace("123-1,abc-1,sdsds-4","-",","), ",")

  for i = lbound(aCartItems) to ubound(aCartItems) step 2
    redim preserve aCI(1,i/2)
    aCI(0,i/2) = aCartItems(i)
    aCI(1,i/2) = aCartItems(i+1)
    sItemList = sItemList & aCartItems(i) & ","
  next
  sItemList = left(sItemList, len(sItemList)-1)

  for i=lbound(aCI,2) to ubound(aCI,2)
    response.Write("PRD: " & aCI(0,i) & "<br/>")
    response.Write("QTY: " & aCI(1,i) & "<br/>")
  next
 
  response.Write(sItemList)

Code:
  dim aCartLines, i, aCartItems(), sItemList, x_description

  x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"
  aCartLines = split(x_description, ",")

  redim preserve aCartItems(ubound(aCartLines))
  
  for i = lbound(aCartLines) to ubound(aCartLines)
    aCartItems(i) = split(aCartLines(i),"-")
    sItemList = sItemList & aCartITems(i)(0) & ","
  next
  
  sItemList = left(sItemList, len(sItemList)-1 )

  for i=lbound(aCartItems) to ubound(aCartItems)
    response.Write("PRD: " & aCartItems(i)(0) & "<br/>")
    response.Write("QTY: " & aCartItems(i)(1) & "<br/>")
  next
 
  response.Write(sItemList)

As usual, watch out for typos/spelling errors in the code etc.

BTW.. there are other ways to solve your problem, these are just samples based on what's being discussed so far in the thread.

Hope that helps

A smile is worth a thousand kind words. So smile, it's easy! :)
 
I'm starting to wonder if maybe I shouldn't just write this stuff into a new cookie?

The thing I need to do with the info is just have a way I can query what items are in the cart, and then for each individual item, how many of it there are. So the request.cookie("cart") (SM346) thing was great, because that gave me the number of SM346's in the cart.
 
YEAAAHH!!!!

Code:
x_description="SM346-0,SM236PM-1,SM250A-1,HC6-0,SM150-1"

x_description = Replace(x_description,"-",",")

arCart = split(x_description,",")

for i = lbound(arCart) to ubound(arCart) step 2
    Response.Cookies("cart") (arCart(i)) = arCart(i+1)
next

It works! My mechanism for reading the cookie and displaying the shopping cart works flawlessly too!

AWESOME! Thanks everybody for the help- this has really helped me learn more about this kind of stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top