在过去的几个小时里,我一直在尝试这样做以使其正确。数据库中的动态复选框。我试图从foreach循环中获取价值。谁能帮我这个忙。
$sql_child = "SELECT muploadid, mupload_filename FROM nattachment na LEFT JOIN multipleupload mu ON mu.mupload_id = na.muploadid WHERE na.notetestid = '$id'";
$result_child = mysqli_query($con, $sql_child);
$childArrayFilename = array();
while($row_child = mysqli_fetch_assoc($result_child)){
$childArrayId[] = $row_child['muploadid'];
$childArrayFilename[] = $row_child['mupload_filename'];
}
$sql_parent = "SELECT mupload_filename FROM multipleupload";
$result_parent = mysqli_query($con, $sql_parent);
?>
<form action="database/db_update.php" method="POST">
<?php
while($row_parent = mysqli_fetch_array($result_parent)) {
if(in_array($row_parent['mupload_filename'], $childArrayFilename)) {
// foreach((array)$childArrayFilename as $result) { REMOVED
echo "<input name='checkbox[]' type='checkbox' value='".$result."'checked='checked'/> ";
echo "<input name='checkbox_hid[]' type='hidden' value='".$result."'checked='checked'/> ";
// } REMOVED
}
else {
echo "<input name='checkbox[]' type='checkbox' value='".$row_parent['mupload_filename']."'/> ";
echo "<input name='checkbox_hid[]' type='hidden' value='".$row_parent['mupload_filename']."'/> ";
}
echo $row_parent['mupload_filename'];
echo "<br>";
}
?>
<input type="submit" name="fm_submit" value="Update">
</form>
使用forloop我得到这样的东西。当前,选中了三个复选框,它们连续重复。
.----------.------------.-----.------------.
| [✓] | [✓] | [✓] | ch7.jpg |
:----------+------------+-----+------------:
| [ ] | tutor1.jpg |
:----------+------------+-----+------------:
| [✓] | [✓] | [✓] | tutor2.jpg |
:----------+------------+-----+------------:
| [ ] | ch1.jpg |
:----------+------------+-----+------------:
| [ ] | ch2.jpg |
:----------+------------+-----+------------:
| [✓] | [✓] | [✓] | ch3.jpg |
:----------+------------+-----+------------:
| [ ] | ch4.jpg |
:----------+------------+-----+------------:
| [Submit] | |
'----------'------------'-----'------------'
我需要这个。
.----------.------------.
| [✓] | ch7.jpg |
:----------+------------:
| [ ] | tutor1.jpg |
:----------+------------:
| [✓] | tutor2.jpg |
:----------+------------:
| [ ] | ch1.jpg |
:----------+------------:
| [ ] | ch2.jpg |
:----------+------------:
| [✓] | ch3.jpg |
:----------+------------:
| [ ] | ch4.jpg |
:----------+------------:
| [Submit] | |
'----------'------------'
答案 0 :(得分:0)
使用$ selected = in_array($ row_parent ['mupload_filename'],$ childArrayFilename);您可以避免if循环不太可读的情况。
$sql_child = "SELECT muploadid, mupload_filename FROM nattachment na LEFT JOIN multipleupload mu ON mu.mupload_id = na.muploadid WHERE na.notetestid = '$id'";
$result_child = mysqli_query($con, $sql_child);
$childArrayFilename = array();
while($row_child = mysqli_fetch_assoc($result_child)){
$childArrayId[] = $row_child['muploadid'];
$childArrayFilename[] = $row_child['mupload_filename'];
}
$sql_parent = "SELECT mupload_filename FROM multipleupload";
$result_parent = mysqli_query($con, $sql_parent);
?>
<form action="database/db_update.php" method="POST">
<?php
while($row_parent = mysqli_fetch_array($result_parent)) {
$selected = in_array($row_parent['mupload_filename'], $childArrayFilename);
echo "<input name='checkbox[]' type='checkbox' value='".$row_parent['mupload_filename']."'". $selected ? "checked='checked' /> " : " /> ";
echo "<input name='checkbox_hid[]' type='hidden' value='".$row_parent['mupload_filename']."'". $selected ? "checked='checked' /> " : " /> ";
echo $row_parent['mupload_filename'];
echo "<br>";
}
?>
<input type="submit" name="fm_submit" value="Update">
</form>
答案 1 :(得分:0)
在删除for循环并为其添加父ID之后,代码已修复。
// foreach((array)$childArrayFilename as $result) { REMOVED
echo "<input name='checkbox[]' type='checkbox' value='".$row_parent['mupload_id']."'checked='checked'/> ";
echo "<input name='checkbox_hid[]' type='hidden' value='".$row_parent['mupload_id']."'checked='checked'/> ";
// } REMOVED