简体   繁体   中英

Codeigniter - loop form input with foreach

I wanted to ask how to make the code can loop the input while also able to call the database to the input(i wanted to make the value by using the database)

View.php

<?php
$i=1;
while ($i<=10){
foreach($tampilan as $u){
    ?>
    <form action="admin/guru_proses" method="POST">
    <table>
        <?php
    echo '
            <tr>
            <td><input type="text" name="id_guru" value="<?php echo $u->id_guru?>"</td>
            <td><input type="text" name="nama_guru"></td>
            </tr>
     ';
    ?>

        <?php
    $i=$i+1;
}}
?>
<tr><td>
    <input type="submit">
</td></tr>
</table>
        </form>

The <?php echo $u->id_guru?> didn't seem to be working well, it becomes like this when you open it

the picture of the view

You can not use php tag inside the php tag.

Please replace below line

<td><input type="text" name="id_guru" value="<?php echo $u->id_guru?>"</td>

To

<td><input type="text" name="id_guru" value="'.$u->id_guru.'"></td>

The second php tag is being used as a simple string. I would change this whole thing into something more readable.

<?php $i=1; ?>
<?php while ($i<=10): ?>
    <?php foreach($tampilan as $u): ?>
    <form action="admin/guru_proses" method="POST">
        <table>
            <tr>
                <td><input type="text" name="id_guru" value="<?php echo $u->id_guru?>"</td>
                <td><input type="text" name="nama_guru"></td>
            </tr>
        <?php $i++ ?>;
    <?php endforeach; ?>
<?php endwhile; ?>
        <tr>
            <td>
                <input type="submit">
            </td>
        </tr>
    </table>
</form>

Change your single quotes to double quotes which you are using for echo.PHP always parse single quotes as string litrel to use the variable inside the string always use double quotes and you cannot use the php tag inside a php tag. try this

<?php
$i=1;
while ($i<=10){
foreach($tampilan as $u){
    ?>
    <form action="admin/guru_proses" method="POST">
    <table>
        <?php
    echo "
            <tr>
            <td><input type='text' name='id_guru' value=$u->id_guru</td>
            <td><input type='text' name='nama_guru'></td>
            </tr>
     ";
    ?>

        <?php
    $i=$i+1;
}}
?>
<tr><td>
    <input type="submit">
</td></tr>
</table>
</form>

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