[eluser]vincej[/eluser]
Hi - I have a CI form which is populated with values from a DB by looping through the DB values using a foreach. It works fine. However, I want to be able to amend those values and then return the new values back to the DB.
The problem is that because the form is built with a foreach only the very last row of DB values can be amended. This is because the previous rows get overwritten as the foreach loops through.
Question: what is the mechanism I need to use to update the values from the whole form ??
Here is the code as it exists at the moment:
Model:
Code:
function Updatelocation(){
$data = array( 'pickuplocation' => db_clean($_POST['pickuplocation']),
'Date1' => db_clean($_POST['Date1']),
'Date2' => db_clean($_POST['Date2']),
'Date3' => db_clean($_POST['Date3']),
);
$this->db->where('pickuplocation', $_POST['pickuplocation']);
$this->db->update('pickup',$data);
}
Controller:
Code:
function UpdateLocation(){
$this->MPickup->UpdateLocation();
redirect('admin/pickup_detail','refresh');
}
View:
Code:
<?php
echo form_open('admin/pickup_detail/Updatelocation');
?>
<table width="800" border="30" cellpadding="5" align="center" >
<tr>
<!-- <th scope="col">Pick Up ID</th>-->
<th width="147" scope="col">Pick Up Location</th>
<th width="98" scope="col">Date 1 </th>
<th width="98" scope="col">Date 2 </th>
<th width="98" scope="col">Date 3</th>
<?php
foreach ($query->result_array() as $row) {
?>
<tr align="center">
<td><?php $data = array('name'=>'pickuplocation','id'=>$row['pickupid'],'size'=>20,'value' => $row['pickuplocation']); echo form_input($data); ?></td>
<td><?php $data = array('name'=>'Date1','id'=>$row['Date1'],'size'=>20,'value' =>$row['Date1']); echo form_input($data); ?></td>
<td><?php $data = array('name'=>'Date2','id'=>$row['Date2'],'size'=>20,'value' => $row['Date2']); echo form_input($data); ?></td>
<td><?php $data = array('name'=>'Date3','id'=>$row['Date3'],'size'=>20,'value' => $row['Date3']); echo form_input($data); ?></td>
</tr>
<?php } ?>
</table>
<?php
echo "<div class=submitted>". form_submit('submit','Save Changes')."</div>";
echo form_close();
?>