foreach循环会产生意外的结果

ecfsfe2w  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(275)

在过去的几个小时里,我一直在尝试这个方法。数据库中的动态复选框。我试图从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] |            |
'----------'------------'
7d7tgy0s

7d7tgy0s1#

在数组中使用$selected=($row\u parent['mupload\u filename',$childarrayfilename);您可以避免if-else循环,因为它不太可读。

$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>
eivgtgni

eivgtgni2#

在删除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

相关问题