gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:16:39 24 4
gpt4 key购买 nike

我有一个数据库设置,如果用户有权访问此权限,则权限为"is";如果用户无权访问此权限,则为“否”。我想根据用户是否勾选了复选框来更改这些答案。我希望"is"答案显示为勾选的复选框,“否”答案显示为空复选框。由于我的数据库当前正在向用户输出 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”显示为空复选框?

最佳答案

将您的相应代码行替换为以下代码行:

    // 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>";
}

关于php - 将数据库信息显示为复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29701206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com