简体   繁体   中英

foreach loop inside while loop in php gives unexpected result

I have been trying this for the past couple of hours to get it right. Dynamic Checkbox from database. I am trying to get the value out from the foreach loop. Can anyone help me on this.

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

Using forloop i get something like this. Currently there are three checkbox selected and they repeated in a row.

.----------.------------.-----.------------.
|   [✓]    |    [✓]     | [✓] |  ch7.jpg   |
:----------+------------+-----+------------:
|   [ ]    | tutor1.jpg                    |
:----------+------------+-----+------------:
|   [✓]    |    [✓]     | [✓] | tutor2.jpg |
:----------+------------+-----+------------:
|   [ ]    | ch1.jpg                       |
:----------+------------+-----+------------:
|   [ ]    | ch2.jpg                       |
:----------+------------+-----+------------:
|   [✓]    |    [✓]     | [✓] | ch3.jpg    |
:----------+------------+-----+------------:
|   [ ]    | ch4.jpg                       |
:----------+------------+-----+------------:
| [Submit] |                               |
'----------'------------'-----'------------'

I need this.

.----------.------------.
|   [✓]    |  ch7.jpg   |
:----------+------------:
|   [ ]    | tutor1.jpg |
:----------+------------:
|   [✓]    | tutor2.jpg |
:----------+------------:
|   [ ]    | ch1.jpg    |
:----------+------------:
|   [ ]    | ch2.jpg    |
:----------+------------:
|   [✓]    | ch3.jpg    |
:----------+------------:
|   [ ]    | ch4.jpg    |
:----------+------------:
| [Submit] |            |
'----------'------------'

using $selected = in_array($row_parent['mupload_filename'], $childArrayFilename); you can avoid if else loop which is not much readable.

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

The code is fixed after removing the for loop and added the parent id to it.

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM