August 7th, 2013, 07:29 PM
Sort cookie ( array by last element added )
I create cookie like this:
I need to sort this array by last element added so cars that have been added last, show first.
setcookie("cars[".$_POST['car_id']."]", $_POST['car_id'], $expire);
Can anyone help please? Thanks
August 7th, 2013, 08:52 PM
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.
August 7th, 2013, 10:43 PM
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.
Originally Posted by ManiacDan
August 8th, 2013, 02:35 AM
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.
August 8th, 2013, 10:05 AM
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.