Welcome Guest, Not a member yet? Register   Sign In
Need Help With Form Submission
#1

[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">&lt;?php echo  $item['locationid'];?&gt;</td>
    <td width="100" align="center">&lt;?php $data = array('name'=>$item['location'], 'size'=>20,'value' => $item['location']); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=>$theDates[0],'size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[0]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=>$theDates[1],'size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[1]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=>$theDates[2],'size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[2]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=> $theDates[3],'size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[3]))); echo form_input($data); ?&gt;</td>
    <td width="150"> &lt;?php echo anchor('admin/pickup_detail/deletelocation/'. $item['location'], 'Delete');?&gt;</td>

  </tr>
&lt;?php  ;} ?&gt;

</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 !!


#2

[eluser]CroNiX[/eluser]
Where are you setting the form element with the name of 'locationid'?
It looks like:
Code:
$data = array('name'=>$item['location'], 'size'=>20,'value' => $item['location']); echo form_input($data);
should be
Code:
$data = array('name'=>'locationid', 'size'=>20,'value' => $item['location']); echo form_input($data);
??

Are you sure $_POST['locationid'] is an array? It seems it would be a single integer, since it's an id to a particular location.

In your Updatelocation() method, you are overriding the 'dates' key, so the final array will only contain the last date. You also don't define $locationid, but use it in your where(). You also enclose '$theDates[0]' in single quotes, which will be taken literally and not a variable.
#3

[eluser]vincej[/eluser]
Hi CroxNix - Thanks so much for riding over the hill and coming to the rescue ! apologies for being such a Dufus .

following your recommendations I have made some alterations to the code - I hope correctly and in the right places. I seem to be impacting the DB now as all the existing test dates have been set to 0 and the locationid is now set to a single location.

However, on the not so good front, I'm still deep in the weeds with several errors. The DB is being updated with a single location, and the dates themselves are not getting through. My CI form is also now giving me lots of 'Undefined Offset'. I'm also not getting hardly anything off the Post array:
Code:
Array ( [location] => Varsity [0] => Wed 31 Dec 1969 [locationid] => 2 [submit] => Save Changes )


Here is the current:
View:

Code:
&lt;?php
echo form_open('admin/pickup_detail/Updatelocation');
?&gt;

<table width="800" border="" cellpadding="5" align="center"  >
  <tr>
    <th width="10" scope="col">Pick Up ID</th>
    <th  width="75" scope="col">Location</th>
    <th width="75" scope="col">Date 1 </th>
    <th width="75" scope="col">Date 2 </th>
    <th width="75" scope="col">Date 3</th>
     <th width="75" scope="col">Date 4</th>
    <th width="75" scope="col">Delete</th>
&lt;?php
foreach ($pickupdates as $item) {
   $theDates = explode(',', $item['TheDates']);
  ?&gt;
<tr align="center">
  <td width="100" align="center">&lt;?php echo  $item['locationid'];?&gt;</td>
    <td width="100" align="center">&lt;?php $data = array('name'=>'location', 'size'=>20,'value' => $item['location']); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=> $theDates[0],'size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[0]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=> $theDates[1],'size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[1]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=>$theDates[2],'size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[2]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=>$theDates[3],'size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[3]))); echo form_input($data); ?&gt;</td>
    <td width="150"> &lt;?php echo anchor('admin/pickup_detail/deletelocation/'. $item['location'], 'Delete');?&gt;</td>

  </tr>
&lt;?php  ;} ?&gt;

</table>

&lt;?php
form_input($data);
echo form_hidden('locationid', $item['locationid']);
echo "<div class=submitted>". form_submit('submit','Save Changes')."</div>";
echo form_close();
?&gt;


Model
Code:
function Updatelocation(){
    foreach( $_POST as $pickupid=>$location) {
    $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->update('pudates',$data);
  $this->session->set_flashdata('message', 'Table Updated');
   }
  
  }
#4

[eluser]vincej[/eluser]
Update: I have tried manually entering dates into the DB. 2 things happen. Firstly the 'Offsets' disappear - Good. However, when I do a 'Submit' 2 things can be observed:

1 - All the entered dates are attributed to a single location '2' ,
2 - All the manually entered dates are set to Zero.
#5

[eluser]CroNiX[/eluser]
This is part of your problem:
Code:
$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]'])
);

if you check $data, it will probably only contain:
Code:
$data = array(
  'locationid' => db_clean($_POST['locationid']),
  'dates' => db_clean($_POST['TheDates[3]'])
);
(the last date) since you are using the same key, 'dates' for each one.

You also have no where() statement in your update(), so it will update all. You will need a unique identifier for your where, like puid instead of just locationid (since there are many ids with the same locationid).
#6

[eluser]vincej[/eluser]
Quote:if you check $data, it will probably only containSadthe last date) since you are using the same key, ‘dates’ for each one.

Ok - I get that, but how do I overcome that .. I mean, all my dates go into the very same column. How do I refer to the same column without suing the same key ?



#7

[eluser]CroNiX[/eluser]
They go into the same column, but in different rows. The row that matches the puid that you need in your where(). First, you need to get the puid in your form.

If you try to update by locationid, you will have problems. Like for locationid = 1, there are 4 of them.
For each date you need to know the locationid AND the puid so you can:
Code:
$data = array('dates' => the_date);
$this->db
  ->where('locationid', $locationid) //don't actually need locationid, but DO need puid
  ->where('puid', $puid)
  ->update('table', $data);
#8

[eluser]vincej[/eluser]
Ok - CroxNix - if I could buy you a beer I'd buy you 10!
Changed my query to include, puid. so it now read:

Code:
function Updatelocation(){
    foreach( $_POST as $pickupid=>$location) {
    $data = array(
  'puid' => db_clean($_POST['puid']),
  '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($_POST['puid'], 'puid');
  $this->db->update('pudates',$data);
  $this->session->set_flashdata('message', 'Table Updated');
   }
  
  }

and I still get "undefined Index TheDates[0]" through to [3]

I'm also getting:

Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\CountryLane\system\core\Exceptions.php:185)

#9

[eluser]vincej[/eluser]
HI CroxNix - Ok, I have been working on my model and have made a few changes. Firstly for the benefit of clarity I removed all the locations barr 1. Secondly I put specific 'names' into the CI form within the view. Thirdly I changed my model to reflect the changes in the form and I added a strtotime() so that the DB get UNIX date stamps. All the undeclared indexes are gone. also the POST array is now giving me all the dates from the from - all good. BUT, my update query is still not updating the DB. I think this is because with only a single pickup id ( puid ) it is trying to push 4 dates into a single row .. that isn't going to work, unless I perhaps missed something big. So ... maybe I should do an IMPLODE() on the POST array so can get them all on 1 row with 1 puid ? If I was successfull doing that then, I would not need GROUP_CONCAT() at the initial SELECT time. I would rather not do it as it could add more complexity ... but if needs must ..


I am really sorry to be a burden, but this has really befuddled me. anyway - here is my data"

POST:
Code:
Array ( [location] => Collingwood [Dates1] => Wed 31 Dec 1970 [Dates2] => Wed 31 Dec 1969 [Dates3] => Wed 31 Dec 1969 [Dates4] => Wed 31 Dec 1969 [locationid] => 4 [puid] => 4 [submit] => Save Changes )

View:
Code:
&lt;?php
foreach ($pickupdates as $item) {
   $theDates = explode(',', $item['TheDates']);
  ?&gt;
<tr align="center">
  <td width="100" align="center">&lt;?php echo  $item['locationid'];?&gt;</td>
    <td width="100" align="center">&lt;?php $data = array('name'=>'location', 'size'=>20,'value' => $item['location']); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=> 'Dates1','size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[0]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=> 'Dates2','size'=>15,'value' =>(strftime("%a %d %b %Y",$theDates[1]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=>'Dates3','size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[2]))); echo form_input($data); ?&gt;</td>
    <td width="100">&lt;?php $data = array('name'=>'Dates4','size'=>15,'value' => (strftime("%a %d %b %Y",$theDates[3]))); echo form_input($data); ?&gt;</td>
    <td width="150"> &lt;?php echo anchor('admin/pickup_detail/deletelocation/'. $item['location'], 'Delete');?&gt;</td>

  </tr>
&lt;?php  ;} ?&gt;

</table>

&lt;?php
form_input($data);
echo form_hidden('locationid', $item['locationid']);
echo form_hidden('puid', $item['puid']);
echo "<div class=submitted>". form_submit('submit','Save Changes')."</div>";
echo form_close();


Model:

Code:
function Updatelocation(){
    foreach( $_POST as $pickupid=>$location) {
  $puid = $_POST['puid'];
  $locationid = $_POST['locationid'];  
    
    $data = array(
  'locationid' => db_clean($locationid),
  'puid' => db_clean($_POST['puid']),
  'dates' => db_clean(strtotime('Dates1')),
  'dates' => db_clean(strtotime('Dates2')),
  'dates' => db_clean(strtotime('Dates3')),
  'dates' => db_clean(strtotime('Dates4'))
    );
  $this->db->where('locationid',$locationid);
  $this->db->where('puid',$puid);
  $this->db->update('pudates',$data);
  $this->session->set_flashdata('message', 'Table Updated');
   }
  
  }
#10

[eluser]CroNiX[/eluser]
Each date should have its own puid, but the same locationid. So in your post data, there should be 4 puids, 4 dates and 1 locationid (since they all share the same location). You will need to keep track of what puid belongs to each date, maybe each (puid1, puid2, etc) in a hidden form field.

Then your updates would look something like:
Code:
$locationid = $this->input->post('locationid');
$puid1 = $this->input->post('puid1');
$date1 = strtotime($this->input->post('Dates1'));
$puid2 = $this->input->post('puid2');
$date2 = strtotime($this->input->post('Dates2'));

//update date1
$this->db
  ->where('puid', $puid1)
  ->where('locationid', $location_id)
  ->update('pudates', array('dates' => $date1));

//update date2
$this->db
  ->where('puid', $puid2)
  ->where('locationid', $location_id) //again, this isn't REALLY needed since PUID is unique
  ->update('pudates', array('dates' => $date2));
//repeat for other 2

Note, this isn't how I would actually do it. 4 queries is inefficient but I wanted to show you where you are going wrong. I'd look into update_batch() to do this in a single query instead of 4.




Theme © iAndrew 2016 - Forum software by © MyBB