...

View Full Version : Resolved Creating Cookie Arrays Dynamically



RonnyNishimoto
08-07-2012, 01:08 PM
Hi!

I have an input in which I only want to submit if the user enters the same information twice (the information can be anything).

For instance, if the user entered "apple," then "orange," then "banana," "pineapple," then "apple," it would submit on the second apple. If the user had entered any other fruit again, it would submit.

I need it so that the user can leave the page and come back if they want and it's important that it's done with cookies. However I do not know how to create dynamic cookies like:



setcookie("query1", $value, time()+604800,"/"); // for user entered apple
setcookie("query2", $value, time()+604800,"/"); // for user entered orange
// etc as user inputs more

^ name increments by one

Then I would have to search through the cookies if it occurs twice. I was thinking an array, most likely a for loop with push_array(); so something like:



$match = array();
for () {
array_push();
}


Thank you! :)

mlseim
08-07-2012, 01:32 PM
You really need to learn MySQL.

If you can go into your webhost control panel and
find the part about creating a MySQL database,
you can create a database ...
give it a db name, a username, a password.

Then, once you have that, the rest is all PHP.

There are many free examples of how to use PHP/MySQL.

What you're doing with cookies should be done as an array,
but you'll discovered that cookies have limitations on length.
Therefore, not the best way to do it. Thus, using MySQL.
Even a "flat-file" database (plain .txt file) would be better
than cookies.



.

RonnyNishimoto
08-07-2012, 07:34 PM
Thanks for the response Mlseim! The user won't be entering more than 20 times so I should be good to go! I'll delete the cookies after it gets a match.

I've spent about 4 hours fiddling with for loops and I just can't seem to get it. I was thinking it had something to do with $i = 0, $i < length, $i++, but alas I have not been able to get it to work, I am not very good with for loops.

I should edit my original post, because that's not really the reason. I'm kind of doing this to learn about PHP :)

mlseim
08-07-2012, 07:43 PM
Read this about using an array for your cookie(s) ...

http://phpprogramming.wordpress.com/2007/03/06/php-cookies-tutorial-and-examples/

Google is the best place to find free tutorials.


.

RonnyNishimoto
08-07-2012, 10:36 PM
I would not be able to find it on Google if I didn't know what I was looking for. :)



for ($i = 1; $i < 10; $i++) {
if (!isset($_COOKIE[$i])) {
setcookie("query" . [$i],$query,time()+604800,"/");
break1;
}
}
foreach ($_COOKIE["query"] as $key => $value) {
echo "$key:$value";
}


I was able to go this far, but it is not echoing the cookie array.

mlseim
08-07-2012, 11:35 PM
You're constructing the variable "query" incorrectly.

maybe like this ... ?



for ($i = 1; $i < 10; $i++) {
if (!isset($_COOKIE[$i])) {
setcookie("query[$i]",$query,time()+604800,"/");
break1;
}
}
foreach ($_COOKIE["query"] as $key => $value) {
echo "$key:$value";
}




Also, to set a cookie and read it in the same script execution might not work.
It will write the cookie, but you will need another refresh (reload) to view them.
Cookies get transferred in the header packet.


.

RonnyNishimoto
08-08-2012, 04:29 AM
Thank you! After many hours of hacking away at the code, I finally got it to work :)

For anyone who may search for this:



for ($i = 1; $i < 20; $i++) {
if (!isset($_COOKIE["temp"][$i])) {
setcookie("temp[$i]",$query,time()+604800,"/");
break 1;
}
}
if (!empty($_COOKIE["temp"])) {
$duplicate = array_count_values($_COOKIE["temp"]);
if (array_search(2, $duplicate)) {
$key = (array_search(2, $duplicate)); // Returns the value in which was duplicated
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum