I want to mark checkboxes if they have been selected via existing data from db or in the postdata. I have an array of all roles, $roles, and $user_roles contains the current roles.
foreach ($roles as $r) {
$checked = false;
if(isset($postdata['roles'][$r->id])){
$checked = true;
}
else{
foreach($user_roles as $ur){
if($ur->id == $r->id){
$checked = true;
}
}
}
<input type="checkbox" name="roles[<?php echo $r->id; ?>]" <?php if($checked){ ?>checked="checked"<?php } ?> value="<?php echo $r->id; ?>" />
The code is working but I wondered if I could tidy it up. I am using Kohana 3.2
1 Answer