Jump to content

Need Help with Uploading Form Data


vincej

Recommended Posts

Hi - I use PHP and CodeIgniter to create a Form which allows the user to amend and then submit the

 

dates and locations. It looks like this:

 

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:

 

 

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:

 

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

 

<?php 
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. The form presents the data fine however my amends to the form do  not update the Database.  I get an error saying invalid argument of the 'Foreach'  in the model code below. I know that my model does not work and is flawed. I am not sure how to properly construct the $Data array in the CI form so that my update controller and update model ( Data Access )  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:

 

function Updatelocation(){
 $this->MPickup->Updatelocation();
redirect('admin/pickup_detail','refresh');
}

 

 

Model

 

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

 

 

 

 

Link to comment
Share on other sites

In your display i cannot see where locationid form variable exists. you are just echoing it out.

Also, foreach() expects it's first parameter ($_POST['locationid'] in your case) to be an array. Which a single id is clearly not (unless you put it inside an array).

 

If you have multiple locationid's and updating multiple forms at once, you will have to iterate each form element for ex;

 

HTML:

<form>
<input name="locationid[0]">
<input name="location_name[0]">
<input name="location_date1[0]">
<input name="location_date2[0]">
--- second form
<input name="locationid[1]">
<input name="location_name[1]">
<input name="location_date1[1]">
<input name="location_date2[1]">
--- etc...

 

PHP:

<?php

if( isset( $_POST['locationid'] ) && count( $_POST['locationid'] ) >= 1){
      
      foreach( $_POST['locationid'] as $formId => $locationId ){
            
            $thisForm = array( 
                  "id" => $locationId, 
                  "name" => $_POST['location_name'][$formId], 
                  "date1" => $_POST['location_date1'][$formId], 
                  "date2" => $_POST['location_date2'][$formId] 
            );
            
            // input validation etc
            // then update database using new values gained.
      }
      
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.