#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Sort cookie ( array by last element added )


    Hi;

    I create cookie like this:
    PHP Code:
    setcookie("cars[".$_POST['car_id']."]"$_POST['car_id'], $expire); 
    I need to sort this array by last element added so cars that have been added last, show first.

    Can anyone help please? Thanks
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    Store a serialized array in the cookie and use array_unshift

    Or array_reverse it when it comes out of the cookie.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by ManiacDan
    Store a serialized array in the cookie and use array_unshift

    Or array_reverse it when it comes out of the cookie.
    I realise I can change the way I am creating the array. I was wondering if there is a way to sort the array based on the 'time the cookie element has been created' so I dont have to change the current code.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    No. Cookies are managed by the client. The server knows nothing about them except their name and their value.

    So you have to change the code either way. You can store the creation time (which wouldn't make lot of sense), you can use a numerical index, or you can do what ManiacDan suggested.
    Last edited by Jacques1; August 8th, 2013 at 02:38 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Another reason to put this data into an array and store it in a single cookie is due to browsers having per-domain cookie limits. It's pretty high these days--50+ I think--but it sounds like you could potentially have a lot of car IDs. That'd be a cookie each.

    And you'll need to include any other cookies you have in that limit.

    Of course you could also store this data in a session that's tied to a cookie by ID. Especially if you have user logins, this is always an option, reducing the need to have any secondary cookies.
    LinkedIn: Dave Mittner

IMN logo majestic logo threadwatch logo seochat tools logo