Page 1 of 1

array to a single string

Posted: Thu Aug 19, 2010 10:06 am
by kevrelland
Hi,
i have this array

Code: Select all

foreach ($_COOKIE['TestCookie'] as $value){
  echo $value.' ';
}
Which works well, but i want to take the resulting list and use it in a mysql db as $prodList
I have tried

Code: Select all

foreach ($_COOKIE['TestCookie'] as $value){
  $prodList = $value.' ';
}
and all i get back is one result, how can i get the full list in $prodList
Cheers
Kevin

Re: array to a single string

Posted: Thu Aug 19, 2010 10:08 am
by shawngoldw
How about:

Code: Select all

$prodList = '';
foreach ($_COOKIE['TestCookie'] as $value){
  $prodList .= $value.' ';
}
Shawn

Re: array to a single string

Posted: Thu Aug 19, 2010 2:06 pm
by kevrelland
works an absolute treat
Cheers
Kev

Re: array to a single string

Posted: Thu Aug 19, 2010 2:13 pm
by AbraCadaver
You really shouldn't store a list of products in a single database column. It's best to have a row for each product and have them relate back to another table. But if you must:

Code: Select all

$prodList = implode(' ', $_COOKIE['TestCookie']);

Re: array to a single string

Posted: Fri Aug 20, 2010 5:30 am
by kevrelland
Don't worry it's not for storage, it's to pass back products a customer has viewed.