简体   繁体   中英

Show multiple check box in codeigniter from database

In my database i have a field called ARG_1. i contains Home,Work,Work2

Now i fetch this from database.

And I want to show this three as separate checkbox. Like this:

input type='checkbox' name='checkboxvar[]' value='Option One'>Home<br>
<input type='checkbox' name='checkboxvar[]' value='Option Two'>Work<br>
<input type='checkbox' name='checkboxvar[]' value='Option Three'>Work2

How to do this?

I did this but didnt work:

$params = $this->model->get();    
foreach($params as $value) $new_val = explode(',',$value->arg_1); 
            {
                echo form_checkbox($new_val);
            }

On your model

Write this query

public function yourMethodName(){
    $this->db->select('ARG_1');
    $q=$this->db->get('your-table-name');
    return $q->result();
}

and on controller first load your model and then call model method which have you made and send data to your view page

like this...

$this->load->model('your-model-name'); # Load model
$data['details']=$this->your-model-name->yourMethodName(); # Load Model Method
$this->load->view('your-view-page-name',$data); #Load your view page

and finally on View page

foreach($details as $rows){

     $exp_arr=explode(',',$rows->ARG_1); # convert ARG_1 string value into array
     for($i=0;$i<count($exp_arr);$i++){

          echo "<input type='checkbox' name='checkboxvar[]' value='Option Two'>".$exp_arr[$i]."<br>";
     }

}

Hope this code will help you

Try this One of the simple way

$sno=1;
$i=0;
for($sno;$sno<=$params->num_rows();$sno++)
{
echo '<input type="checkbox" name="checkboxvar[]" id="checkboxvar" value="<?php echo $params[$i]->check_box_value; ?>">';
$i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM