Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

php - How to check more than one checkbox if value for each checkbox exists in database else unchecked

I have three Table their structure is added down there. i want to show all role on one screen. and when click on one any role it will take you to next screen where (all the permission shown in check boxes) permission that is given to that role checked and other unchecked. and i want to that dynamically, as it can be given and taken by using check boxes. there check boxes should contain role_id and perm_id so it can b use for crud operations.table has many to many relation.

i have the role_id bu using the isset($_GET['role_id']) metthod and perm_id by calling a function that will give me the perm_id

<?php if (isset($_GET['role_id'])) {
$role_id = $_GET['role_id'];
} ?>

user_permission.php page

<?php  $row = $role->allPermission($role_id); if(!empty($row)){ 
                     ?>
                <table class="table table-bordered table-striped" id="datatable-editable">
                    <thead>
                        <tr>
                            <th><center>Add</center></th>
                            <th><center>Update</center></th>
                            <th><center>Delete</center></th>
                            <th><center>View</center></th>
                        </tr>
                    </thead>
                    <tbody>

                        <tr>
                            <?php $all_permissions = $permission->allPermissions();
                            while ($row=mysqli_fetch_assoc($all_permissions)) {
                                $perm_id = $row['perm_id'];
                                $check_permission = $permission->checkPermission($role_id,$perm_id);
                            while ($row=mysqli_fetch_assoc($check_permission)){
                                ?>
                                <td><center><input type="checkbox" perm_id="<?php echo $row['perm_id']?>" value="<?php echo $row['permission']?>">  <?php echo $row['permission']?>
                            </center>
                                </td>

                            <?php 

                             }} ?>
                        </tr>
                        <tr>
                            <?php foreach ($row as $val) {?>
                            <td> 
                                <?php if ($val['permission'] == 'add' || $val['permission'] == 'update' || $val['permission'] == 'delete' || $val['permission'] == 'view'){ ?>
                                    <input type="checkbox" checked="checked" value="<?php echo $val['id']?>">
                                <?php } else { ?> <input type="checkbox" value="<?php echo $val['id']?>" > <?php }?>
                            </td> 
                            <?php } ?> 
                             <!-- foreach ($value as $key => $val) {
                                            // echo $key . ' => ' . $val." / "; 
                                            echo $key['prem'];
                                        } -->
                          <!--   <td>   
                                <?php if (in_array('add', $row)){ ?>
                                    <input type="checkbox" checked="checked" value="<?php echo $id?>">
                                <?php } else { ?> <input type="checkbox" > <?php }?>
                            </td>
                            <td>   
                                <?php if (in_array('update', $row)){ ?>
                                    <input type="checkbox" checked="checked">
                                <?php } else { ?> <input type="checkbox" > <?php }?>
                            </td>
                            <td>   
                                <?php if (in_array('delete', $row)){ ?>
                                    <input type="checkbox" checked="checked">
                                <?php } else { ?> <input type="checkbox" > <?php }?>
                            </td>
                            <td>   
                                <?php if (in_array('view', $row)){ ?>
                                    <input type="checkbox" checked="checked">
                                <?php } else { ?> <input type="checkbox" > <?php }?>
                            </td> -->

                        </tr>
                    </tbody>
                </table>

allPermissions()

    public function allPermissions()
{

    $query="SELECT * from permission where status = 1";
    $conn=$this->Connection();

    $result = mysqli_query($conn,$query);
    $num_rows = mysqli_num_rows($result);
    if ($num_rows>0) {
        return $result;
    }
}

chechkPermission()

public function checkPermission($role_id,$perm_id)
{
    $query = "select * from role_perm rp where role_id = '$role_id' and perm_id = '$perm_id' ";
    $conn=$this->Connection();

    $result = mysqli_query($conn,$query);
    $num_rows = mysqli_num_rows($result);
    if ($num_rows>0) {
        return $result;
    }
}

its been three(now) days i'm stuck here, please help. and sorry for my bad english

Table Structure

enter image description here

This is how i want it

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you mean something like this? <input type="checkbox" <?= ($permission >= 3 ? "checked" : "") ?>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...