简体   繁体   中英

form input value within foreach loop in codeigniter

I have a form and i am trying to capture its input field values which is within foreach loop (looping through database entry). The problem is i only managed to capture only the value entered at the first input field. Can anyone help me here please. This is my form:

<form action="" method="post" role="form">
    <table class="table table-hover">
        <thead>
            <tr>
                <td>A/C ID</td>
                <td>A/C NAME</td>
            </tr>
        </thead>
        <tbody>
            <?php $i = 1;?>
            <?php foreach ($accounts as $row) { ?>
            <tr>
                <td><?php echo $row->acc_id; ?></td>
                <td><?php echo $row->acc_name; ?></td>
                <td>
                    <input name='amount<?php echo $i;?>'  value="" />
                </td>
            </tr>
            <?php $i++; } ?>
            <button type="submit">View Summary</button>
        </tbody>
    </table>
</form>

And here i how i am access the values in a controller:

$i = 1;
$values = array(
    'amount' . $i => $this->input->post('amount' . $i),
);

print_r($values);
$i++;

You could count the number of results, that you send to your view and then loop through that count in your controller

$count = number of accounts send to the view
$i = 1;

while ($i <= $count) {

    $index = 'amount' . $i; 

    $values[index] =  $this->input->post('amount' . $i;
    $i++;
}

print_r($values);

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