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
Code:
if($extra_items->exists()){
$data['extra_items'] = $extra_items; // Populate extra_item dropdown Data
$this->form_validation->set_rules($config);
$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.';
$this->form_validation->clear();
$data['is_cleared'] = TRUE; // Form is now cleared
}else {
// Failure
$data['error'] = $date->error->create;
}
}
}else {
$data['error'] = 'No extra items for your login.';
}
$this->cfd->build('diet/add_extra_items',$data);
My code in the model is
Code:
function create ()
{
$item_rows = $this->item_rows;
print_r($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