Jump to content

Solution needed for my cookie values


Sharkadder

Recommended Posts

Hi, recently i have been working on a website using a javascript shopping cart example, (don't worry i won't go into the shopping cart, the code should be fairly straight forward to understand). Basically i use a form to submit data, the data is then held within a javascript file and then the relevant data is stored within a cookie. The data is then needed to be accessed on another web page by checking a cookie, so what is my problem? I shall explain.Currently when a user selects an item from a form a presses submit, the javascript file is queried and if a cookie doesn't currently exsist then it will create one called "paycart1" that will hold all the information for variable values. If the cookie paycart1 does indeed exsist then it creates a new cookie with the same name but incrementing a number on the end of it, e.g. paycart1 exsists so the second cookie would be called paycart2 etc. (this happens everytime a new item is added onto the shopping cart).Now what i need to know is, on my next page, how do i get the values from paycart cookies efficiently? By this i mean i want to create an array which will hold some data from a cookie, array 1 will hold the relevant data for cookie 1, array 2 will hold the relevant data for cookie 2 etc.So here is some code i have in my javascript file to hold variable values: for (i=1; i<cmax; i++) { // find empty entry, cmax is the max amount of cookies allowed cook = new Cookie (document, "paycart" + i); cook.load(); if (cook.qty*1.0 == 0 || isNaN (cook.qty)) { cook.pcat = pcat cook.qty = blk2q; cook.cod = bcode; cook.des = blk3n; cook.ihnd = blkhn; cook.ishp = blksh; cook.op1n = op1n; cook.op1v = op1v; cook.op2n = op2n; cook.op2v = op2v; cook.prc = blk4a; cook.imgx = imgx; cook.ozs = bozs; cook.ader = ader; cook.adtx = adtx; cook.store(); ClearAll (); if (i == cmax - 1) alert ("Warning - that was your last cookie!"); return; } } alert ("Error 1 - out of cookies!");}As you can see i am creating a new cookie to hold this data, on the following page if i do the code below, it will display the values from the FIRST cookie which was set: document.write (cook.des + cook.adtx);So my question is this, what do i need to do to check if other cookies exsist, if other cookies do exsist, how do i get the same 2 values from the other stored cookies and then display them in this format:value[0] = //the 2 values added together from cookie1 (as shown above)value[1] = //the 2 values added together from cookie2etc.If you need me to explain more i can, but i think you should get what i am after doing, getting values from cookies named paycart1,2 etc. then putting them values into arrays.p.s. i can access the javascript file from the page i wish to display values, so no linking to the file is needed as that's already set up, as you would of guessed.thanks

Link to comment
Share on other sites

How about you store all of your cart information in a single array and then serialize the array and store the whole mess in one cookie? That way you only have one cookie regardless of what they do with the cart, you just unserialize the value in the cookie and get your array structure back to do whatever you want with.

Link to comment
Share on other sites

well the thing is it's not my cart example, it's an example from a guy who develops paypal scripts, the cart itself works a treat just i need to know this one thing because i'm wanting to store some values within an array. Since i posted i have uncovered more coding, if you can see what i need to do then that would be great but basically this is more code: for (i=1; i<cmax; i++) { // print all entries cook = new Cookie (document, "paycart" + i); cook.load(); if (cook.qty > 0) { PrintItem (i); } }As you can see the printitem function get's it's name depending on the value of the integer i.function PrintItem (i) { // make one entry in cartvar tm,t1,t2,wt; document.writeln ('<tr valign = "top"'); document.writeln (' align = "right">'); document.writeln (' <td>'); document.writeln (' <input type = "checkbox" '); document.writeln (' value = "BOX1"'); document.writeln (' onclick = "ClearEntry (', i, ');'); document.writeln (' document.location.reload ();" />'); document.writeln (' </td>'); document.writeln (' <td align = "right">', cook.qty,'</td>'); if (root.xx_pc == on) { document.writeln (' <td align = "right">', cook.pcat,'</td>'); } document.writeln (' <td align = "left">', cook.cod,'</td>'); document.writeln (' <td align = "left">', cook.des + cook.adtx);}Ok half of that code means nothing to you but where you see this part:cook.des + cook.adtx <---that value changes depending on what cookie you are on, what i need to know is this:how do i get cook.des + cook.adtx for each different cookie? Once i know how to do that i can then do what i need to do, i think each cookie is called paycart then + the integer i, so cookie1 would be called paycart1, but i am unsure how i'd get the values i am after from that cookie name. Thanksthanks.

Link to comment
Share on other sites

Well, you could keep looping and checking if cookies exist until you get to one that doesn't. This seems really overly-complicated though, it's pretty easy to store your entire cart data structure in an object or array, serialize it, and save the entire structure in a single cookie. Then you don't have to deal with any of this other stuff that you're dealing with. The way it's being done now sounds overly-complex and inefficient.

Link to comment
Share on other sites

well i don't want to modify the code too much incase the cart stops working, it's not my code and i think i understand most of it but don't want to modify exsisting code. What would i need to do for the looping/checking a cookie exsists? That is the code i am stuck on.I'm not sure what code i need to use for looping through cookies and then giving an array the values from it. As i say, once i know that simple code my problem is then sorted. I persume my understanding is right that the cookie is indeed called paycart followed by a number? if that is true i will need to check the cookies of that name somehow, then get the values into some sort of array until no cookies remain.theres not many examples on the net about looking through cookies in such a way.thanks

Link to comment
Share on other sites

theres not many examples on the net about looking through cookies in such a way.
Right, because not many people decide to handle cookies like this.You can start a counter variable at 0 or 1 or whatever your cookies start at and use a while loop to loop through and check to see if the cookie exists. If a cookie doesn't exist then you set a variable to stop the loop. You can do whatever else you want inside the loop, storing the values in another array or whatever.
var i = 0;var done = false;while (!done){  ...  // if the cookie doesn't exist  done = true;}

Link to comment
Share on other sites

yes thanks for the advice, i think i have got it now. Also i have been able to get hold of each individual integer what the number i represents, so in this case i can call upon say if (i==5) then get the value i need for the 5th cookie, both way's will do it, instead of having 15 if statements i think i will just have to loop through and output values, i should be ok from now on, if i do get stuck with my looping i will come back, but from what i see all looks ok.thanks for the help once again

Link to comment
Share on other sites

ok i've sorted my problem out but i have one last question to ask, say i'm wanting to take another approach and create an array with the values below, i have tried putting it into some sort of loop as a different approach but i cannot seem to see how a loop would do the job, basically here is some if statements: n = i var myarray = new Array(15) if (n == 1) { myarray[0] = (cook.des + cook.adtx); document.writeln(',Val=' + (myarray[0])); } if (n == 2) { myarray[1] = (cook.des + cook.adtx); document.writeln(',Val=' + (myarray[1])); }As you can see the array size is 15, imagine doing 15 different if statements just to check the value of n, what would i need for some sort of for loop or can this not be done? When i tried a for loop before, what happened was, everytime the n incremented by 1 and i wanted myarray[n] to be updated with the value (cook.des +cook.adtx), the other array values all changed to the new value what n was on, instead of just for that 1 value of n. e.g. if n = 2 i don't want the array value for if n = 1 to change to the new value.If it cannot be done it's fine, i was just after a different view to how i could do it another way, so far the If statements is the only other way i have found, although it would be nice to use loops like this.

Link to comment
Share on other sites

thanks for all the help dude, after more research i have now found out how i can access the cookies and split them up using php, i can also displays lengths of strings too.Now i can get all values, of course that technique i tried with javascript gives me knowledge, all i have to do is get the cookie values into an array, then look up that array and store the values within a mysql table using the php way.Thanks for the help, i have to admit though, i caught onto php much quicker than javascript. I seem to favour these server side languages more, people keep mentioning ajax as it's good for this sort of thing but i may look into that another time.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...