php - php - 多个复选框将检查数据何时加载有效值
问题描述
我正在编写html,php代码。我的html:
<input type="checkbox" name="food[]" value="apple"/>
<input type="checkbox" name="food[]" value="banana"/>
<input type="checkbox" name="food[]" value="something"/>
我确实在数据库中获取并保存了多个复选框的值。当我得到数据库时:
Array([0]=>'apple',[1]=>'banana')
我的问题是,如果数组的值类似于复选框的值,我想检查复选框,但我不能。
解决方案
大概您希望能够检查您的复选框,如果它们的值是从数据库返回的(这就是我所理解的):
<?php
function checkit($queryArr, $val)
{
# If a value is in the array from the database, add checked attribute
return (in_array($val, $queryArr))? ' checked="checked"' : '';
}
# Array back from database
$queryArr = ['apple','banana'];
?>
<input type="checkbox" name="food[]" value="apple"<?php echo checkit($queryArr, 'apple') ?> />
<input type="checkbox" name="food[]" value="banana"<?php echo checkit($queryArr, 'banana') ?>/>
<input type="checkbox" name="food[]" value="something"<?php echo checkit($queryArr, 'something') ?>/>
推荐阅读
- swift - SVG 图像在导入 Xcode 时获得了奇怪的偏移量
- asp.net-core - NU1202:包 Microsoft.AspNetCore.Authentication.JwtBearer 5.0.2 与 netcoreapp3.1 不兼容
- python-3.x - 如何将 RLE 中的输入更改为 Python 3 中的字节?
- firebase - Firebase 函数 (Google Cloud) 上的 Puppeteer PDF 创建超时
- ios - iOS 14 设备无法跟踪 Facebook 上的应用安装
- firefox - uBlock和代理socks5 firefox插件的兼容性问题
- python-3.x - 我很难使用 python opencv 将非正方形图像转换为正方形
- javascript - 从对象和键数组中查找值
- regex - hiveql 上的正则表达式:悬空元字符映射减少
- python - Python Django 在一个类别中安排帖子?