显示true和false from sql select query as复选框

wsxa1bj1  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(351)

当前状态是此代码:

while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['timeStamp'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['what'] . "</td>";
echo "<td>" . $row['salad'] . "</td>";
echo "<td>" . $row['tomatoes'] . "</td>";
echo "<td>" . $row['onions'] . "</td>";
echo "<td>" . $row['carrots'] . "</td>";
echo "<td>" . $row['hot'] . "</td>";
echo "<td>" . $row['cheese'] . "</td>";
echo "<td>" . $row['sauce'] . "</td>";
echo "</tr>";

沙拉、西红柿、洋葱、胡萝卜、热菜和奶酪的行保存为true和false,因为用户将它们作为复选框插入。我基本上想要的是在我编写sql结果的表中显示true或false作为复选框
这有可能吗?如果您需要更多的代码(比如整个php),请告诉我。

thigvfpy

thigvfpy1#

假设您将每个项目的值保存为 true 或者 false 单位:db

while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['timeStamp'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['what'] . "</td>";
echo "<td><input type='checkbox' " . ($row['salad']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['tomatoes']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['onions']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['carrots']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['hot']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['cheese']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['sauce']=='true' ? 'checked' :'') . "/></td>";
echo "</tr>";
wwtsj6pe

wwtsj6pe2#

如果我理解正确,您只需添加一个复选框并设置checked属性(如果值为true),如下所示

echo '<td><input type="checkbox" value="true" ';
echo ($row['salad'])? 'checked' : '';
echo '></td>';

注意:如果您不希望用户编辑它或取消选中它,您可以添加disabled属性。

相关问题