Welcome Guest, Not a member yet? Register   Sign In
Only the last entry in the array is saving to the database?

In my model I have an array being sent from the controller and I want all entries in the array to
save to the db but only the last one is.

My code in the controller is

   $data['extra_items'] = $extra_items; // Populate extra_item dropdown Data  
   $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        if ($this->form_validation->run() == FALSE) {
               // Extra Item Not Created
        }else {
             $date = new Extra_item_date();
             $item_rows = array();
             $post_amounts = $this->input->post('extra_item_amount');  
             $post_hours = $this->input->post('extra_item_hours');  
             $post_items = $this->input->post('extra_item_id');
                  foreach($post_amounts AS $index => $item) {
                      if(!empty($post_hours[$index])&&(!empty($post_items[$index]))) {
                          echo "definately here  ";  
                          $item_rows[] = array('amount' => $item, 'hours' => $post_hours[$index], 'extra_item' => $post_items[$index]);
                 $date->item_rows = $item_rows;
                     if($date->create() === TRUE) { // Attempt to create the extra_item_date data
                         // Success
                         $data['success'] = 'The extra item data has been entered into database.';
                         $data['is_cleared'] = TRUE; // Form is now cleared
                     }else {
                         // Failure
                         $data['error'] = $date->error->create;
}else {
$data['error'] = 'No extra items for your login.';

My code in the model is
function create ()
 $item_rows = $this->item_rows;

   $extra = new Extra_item_date();

   foreach($item_rows AS $row) {
      $extra->extra_item_amount = $row['amount'];
      $extra->extra_item_hours = $row['hours'];
      $extra->extra_item_id = $row['extra_item'];
      $success = $extra->save();
      echo "new entry";
       if(!$success) {
         $this->error_message('create', 'Failed to create the new Extra item data in model.');
         return FALSE;
       }else {
         return TRUE;

what comes out in the browser =

definately here definately here Array ( [0] => Array ( [amount] => 25 [hours] => 2 [extra_item] => 1 ) [1] => Array ( [amount] => 35 [hours] => 5 [extra_item] => 2 ) ) 2521new entry3552new entry

so i thought it would do the $success = $extra->save(); twice, once each time just before
the echo "new entry"; but it only does a save to the database once at the end so only the
values of [1] => Array ( [amount] => 35 [hours] => 5 [extra_item] => 2 )  get saved to the db!

Does anyone have a suggestion for changing the model code so 2 entries get saved to db??

thanks so much for your time

I'm guessing that your Extra_item_date class is inserting the first row, then updating that row with the data for each subsequent row, because you're only creating a single instance of the Extra_item_date class (before the foreach loop), then using a save() method to save the changes made to that instance on each iteration of the loop. If you move the $extra = new Extra_item_date(); line into the loop, it will probably insert each of them.

If creating a new object for each iteration of the loop is a concern, your best bet would be to find a way to perform a batch insert.

you're a genius!!! thank you sooo much!!

Theme © iAndrew 2016 - Forum software by © MyBB