Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
added 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

CodeIgniter: Checkbox Group Function and Usage

In codeignierCodeIgniter, 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');

CodeIgniter: Checkbox Group Function and Usage

In codeignier 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');

Checkbox Group Function and Usage

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');
Source Link
pixelngrain
  • 285
  • 2
  • 17

CodeIgniter: Checkbox Group Function and Usage

In codeignier 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');