Hello all,
Need some help with "array" values or more specifically, creating them from checked checkboxes to use them when producing a .csv file for download.
I have a page where the user checks various checkboxes, example:
<input type=checkbox name=box[] value='first_name' /> First Name
<input type=checkbox name=box[] value='last_name' /> Last Name
Whatever they check is "POST"ed to another php page where what was checked is processed:
=================
$box = $_POST["box"];
$how_many = count($box);
if ($how_many>0) {
for ($i=0; $i<$how_many; $i++) {
$box[$i];
$flist=$flist . "'$box[$i]', ";
}
}
PROBLEM (this does NOT work: see below) ---> $fields = array($flist);
This DOES work: $fields = array('first_name', 'last_name');
// process information for output file
// csv header row
print(join(',', $fields) . "\n");
// loop over records, outputting a row for each
foreach ($records as $record) {
$row = array();
foreach ($fields as $field) {
$value = $record[$field];
// quote and escape special character
if ( preg_match('/[," \t\n]/', $value) ) {
$value = '"' . preg_replace('/"/', '""', $value) . '"';
}
array_push($row, $value);
}
echo(join(',', $row) . "\n");
}
=============
PROBLEM (explained):
If I construct a 'static' array like this:
$fields = array('first_name', 'last_name');
It works fine; the .csv file is created (file creation code left out to save space), and made available for viewing or download. BUT when I use the above code to create an array based on what checkboxes are checked, that I thought looked the same as the 'static' array, I get and error similar to:
Notice: Undefined index: 'first_name' 'last_name' in /my/home/public_html/export2csv.php on line 68
In this case line '68' is: $value = $record[$field];
I think the error is caused because it is looking at both fields as one. I've tried with and without the single quote (as is listed in the 'static' array) but it makes no difference.
Can someone point me in the right direction?
TIA!
Eric