PHP Pass array Values to CSV

All about creating websites!
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

PHP Pass array Values to CSV

Post by maboroshi »

Trying to pass an array of values from a series of checkboxes into a csv and I am not sure how to get the values from the checkboxes.

Code: Select all

<input type="checkbox" class="colorWheel" name="check_list[]" value="Red"> Red 
<input type="checkbox" class="colorWheel" name="check_list[]" value="Green"> Green 
<input type="checkbox" class="colorWheel" name="check_list[]" value="Blue"> Blue  
This is what I am doing atm

Code: Select all

if (!empty($_POST['check_list'])) 
{
	foreach($_POST['check_list'] as $color)
	{
		echo $color;
	}
}
Adding the info to the csv works with out issue. But it's only adding the last value in the list of selected items.

Code: Select all

$line = array($name, $group, $mail, $color);
$file = fopen("info.csv", "a");
fputcsv($file, $line);
I am not sure how to add all the items that are checked. Any advice is greatly appreciated

*cheers

Mabo

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: PHP Pass array Values to CSV

Post by ayu »

Not sure how all of your code looks like, but you can add them in the loop.
The following works for me:

http://code.suck-o.com/42525" onclick="window.open(this.href);return false;" onclick="window.open(this.href);return false;

Code: Select all

root@Haruhi:/var/www# cat info.csv 
foo,bar,Red
foo,bar,Blue
"The best place to hide a tree, is in a forest"

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: PHP Pass array Values to CSV

Post by maboroshi »

Hey cats thanks for the help. I was able to get it on my own in the end.

Code: Select all

if (!empty($_POST['check_list']))
{
	$file = fopen("info.csv", "a");
	$color_wheel = '';
	foreach($_POST['check_list'] as $color)
	{
		$color = (string)$color;
		$color_wheel .= $color . ' '; 
	}
	$line = array($name, $group, $mail, $color_wheel);
	fputcsv($file, $line);
	fclose($file);
}

joeboxbeta
Newbie
Newbie
Posts: 9
Joined: 12 Nov 2013, 20:47
10

Re: PHP Pass array Values to CSV

Post by joeboxbeta »

Would you still function with the extra parentheses on the first line?

with parenthesis :
if (!empty($_POST['check_list']))
{
$file = fopen("info.csv", "a");
$color_wheel = '';
foreach($_POST['check_list'] as $color)
{
$color = (string)$color;
$color_wheel .= $color . ' ';
}
$line = array($name, $group, $mail, $color_wheel);
fputcsv($file, $line);
fclose($file);
}


without parenthesis:
if (!empty($_POST['check_list'])
{
$file = fopen("info.csv", "a");
$color_wheel = '';
foreach($_POST['check_list'] as $color)
{
$color = (string)$color;
$color_wheel .= $color . ' ';
}
$line = array($name, $group, $mail, $color_wheel);
fputcsv($file, $line);
fclose($file);
}

seems extra?

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: PHP Pass array Values to CSV

Post by ayu »

joeboxbeta wrote: seems extra?
If you look closely, both the if and the empty has an opening parenthesis, and now you are only closing one of them :)


if (!empty($_POST['check_list'])
"The best place to hide a tree, is in a forest"

Post Reply