In CodeIgniter, I have created function (helper) to populate, store and retrieve checkbox group. I just wonder if I have written code is in proper way and optimized or needs some more finishing?
Function
if ( ! function_exists('checkbox_group') )
{
function checkbox_group($checkboxes = array(), $name)
{
// setting hidden field for null, if no checkbox selected
echo form_hidden($name.'[]', 'null');
// start checkbox loop
foreach($checkboxes as $check => $label_text):
$checked = FALSE;
$selects = get_config_row($name);
if(in_array($check, $selects))
$checked = TRUE;
echo '<div class="checkbox">';
echo form_label(form_checkbox($name.'[]', $check, $checked, 'id="'.$check.'" class="checkbox"') . $label_text, $check);
echo '</div>';
endforeach;
}
}
Usage
// checkbox items
$checkboxes = array(
'noindex'=>'No Index',
'nofollow'=>'No Follow',
'noarchive'=>'No Archive',
'nosnippet'=>'No Snippet'
);
// render checkboxes useing function
checkbox_group($checkboxes, 'indexes');