[thelist] JS Cookie problem - more info!

Morgan Kelsey morgan at morgankelsey.com
Fri Jan 10 14:17:00 CST 2003


tom,



> Ok, figured something out! Again, what I am doing is looking for a
> few cookies within my cookie string. These cookies start with "pid"
> and have a number after them, like:
>
> pid121=5
> pid7=4
>
> Because I don't know how many might be in a cookie, or what the
> values are, I figured I would run through a loop to the highest
> number pid could be, and check for each one.
>
> So - my code to do that was this:

ok, before .jeff tells you how ;p which will involove some yucky looping i
predict....maybe you want to reconsider your approach here.

it looks to me like you're trying to store complex data as simple data. pid123
refers to product ID 123, i assume? and then the value is probably the qty for
each? am i right?

what happens if you need to store the price too, or the name? don't tell me you're
going to go hit the DB up every time for those....

what you need here is an array or some other complex datatype that will hold the
necessary data together for each item in the shopping cart (assuming thats what
you've got here).
if it was javascript arrays, they'd be like (forgive me i don't javascript
everyday)

cart = Array(2);
cart[1][1] = (product id);
cart[1][2] = (price);
cart[1][3] = (name of the thing);
cart[2][1] = (the next prod id);
cart[2][2] = (price);

etc etc

now the problem is, you can't store complex datatypes as cookies.
this is what session variables in server side languages are commonly used for....

hope that helps, i may be way off base, but i think you may just be trying to hard
to do something with cookies that they're not designed to do.


nagrom
http://www.morgankelsey.com




More information about the thelist mailing list