在过去的几个小时里,我一直在尝试这个方法。数据库中的动态复选框。我试图从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] | |
'----------'------------'
2条答案
按热度按时间7d7tgy0s1#
在数组中使用$selected=($row\u parent['mupload\u filename',$childarrayfilename);您可以避免if-else循环,因为它不太可读。
eivgtgni2#
在删除for循环并向其添加父id之后,代码是固定的。