我正在从数据库表中获取的数据中填充复选框。我的问题与SO上的其他问题不同,因为我定义了一个复选框,并且在从db填充时循环。所以我想检查包含另一个表中存在的值的复选框。使用我的代码,只选中带有表格最后一行值的复选框。我做错了什么?
//this is the value I get from the other table.
$indb = "$bid,$c,$sc,$p";
$q5=mysqli_query($link,"SELECT * FROM brands_offer WHERE Brand_Id='$bid' AND Published='1' ");
while($row5 = mysqli_fetch_array($q5)){
$catid= $row5['Catg_Id'];
$subcatid= $row5['Subcatg_Id'];
$pid= $row5['Product_Id'];
//this is the value that populates the checkboxes
$vals="$bid,$catid,$subcatid,$pid";
?>
//my argument is here
<input type="checkbox" name="checkbox[]" value="<?php echo "$vals";?>" <?php if ($vals == $indb){?> checked="checked" <?php } ?>>
<?php
echo $bname;
echo " -> ";
echo $catid;
echo ", ";
echo $subcatid;
echo ", ";
echo $pid;
echo " ";
}
答案 0 :(得分:0)
知道了,我将表中的值添加到数组$indb[] = "$bid,$c,$sc,$p";
并使用<?php if (in_array($vals, $indb)){?> checked="checked" <?php } ?>
来检查它是否在数组中。工作:D