Creating an array from checked checkboxes
4 posts by 2 authors in: Forums > CMS Builder
Last Post: October 26, 2009 (RSS)
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
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
Re: [eduran582] Creating an array from checked checkboxes
By Dave - October 26, 2009
Hi Eric,
This isn't something we typically support... but try this:
The [] on the end appends an element to the array and is the same as: array_push($fields, $fieldname)
Another option might be to print out flist directly since it's already in the CSV format you need.
Hope that helps! Let me know how it goes. :)
This isn't something we typically support... but try this:
//
$fields = array();
foreach ( $_POST['box'] as $fieldname ) {
$fields[] = $fieldname;
}
The [] on the end appends an element to the array and is the same as: array_push($fields, $fieldname)
Another option might be to print out flist directly since it's already in the CSV format you need.
Hope that helps! Let me know how it goes. :)
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com
Re: [Dave] Creating an array from checked checkboxes
Hi Dave,
First off, your solution worked great! Second, sorry for adding something to your already busy schedule. [unsure] I thought because it was within a cmsb program in php I could use the forum to ask for help. I had already been trying to find the solution for a couple of days now and was stumped. I truly do appreciate your help.
Thanks again for your great support.
Eric
First off, your solution worked great! Second, sorry for adding something to your already busy schedule. [unsure] I thought because it was within a cmsb program in php I could use the forum to ask for help. I had already been trying to find the solution for a couple of days now and was stumped. I truly do appreciate your help.
Thanks again for your great support.
Eric
Re: [eduran582] Creating an array from checked checkboxes
By Dave - October 26, 2009
Hi Eric,
Great, glad to hear that worked!
Yea, it's no problem and we're happy to help. Feel free to post when you're stumped on something. We'll help if we can, or we'll try and point you in the right direction. :)
Great, glad to hear that worked!
Yea, it's no problem and we're happy to help. Feel free to post when you're stumped on something. We'll help if we can, or we'll try and point you in the right direction. :)
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com