I have a dynamic form that is populated with check boxes. I'm trying to implement a permissions / roles system for my dashboard.
the form is populated depending on what permissions are stored in the db. for this example i only have 2. this is the code for the form :
if (!empty($permissions)) {
foreach ($permissions as $perm) {
?>
<tr>
<td><?php echo $perm->display_name; ?></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="view[]" id="<?php echo $perm->id ?>-view" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-view"> </label>
</div></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="edit[]" id="<?php echo $perm->id ?>-edit" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-edit"> </label>
</div></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="create[]" id="<?php echo $perm->id ?>-create" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-create"> </label>
</div></td>
<td class="text-center"><div class="checkbox checkbox-success checkbox-circle mb-2 ml-2">
<input name="delete[]" id="<?php echo $perm->id ?>-delete" type="checkbox" value="1">
<label for="<?php echo $perm->id ?>-delete"> </label>
</div></td>
</tr>
<input type="hidden" name="permission_id[]" value="<?php echo $perm->id ?>">
<input type="hidden" name="permission_name[]" value="<?php echo $perm->permission_name ?>">
<?php
}
}
?>
the form works great and is displayed like this :
this is my controller code :
public function setPermissions()
{
if ($this->isAdmin() == true) {
$this->loadThis();
} else {
// GET FORM FIELD INPUTS
extract($_POST);
foreach ($permission_id as $key => $permission_id) {
$data = array(
'permission_id'=> $permission_id,
'permission_name' => $permission_name[$key],
'can_view' => $view[$key],
'can_edit' => $edit[$key],
'can_create' => $create[$key],
'can_delete' => $delete[$key],
'role_id' => $roleId,
);
$result = $this->settings_model->setPermissions($data);
}
}
if ($result > 0) {
$this->session->set_flashdata('success', 'Permissions set successfully');
} else {
$this->session->set_flashdata('error', 'Setting permissions failed');
}
// LOAD VIEW
redirect('staffPermissions');
}
and my model :
public function setPermissions($data)
{
$this->db->trans_start();
$this->db->insert('app_staff_permissions', $data);
$this->db->trans_complete();
return true;
}
i have tried the trick of putting a hidden input field above the checkbox's but this only returns a result for the last row added to the db.
The problem i having is when a check box is not checked i get an error that the variable is not declared. Presumably this is because it is returning a null value.
i have tried to check for a value like this :
'can_view' => ($view[$key]) == '1' ? '1' : '0',
'can_edit' => ($edit[$key]) == '1' ? '1' : '0',
'can_create' => ($create[$key]) == '1' ? '1' : '0',
'can_delete' => ($delete[$key]) == '1' ? '1' : '0',
but that also does not work and throws the same error. i have tried an if else statement within the array but that obviously does not work. I'm really stuck with this now and willing to try any suggestions. Is there any way to check if the value is null and if so assign a '0' to it?
Aucun commentaire:
Enregistrer un commentaire