有更新表单所以我需要,如果某些复选框输入已经根据MySQL数据进行了检查,那么它将显示已检查。
<?php
$statement = $db->prepare("SELECT leavingEquipment FROM orderform WHERE orderNo='$orderNo'"); // here are the checked data
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row4){
$leavingEquipment = explode(", ",$row4['leavingEquipment']); // Here I use explode I check this with var_dump
$statement = $db->prepare("SELECT * FROM equipment"); // here all equipment
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row3) {
$leavingEquipment3=$row3['equipmentTitle'];
?>
<input type="checkbox" name="leavingEquipment[]" value="<?php echo $row3['equipmentTitle']; ?>" <?php if($leavingEquipment==$leavingEquipment3) echo 'checked'; ?>><label class="control-label"><?php echo $row3['equipmentTitle']; ?></label>
<?php
}
}
?>
Var_dum输出如下: array(2){[0] =&gt; string(9)“Bluetooth”1 =&gt; string(6)“casing”}
先谢谢。
答案 0 :(得分:2)
见这里,
<input ... name="leavingEquipment[]" } value="<?php echo $row3['equipmentTitle']; ?>" <?php if($leavingEquipment==$leavingEquipment3) echo 'checked'; ?>> ...
^ a lone closing bracket ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
您正在将数组$leavingEquipment
与字符串$leavingEquipment3
进行比较。
而是使用in_array()
函数来检查是否已经检查过复选框,即设备是否已在阵列中。所以你的内部foreach
循环应该是这样的:
// your code
foreach($result as $row3) {
$leavingEquipment3=$row3['equipmentTitle'];
?>
<input type="checkbox" name="leavingEquipment[]" value="<?php echo $row3['equipmentTitle']; ?>" <?php if(in_array($leavingEquipment3, $leavingEquipment)){ echo " checked='checked'"; } ?>>
<label class="control-label"><?php echo $row3['equipmentTitle']; ?></label>
<?php
}
// your code