Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    Creating Cookie Arrays Dynamically

    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:

    PHP Code:
    setcookie("query1"$valuetime()+604800,"/"); // for user entered apple
    setcookie("query2"$valuetime()+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:

    PHP Code:
    $match = array();
    for () {
        
    array_push();

    Thank you!
    Last edited by RonnyNishimoto; 08-08-2012 at 03:34 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.



    .

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    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

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Read this about using an array for your cookie(s) ...

    http://phpprogramming.wordpress.com/...-and-examples/

    Google is the best place to find free tutorials.


    .

  • Users who have thanked mlseim for this post:

    RonnyNishimoto (08-08-2012)

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    I would not be able to find it on Google if I didn't know what I was looking for.

    PHP Code:
    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.
    Last edited by RonnyNishimoto; 08-07-2012 at 09:54 PM.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    You're constructing the variable "query" incorrectly.

    maybe like this ... ?
    PHP Code:

    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.


    .

  • Users who have thanked mlseim for this post:

    RonnyNishimoto (08-08-2012)

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Thank you! After many hours of hacking away at the code, I finally got it to work

    For anyone who may search for this:

    PHP Code:
    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
        
    }

    Last edited by RonnyNishimoto; 08-08-2012 at 03:33 AM.


  •  

    Posting Permissions

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