Search code examples
phpcodeignitercheckboxchecked

Getting check box values as checked from database codeigniter


I'm trying to get check box values inserted into my database as checked.. I'd inserted it using the implode method as string. It inserted the values successfully., but my condition to get the check box value "checked" is not working..

<label>Some text</label>
<input type="checkbox" name="text[]" value="text1" 
<?php echo set_checkbox('text', $row->Some_text)== 'text1' ? "checked" : "";?>>text1
<input type="checkbox" name="text[]" value="text2" 
<?php echo set_checkbox('text', $row->Some_text)== 'text2' ? "checked" : "";?>>text2
<input type="checkbox" name="text[]" value="text3" 
<?php echo set_checkbox('text', $row->Some_text)== 'text3' ? "checked" : "";?>>text3 
<input type="checkbox" name="text[]" value="text4" 
<?php echo set_checkbox('text', $row->Some_text)== 'text4' ? "checked" : "";?>>text4

I wrote model for edit as :

public function edit($id)
    {
    $sometext = $this->input->post('text');
        $data=array(
        'Some_text'=>json_encode(implode(",", $sometext)),
        );
        $this->db->set($data);
        $this->db->where('User_id',$id);
        $this->db->update('tbl_check');
        $query = $this->db->get('tbl_check');
        return $query->row();
        }

And edit is working well..


Solution

  • Sometimes the use of functions from frameworks just make it totally unnecessarily and dirtier.

    <input type="checkbox" name="text[]" value="text1" <?php echo ($yourVar == 'text1' ? 'checked' : null); ?>>