[eluser]vincej[/eluser]
Hi - Can someone PLEASE help me figure out how to get my data into my Database from my form ?? I have a CI Form which allows the user to amend and then submit the dates and locations. It looks like this:
Code:
Pick Up ID Location Date 1 Date 2 Date 3 Date 4 Remove?
1 Canmore 1 April 1 May 1 June 1 July Delete
In order to facilitate easy searches and DB management the Dates Table looks like this:
Code:
puid locationid dates
1 1 1331668779
15 1 1383668779
12 1 1311746700
5 1 1321746641
16 2 1381668779
9 2 1331746700
Every date falls within the same DB column.
Using Group_CONCAT in the query and EXPLODE in the view , It all works very well for pivoting the dates into a horizontal display.
The resulting array which feeds the form looks like this:
Code:
Array
(
[0] => Array
(
[location] => Banff
[locationid] => 1
[TheDates] => 1311746700,1321746641,1331668779,1383668779
)
[1] => Array
(
[location] => Canmore
[locationid] => 3
[TheDates] => 1311746700,1321746641,1331746641,1371668779
)
[2] => Array
(
[location] => Collingwood
[locationid] => 4
[TheDates] => 1311746700,1321746641,1331746641,1371668779
)
As you can see TheDates for each location is held in a comma delimited string. They are exploded out such that they can be placed into the view using this code:
View
Code:
foreach ($pickupdates as $item) {
$theDates = explode(',', $item['TheDates']);
?>
<tr align="center">
<td width="100" align="center"><?php echo $item['locationid'];?></td>
<td width="100" align="center"><?php $data = array('name'=>$item['location'], 'size'=>20,'value' => $item['location']); echo form_input($data); ?></td>
<td width="100"><?php $data = array('name'=>$theDates[0],'size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[0]))); echo form_input($data); ?></td>
<td width="100"><?php $data = array('name'=>$theDates[1],'size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[1]))); echo form_input($data); ?></td>
<td width="100"><?php $data = array('name'=>$theDates[2],'size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[2]))); echo form_input($data); ?></td>
<td width="100"><?php $data = array('name'=> $theDates[3],'size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[3]))); echo form_input($data); ?></td>
<td width="150"> <?php echo anchor('admin/pickup_detail/deletelocation/'. $item['location'], 'Delete');?></td>
</tr>
<?php ;} ?>
</table>
At this point things stop working properly. I know that my model does not work and is flawed. I get an error saying invalid argument of the 'Foreach' in the model code below. I am not sure how to properly construct the $Data array in the CI form so that my update controller and update model amend the fields correctly bearing in mind the nature of the data structures and the fact that it is being presented in a converted Human readable date yet stored as date stamps.
Controller:
Code:
function Updatelocation(){
$this->MPickup->Updatelocation();
redirect('admin/pickup_detail','refresh');
}
Model
Code:
function Updatelocation(){
if( isset( $_POST['locationid'] ) ) {
foreach( $_POST['locationid'] as $pickupid=>$location) {// ERROR; "Invalid Arguement provide in Foreach"
$data = array(
'locationid' => db_clean($_POST['locationid']),
'dates' => db_clean($_POST['$theDates[0]']),
'dates' => db_clean($_POST['$theDates[1]']),
'dates' => db_clean($_POST['$theDates[2]']),
'dates' => db_clean($_POST['$theDates[3]']),
);
$this->db->where('locationid', $locationid);
$this->db->update('pudates',$data);
$this->session->set_flashdata('message', 'Table Updated');
}
}
else echo "Update Failed";
}
If ANY ONE can provide me a steer in the right direction I will be forever grateful !!
Many Thanks !!