0

table :

role_id   role_name              perm_id
  1         admin                  0
  2         Manager                0
  7         Accounts Assitant      4,6
  8         Registrar              2,5,6

Iam storing all these information from a PHP Form

<form action="" method="post">               
   <table>
    <tr><th>ROLE</th>
        <th>PERMISSIONS<br><input type="checkbox" class="chk_boxes"></th>  
    </tr>
    <tr>
      <td><input type="text" name="role_name" required></td>      
      <td><input type="checkbox" class="chk_boxes1" name="perm[]" value="1">My Account<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="2">Edit Account<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="3">Change password<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="4">List of users<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="5">Define roles<br>
          <input type="checkbox" class="chk_boxes1" name="perm[]" value="6">Assign roles<br>
      </td>
    </tr></table>
    <div><input type="submit" name="submit" value="create role"></div>
  </form>

I was succesfully submitting the data to database, I had issues while retrieving the checkbox values from database. Now I want to edit this form and show the checkboxes(selected one's and deselected one's) also.

<?php
    $query = $db->prepare("SELECT * FROM role WHERE role_id = ".$role_id." ");
    $query->execute();
    foreach($query as $q)
    {
      echo '<form action="" method="post">               
             <table>
              <tr><th>ROLE</th>
                  <th>PERMISSIONS<br><input type="checkbox" class="chk_boxes"></th>  
              </tr>
              <tr><td><input type="text" value="'.$q['role_name'].'" name="role_name" required></td>
                  <td>';
                      $permid_array = $q['perm_id'];                                          
                      foreach(explode(',', $permid_array) as $n)
                      {
                        if ($n == 1 || $n == 2 || $n == 3 || $n == 4 || $n == 5 || || $n == 6) { $set_checked = "checked";}
                        else {$set_checked = ""; }

                        echo '
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="1" '.$set_checked.' > My Account<br> 
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="2" '.$set_checked.' > Edit Account<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="3" '.$set_checked.' > Change Password<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="4" '.$set_checked.' > List of users<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="5" '.$set_checked.' > Define roles<br>
                        <input type="checkbox" class="chk_boxes1" name="perm[]" value="6" '.$set_checked.' > Assign roles<br>';
                      }
                   echo '</td></tr>
             </table>
             <div><input type="submit" name="submit" value="create role"></div>
            </form>';
    }
  ?>

When I execute the edit form, the checkboxs are getting repeated. Suggest me where I went wrong or guide me other way of doing.

Many Thanks

1 Answer 1

2

You do:

echo '
<input type="checkbox" class="chk_boxes1" name="perm[]" value="1" '.$set_checked.' > My Account<br> 
<input type="checkbox" class="chk_boxes1" name="perm[]" value="2" '.$set_checked.' > Edit Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="3" '.$set_checked.' > Change Password<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="4" '.$set_checked.' > List of users<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="5" '.$set_checked.' > Define roles<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="6" '.$set_checked.' > Assign roles<br>';

for every perm_id. If you have 2,4 in perm_id you will do this twice. If you have 2 you will only do it once and if you have 1,2,3,4,5,6 you will do it 6 times.

You can easily solve it by looping over each value:

$arr = array(1,2,3,4,5,6);
$arrNames = array('My Account', 'Edit Account', 'Change Password', 'List of users', 'Define roles', 'Assign Roles');
foreach($arr as $val) {
    $set_checked = "";
    if(in_array($val, $permid_array)) {
        $set_checked = "checked";
    }
    echo '<input type="checkbox" class="chk_boxes1" name="perm[]" value="$val" '.$set_checked.' > '.$arrNames[$val].' <br>'

    }
}

If you want to stick to your original solution, this is what you need to change:

 $permid_array = explode(',', $q['perm_id']);                                          
 echo '
<input type="checkbox" class="chk_boxes1" name="perm[]" value="1" '.(in_array(1, $permid_array))?'checked':''.' > My Account<br> 
<input type="checkbox" class="chk_boxes1" name="perm[]" value="2" '.(in_array(2, $permid_array))?'checked':''.' > Edit Account<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="3" '.(in_array(3, $permid_array))?'checked':''.' > Change Password<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="4" '.(in_array(4, $permid_array))?'checked':''.' > List of users<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="5" '.(in_array(5, $permid_array))?'checked':''.' > Define roles<br>
<input type="checkbox" class="chk_boxes1" name="perm[]" value="6" '.(in_array(6, $permid_array))?'checked':''.' > Assign roles<br>';
Sign up to request clarification or add additional context in comments.

1 Comment

No problems! Good luck!

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.