1

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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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 :

enter image description here

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?

Edit :

i have tried hundreds of different ways to solve this problem and i think by using

 'can_view' => isset($view[$i]) ? 'on' : 'off',
 'can_edit' => isset($edit[$i]) ? 'on' : 'off',
 'can_create' => isset($create[$i]) ? 'on' : 'off',
 'can_delete' => isset($delete[$i]) ? 'on' : 'off',

i over come the variable's not being designed and it does write either 'off' or 'on' in the db.

However i get a strange behaviour that only the first row gets inserted. for example if i set my check-boxes (now changed to toggle switches) like this :

enter image description here

i would expect my database to look this

on | off | on | off ---- off | on | off | on

but instead this is what i get :

enter image description here

Another strange thing i have noticed is they are being inserted from the bottom, The first column 'id' is an AI column but the id's are incrementing backwards up the table.

The bottom row was the first one inserted. Now you can see that only the first row contains any of the checkbox's 'on' fields, the second is off or null. when we should see on, off, on, off etc.

I have tried including the db call within the foreach loop in controller and doing a regular $this->db->insert and have also tried removing it out of the for each and using $this->db->insert_batch

The other parameters permission_id and permission_name iterate through the loop fine, its as though something breaks when it gets to the checkbox data.

1 Answer 1

1

you can use isset to check if a variable is declared and is different than NULL.

'can_view' => isset(view[$key])? '1' : '0',
'can_edit' => isset($edit[$key])? '1' : '0',
'can_create' => isset($create[$key])? '1' : '0',
'can_delete' => isset($delete[$key])? '1' : '0',
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.