array to a single string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kevrelland
Forum Commoner
Posts: 73
Joined: Mon Jan 08, 2007 7:41 am

array to a single string

Post 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
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: array to a single string

Post by shawngoldw »

How about:

Code: Select all

$prodList = '';
foreach ($_COOKIE['TestCookie'] as $value){
  $prodList .= $value.' ';
}
Shawn
kevrelland
Forum Commoner
Posts: 73
Joined: Mon Jan 08, 2007 7:41 am

Re: array to a single string

Post by kevrelland »

works an absolute treat
Cheers
Kev
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: array to a single string

Post 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']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kevrelland
Forum Commoner
Posts: 73
Joined: Mon Jan 08, 2007 7:41 am

Re: array to a single string

Post by kevrelland »

Don't worry it's not for storage, it's to pass back products a customer has viewed.
Post Reply