将数据库信息显示为复选框

时间:2015-04-17 13:52:29

标签: php mysql database

如果用户有权访问此权限,则我拥有权限为YES的数据库设置,如果用户无权访问此权限,则为NO。我想根据是否为用户勾选复选框来更改这些答案。我希望YES答案显示为勾选复选框,No答案显示为空复选框。我怎么能这样做,因为我的数据库当前正在输出YES或NO作为用户的结果。

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "databasename";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT adduser, edituser, removeuser, unlockallfiles, viewlocked, filepermissions FROM departments";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table class='table table-hover'><tr style='font-size:18px;'><th>Add User</th><th>Edit User</th><th>Remove User</th><th>Unlock All</th><th>View Locked</th><th>File Permissions</th></tr>";
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr style='font-size:16px;'><td>".$row["adduser"]."</td><td>".$row["edituser"]."</td><td>".$row["removeuser"]."</td><td>".$row["unlockallfiles"]."</td><td>".$row["viewlocked"]. "</td><td>" .$row["filepermissions"]. "</td></tr>";
        }
        echo "</table>";
    } else {
        echo "There are 0 clients in the system matching your search criteria";
    }
    $conn->close();
?>

如何将数据YES显示为勾选复选框,将NO显示为空复选框?

2 个答案:

答案 0 :(得分:1)

用你的代码行替换相应的代码行:

    // output data of each row
    while($row = $result->fetch_assoc()) 
    {
       echo "<tr style='font-size:16px;'>";
       echo "<td><input type='checkbox'". ($row["adduser"] == 'Yes' ? " checked" : "")  ."></td>";
       echo "<td><input type='checkbox'". ($row["edituser"] == 'Yes' ? " checked" : "") . "></td>";
       echo "<td><input type='checkbox'". ($row["removeuser"] == 'Yes' ? " checked" : "") . "></td>";
       echo "<td><input type='checkbox'". ($row["unlockallfiles"] == 'Yes' ? " checked" : "")  ."></td>";
       echo "<td><input type='checkbox'". ($row["viewlocked"] == 'Yes' ? " checked" : "") . "></td>";
       echo "<td><input type='checkbox'". ($row["filepermissions"] == 'Yes' " checked" : "") . "></td>";
       echo "</tr>";                                  
    }

答案 1 :(得分:0)

echo "<tr style='font-size:16px;'><td>".($row["adduser"]=='Yes'?'<input type="checkbox" checked/>':'<input type="checkbox"/>')."</td><td>".($row["edituser"]=='Yes'?'<input type="checkbox" checked/>':'<input type="checkbox" />')."</td><td>".($row["removeuser"]=='Yes'?'<input type="checkbox" checked/>':'<input type="checkbox"/>')."</td><td>".($row["unlockallfiles"]=='Yes'?'<input type="checkbox" checked/>':'<input type="checkbox"/>')."</td><td>".($row["viewlocked"]=='Yes'?'<input type="checkbox" checked/>':'<input type="checkbox"/>'). "</td><td>" .($row["filepermissions"]=='Yes'?'<input type="checkbox" checked/>':'<input type="checkbox"/>'). "</td></tr>";