[eluser]spheroid[/eluser]
I've been passing an array of data to create drop-downs but wanted the same for checkboxes. I've modified the form_checkbox function in system/helpers/form_helper.php to the following. This also contains code to create a set of check all/uncheck all buttons OR a toggle checkbox to do the same, your choice! I hope this helps someone!
Note: Below you'll need to manually add the opening and closing javascript tags where the "[removed]" items appear below.
Code:
if ( ! function_exists('form_checkbox'))
{
function form_checkbox($data = '', $options = array(), $selected = array(), $extra = '', $select_all = array())
{
$defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data.'[]' : $data['name'].'[]'));
$name = ( ! is_array($data) ? $data : $data['name']);
if ( ! is_array($selected))
{
$selected = array($selected);
}
if ($extra != '') $extra = ' '.$extra;
$form = "";
$tmpform = "";
$select_all_html = "";
$select_all_js = "";
if ($select_all != NULL)
{
if (is_array($select_all) AND array_key_exists('type', $select_all))
{
$select_all_type = $select_all['type'];
}
else
{
$select_all_type = "button";
}
if (is_array($select_all) AND array_key_exists('location', $select_all))
{
$select_all_location = $select_all['location'];
}
else
{
$select_all_location = "bottom";
}
switch($select_all_type)
{
case 'checkbox':
$select_all_html .= '<input type="checkbox" name="'.$name.'_show_all" id="'.$name.'"> Select all '.$name."s<br />";
break;
case 'button':
default:
$select_all_html .= '<input type="button" name="'.$name.'_show_all" id="'.$name.'" value="Select all '.$name.'s">';
$select_all_html .= '<input type="button" name="'.$name.'_show_all" id="'.$name.'" value="Uncheck all '.$name.'s"><br />';
break;
}
}
foreach ($options as $key)
{
$key = (string) $key;
$sel = (in_array($key, $selected))?' checked':'';
$tmpform .= '<input '._parse_form_attributes($data, $defaults).' value="'.$key.'"'.$sel."> " . $key . "\n";
}
$select_all_js .= ('
[removed]
var '.$name.'_checkboxes = document.getElementsByName("'.$name.'[]");
function checkboxToggleCheckboxes_'.$name.'('.$name.') {
if (document.getElementById('.$name.').checked==false) {
for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
'.$name.'_checkboxes['.$name.'_counter].checked = false;
}
} else {
for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
'.$name.'_checkboxes['.$name.'_counter].checked = true;
}
}
}
function buttonToggleCheckboxesOn_'.$name.'('.$name.') {
for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
'.$name.'_checkboxes['.$name.'_counter].checked = true;
}
}
function buttonToggleCheckboxesOff_'.$name.'('.$name.') {
for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
'.$name.'_checkboxes['.$name.'_counter].checked = false;
}
}
[removed]
');
switch($select_all_location)
{
case 'top':
$form .= $select_all_html . "<br />" .$tmpform . $select_all_js;
break;
case 'bottom':
$form .= $tmpform . "<br />" . $select_all_html . $select_all_js;
break;
}
return $form;
}
}
To test, in your controller try:
Code:
$data['skill_array'] = array(
'PHP', 'ASP', 'ASP.NET', 'Ruby On Rails'
);
$data['language_array'] = array(
'English', 'Spanish', 'French', 'Swahili', 'Kirundi', 'Russian', 'Tagalog', 'American Sign Language'
);
And in your view add:
Code:
<tr valign=top>
<td width=15%>Skills</td>
<td>
<?php echo $this->validation->error('skill'); ?>
<?php echo form_checkbox('skill', $skill_array, ($this->input->post('skill') ? $this->input->post('skill') : NULL), '', array('type'=>'button')); ?>
</td>
</tr>
<tr valign=top>
<td width=15%>Language</td>
<td>
<?php echo $this->validation->error('language'); ?>
<?php echo form_checkbox('language', $language_array, ($this->input->post('language') ? $this->input->post('language') : NULL), '', array('type'=>'checkbox')); ?>
</td>
</tr>