SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Cookie Array

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie Array

    Hi all,

    ok so im trying to build a function so people can save there favorite games on my site. I want to make it so they can have a list of there favorite games on the site so they can find them quickly.

    My idea was to use cookies and store the games ID then just call the cookies and retrieve the data.

    so if someone clicks the 'add to my favorites' button on game 42 a cookie is created with that id.

    I checked out php.nets page: http://nl3.php.net/set_cookie

    and i copied there code:

    PHP Code:
    <?php
    // set the cookies
    setcookie("cookie[three]""cookiethree");
    setcookie("cookie[two]""cookietwo");
    setcookie("cookie[one]""cookieone");

    // after the page reloads, print them out
    if (isset($_COOKIE['cookie'])) {
       foreach (
    $_COOKIE['cookie'] as $name => $value) {
           echo 
    "$name : $value <br />\n";
       }
    }
    ?>
    but i get an error everytime, "Parse error: parse error, unexpected T_FOREACH in /home/****/public_html/myfavorites.php on line 20"

    I need to store the games ID and possibly its title, and then retrieve all that info and echo it out and i need to let people delete games from there favorites list. How can i do this?
    http://www.mmatalk.com <- Talk Mixed Martial Arts / Pride / UFC

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is no parse error in the code you have posted. your error lies before this code.

    anyway, i dont think this is a good design. if the user clears thier cookies(intentionally or not) they lose thier "favorites list"
    it also would worknt if they log in from a different computer, or even the same computer but use a different browser.

    instead, create a table to hold all the users favorites. each record will contain a user id and the game id. then you can easily retrieve the favorite games for a given user id.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I tried just pasting that code from php.net on a blank page and it still gives me that error, try for yourself.

    And for your idea, I dont have a members system, so where would i get a user ID from? I could store there favorites based on their IP.. ive seen this on other sites where it lets you save this page to your favorites list for example, and theres no sign up required.
    http://www.mmatalk.com <- Talk Mixed Martial Arts / Pride / UFC

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    again, there is no parse error in the code you have posted. i tried it the first time i posted it must be different in some way to the code you are running. look carefully, comment out parts to find it.

    i guess i shouldnt have assumed your users have logged in. sorry.

    however i wouldnt use arrays in cookies for this because each entry will store a new cookie on the users machine. i think ive read that sometimes browsers may impose limits on how many cookies it will store per domain, although im not sure which browsers or what these limits are. i think i would use implode()/explode()

    generally theres about a 4k limit to how much data a browser will store in a cookie, but if your just storing numerical id's, a user would have to create quite a long list to even approach that limit.

    to add to thier list, you would want to explode the existing cookie into an array, add the id to the array, then implode it again and send it back to them.

    but if you want to go with multiple cookies, it might be easier to use the id as the key
    PHP Code:
    setcookie("cookie[$id]"'1'); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for replying

    yea you are right, i should store all the ID's in one cookie instead of multiple.

    however, the piece of code i posted still wont work, im still getting the same error with the foreach statement, ive tried a few things, just that code on a single page, tried taking certain parts of the code out and seeing it it works, problem arises when theres the foreach statement.. arr this is the only thing thats confusing me.
    http://www.mmatalk.com <- Talk Mixed Martial Arts / Pride / UFC

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try retyping it from scratch in a new file. maybe you have some junk invisible bytes in there.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah that was the problem.. frustrating.

    okay so now ive got:
    PHP Code:
    setcookie("favgames""23|24|43|56|67"); 
    which stores the id's of the games, now i need to do a query and grab all the games with those id's, how can i break down those id's and call only those from the query?

    like
    PHP Code:
    $query mysql_query("SELECT * FROM games WHERE id='$favgames'"); 
    <- i know this is incorrect but im trying to illustrate what im trying to do
    http://www.mmatalk.com <- Talk Mixed Martial Arts / Pride / UFC

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $ids explode('|'$_COOKIE['favgames']);
    // cast all values to integers to make safe
    array_walk($ids'intval');

    $list implode(', '$ids);
    echo 
    $query "SELECT * FROM games WHERE id IN ($list)"

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much! that worked perfectly.. now i just gotta figure out how to add/delete records from the cookie..
    http://www.mmatalk.com <- Talk Mixed Martial Arts / Pride / UFC

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // add
    $ids[] = (int) $newid;

    // delete
    $ids array_diff($ids, array($id_to_delete)); 

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks again!

    I added those codes in, but it doesnt seem to be working, what im not getting is how to set the cookie first, and if i have to reset it etc.. heres my code at the moment:

    PHP Code:
    <?php
    // set the cookies 
    setcookie("favgames"""); 

    $title "My Favorite Games";
    require(
    "header.inc.php");
    ?>

    <h1>My Favorite Games</h1>
    <table>
    <tr>
    <?php

    $cookie 
    explode("|"$_COOKIE['favgames']);
    // cast all values to integers to make safe 
    array_walk($cookie'intval'); 

    $list implode(', '$cookie);

    $deleteid $_GET['id'];

    if (
    $_GET['a'] == "del") {
    $cookie array_diff($cookie, array($deleteid)); 
    }

    if (
    $_GET['a'] == "add") {
    $newid $_GET['id'];
    $cookie[] = (int) $newid;
    }

    $query mysql_query("SELECT id, title, description, imageurl FROM games WHERE id IN ($list) ORDER BY date DESC");

  12. #12
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use implode() and set the cookie after updating.

    give the cookie a lifetime parameter.

    also make sure the cookie exists before you try to use it.


Bookmarks

Posting Permissions

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