'Codeigniter - foreach form input table
I have a foreach loop contained within a form there an input is required. I want to submit these all of input values to the database. Currently it only submits the last entry. I have tried a few different things like name="[]", Im not sure whether my issue lies in the view page or the values being picked up in the controller.
This is my view:
<form method="post" id="add_entry" name="add_entry" action="<?= base_url() ?>/WebAppController/add_test_batch">
<table id="example1" class="display text-center mt-5">
<thead>
<tr>
<th>Phase</th>
<th>Ingredient</th>
<th>Instructions</th>
<th>%</th>
<th>Amount (g)</th>
<th>Actual (g)</th>
</tr>
</thead>
<tbody>
<?php foreach($batch as $batch){?>
<tr>
<td><?php echo $batch->f_ingredients_phase?></td>
<td><?php echo $batch->ingredient_name?></td>
<td><?php echo $batch->f_ingredients_instructions?></td>
<td><?php echo $batch->f_ingredients_qty?></td>
<td>
<?php
$first_number = $batch->f_ingredients_qty;
$second_number = $batch->product_qty;
$third_number = 100;
$sum_total = $second_number * $first_number / $third_number;
print ($sum_total);
?>
</td>
<td>
<input type="hidden" id="<?php echo $batch->product_id?>" name="production_batch_production[]" value="<?php echo $batch->product_id?>">
<input type="hidden" id="<?php echo $batch->ingredient_id?>" name="production_batch_ingredients[]" value="<?php echo $batch->ingredient_id?>">
<input type="text" class="form-control text-center" id="production_batch_qty" name="production_batch_qty[]" placeholder="Enter Actual Qty">
</td>
</tr>
<?php }?>
</tbody>
</table>
<div class="container text-center mt-3">
<button type="submit" class="btn btn-sm btn-success mt-3 col-2">Product Produced</button>
</div>
</form>
This is my Controller:
public function add_test_batch() {
$alltestbatch = new RdProduction_BatchModel();
$data = [
'production_batch_qty' => $this->request->getVar('production_batch_qty'),
'production_batch_production' => $this->request->getVar('production_batch_production'),
'production_batch_ingredients' => $this->request->getVar('production_batch_ingredients'),
];
$alltestbatch->insert($data);
return redirect()->back();
}
var_dump produces the following:
array(4) { [0]=> string(4) "2500" [1]=> string(4) "1250" [2]=> string(4) "1000" [3]=> string(3) "250" } array(4) { [0]=> string(1) "6" [1]=> string(1) "6" [2]=> string(1) "6" [3]=> string(1) "6" } array(4) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "1" [3]=> string(1) "3" }
Using the die; I get an Array, stdClass Object, see below. Is then when I need to use " for($i=0;$i " as ive seen it alot in answers to similar questions?
Array([0] => stdClass Object(
[product_id] => 6
....
[plan_status] => New)
[1] => stdClass Object
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
