php - 在 PHP 中动态检查来自 MySQL 的动态复选框

标签 php mysql arrays checkbox

我有一些动态创建的复选框,使用 PHP 的这段代码创建:

<?php do { ?>
<input type="checkbox" name="Cat_Ref_Array[]" value="<?php echo $row_selectCategories['Cat_Ref']; ?>" />
<label for="Category"><?php echo $row_selectCategories['Category']; ?></label>
<?php } while ($row_selectCategories = mysql_fetch_assoc($selectCategories)); ?>

我还有另一个表,其中保存了可能的响应,其检索方式如下:

$query_getFeatureCat = sprintf("SELECT `Ref`, Cat_Ref, dist FROM newtable WHERE `Ref` = %s", GetSQLValueString($colname_getFeatureCat, "text"));
$getFeatureCat = mysql_query($query_getFeatureCat, $db) or die(mysql_error());
$row_getFeatureCat = mysql_fetch_assoc($getFeatureCat);

当复选框 value 出现在 newtable 查找的输出中时,如何让表单预填充/“选中”复选框?

最佳答案

试试这个

<?php
$query_getFeatureCat = sprintf("SELECT `Ref`, Cat_Ref, dist FROM newtable WHERE `Ref` = %s", GetSQLValueString($colname_getFeatureCat, "text"));
$getFeatureCat = mysql_query($query_getFeatureCat, $db) or die(mysql_error());
$row_getFeatureCat = mysql_fetch_assoc($getFeatureCat);
while ($row_selectCategories = mysql_fetch_assoc($selectCategories)) {
  if ($row_getFeatureCat) {
    if (in_array($row_selectCategories['Cat_Ref'], $row_getFeatureCat)) {
      $checked = 'checked';
    } ?>
  }
  <input type="checkbox" name="Cat_Ref_Array[]" value="<?php echo $row_selectCategories['Cat_Ref']; ?>"  <?php echo $checked; ?>/>
  <label for="Category"><?php echo $row_selectCategories['Category']; ?></label>
<?php }  ?>

关于php - 在 PHP 中动态检查来自 MySQL 的动态复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32582605/

相关文章:

mysql - 在 AngularJS 中显示 SQL 错误消息

mysql - 使用 moodle 数据操作 API 从表中获取特定字段

python - 我尝试创建我的第一个数据库,但出现错误

php - 如何在 PostgreSQL array_append() 中使用数组变量?

java - Eclipse - 数组无法解析

PHP - 如何适应时区

javascript - Flot 问题中的时间图?

php - 尝试回应准备好的声明

php - CakePHP:自动设置模型创建和更新日期

python - Numpy - 如何根据条件(或匹配模式)替换元素