Multiple values in one cookie

Thread Solved

Join Date: Apr 2009
Posts: 9
Reputation: macfi is an unknown quantity at this point 
Solved Threads: 0
macfi macfi is offline Offline
Newbie Poster

Multiple values in one cookie

 
0
  #1
Apr 16th, 2009
Hi All

I am a bit of a begginer in PHP.
How do I store multiple vales in a cookie?
I am going along the lines of:

  1. $data = array('apple'=>'green', 'orange'=>'orange',
  2. 'banana'=>'yellow');


then something like (not sure about the sytax here)::
  1. $var=implode($data);
  2. setcookie('data', $var, time() +3600);


Basically how do I get the information into and out of the cookie.

Thanks in advance for any help.
Last edited by peter_budo; Apr 16th, 2009 at 8:03 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 133
Reputation: vicky_rawat is an unknown quantity at this point 
Solved Threads: 17
vicky_rawat's Avatar
vicky_rawat vicky_rawat is offline Offline
Junior Poster

Re: Multiple values in one cookie

 
0
  #2
Apr 16th, 2009
Hi,

Why don't you concatenate values in a string like
  1. apple,orange,mango
  2. and then store it in cookie.
Vivek Rawat
Keep solving complexities.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 9
Reputation: macfi is an unknown quantity at this point 
Solved Threads: 0
macfi macfi is offline Offline
Newbie Poster

Re: Multiple values in one cookie

 
0
  #3
Apr 16th, 2009
I really need it to be an associative array that is stored in the cookie and then be able to output the relevant data. If that makes any sense.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Multiple values in one cookie

 
0
  #4
Apr 16th, 2009
Well if you want to implode the values and store it in the cookie then you'd explode it to retrieve it as an array.

  1. $array = array('orange', 'green', 'blue');
  2. $string = implode(',', $array); // 'orange,green,blue'
  3. $second_array = explode(',', $string); // array('orange', 'green', 'blue');
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 9
Reputation: macfi is an unknown quantity at this point 
Solved Threads: 0
macfi macfi is offline Offline
Newbie Poster

Re: Multiple values in one cookie

 
0
  #5
Apr 16th, 2009
Thanks for all the help.
I know how to do the array part but I cannot get it into my head how to put the array into a single cookie and then get the data back out of the cookie.
I have spent most of yesterday googling for a solution with no real luck.

Thanks again in advance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Multiple values in one cookie

 
0
  #6
Apr 16th, 2009
Well you did it in your example. All I did was explain the correct syntax for implode/explode. Also, see www.php.net/cookies
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 9
Reputation: macfi is an unknown quantity at this point 
Solved Threads: 0
macfi macfi is offline Offline
Newbie Poster

Re: Multiple values in one cookie

 
0
  #7
Apr 16th, 2009
Thanks
but I am not sure on how to get the data back out of the cookie.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Multiple values in one cookie

 
0
  #8
Apr 16th, 2009
Originally Posted by macfi View Post
Thanks
but I am not sure on how to get the data back out of the cookie.
Then you didn't read the link I posted
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 9
Reputation: macfi is an unknown quantity at this point 
Solved Threads: 0
macfi macfi is offline Offline
Newbie Poster

Re: Multiple values in one cookie

 
0
  #9
Apr 16th, 2009
Hi
Thanks again for all the help, my head is going round and round as I have been at thsi for so long now. i right in saying to set the cookie I :
  1. $data = array('apple'=>'green', 'orange'=>'orange',
  2. 'banana'=>'yellow');
  3. $var=implode( ',', $data);
  4. setcookie('data', $var, time() +3600);
and that has set the cookie?
I know how to set and then get everything out of a normal cookie
I alsoknw how to use the array but put them both together and I have a total block. I just cannot get my head round getting the data from within the array that is stored in the cookie. I have read your link but it is not helping me as I think my brain is fried.

Any help very very gratefully accepted.
Last edited by peter_budo; Apr 16th, 2009 at 8:03 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Multiple values in one cookie

 
0
  #10
Apr 16th, 2009
If you know how to set and get data from the cookie I just gave you how to use implode/explode, just put them together. You already did the first part in your original post.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC