<td><input type="checkbox" name="cb" /></td>
how i get this checkbox checked based on my database content on my database field the contents is 1 and 0
if 1 checkbox will checked and if 0 checkbox unchecked
How i do that
in my view i have 5 checkbox......
1 2 3 4 5
1.[] [] [] [] []
2.[] [] [] [] []
3.[] [] [] [] []
4.[] [] [] [] []
my field status on database........
| Status |
| 1,2,3 |
| 1,2,3,4,5 |
| 2,3,4 |
| 1,2,5 |
Updatee
how i get checked checkbox
<?php $j=1; foreach ($dudu as $row2 ): ?>
<td><input type="checkbox" name="popok[]"
<?php if(strrpos($row->menu_allowed,''.$j.'')!==FALSE){echo 'checked';$j++;}/></td>
<?php endforeach; ?>
if the data 1,2,3,4 it will check
1 2 3 4
[v] [v] [v] [v]
but if the data not sequential like 1,3,4 it only check the first box
1 3 4
[v] [] []
in your controller get the chek box value
<?php
class Test extends CI_Controller {
function index()
{
$check_status = $this->test_model->get_checkbox_status();
$data['check_status'] = $check_status;
$this->load->view('testview', $data);
}
}
?>
then in your view
<td><input <?php if($check_status==1){"checked='checked'";}?> type="checkbox" name="cb" /></td>
UPDATE
use strrpos
<?php if(strrpos($check_status,'1')!==false){"checked='checked'";}?>
this will check 1
another method using a for loop
<?php for($i=1;$<6;$i++) { >?
<td><input <?php if(strrpos($check_status,$i)!==false){"checked='checked'";}?> type="checkbox" name="cb" /></td>
<?php } ?>
when field in you database is one print this instead of you existing code.
<input type="checkbox" name="cb" checked="checked" />
Let's say you fetch value in the following variable
var cbValue = getValueFromDB();
Using JQuery:
if (cbValue == 1 ){
$('input[name="cb"]).attr(this.checked);
}
if (cbValue == 0){{
$('input[name="cb"]).removeAttr("checked");
}
Try this:
$check_val = //Collect from db
<input type="checkbox" name="check1" <?php if($check_val=='1') echo checked;?>>
<input type="checkbox" name="check2" <?php if($check_val=='0') echo checked;?>>
may you can get from this
Or you can also use
1 2 3 4 5
1 [00] [01] [02] [03] [04]
2 [10] [11] [12] [13] [14]
3 [20] [21] [22] [23] [24]
4 [30] [31] [32] [33] [34]
as id's individually and then store it in database