I have a problem slightly different to this and this. In my model class I had to separate the delete and save into two different functions as follows.
#save advanced preferences
function savePreference($preferences){
$this->db->insert('bingo_advanced_preferences', $preferences);
echo $this->db->last_query();
}
#delete advanced preferences
function deletePreference($user_id,$criteria){
return $this->db->delete('bingo_advanced_preferences', array('bingo_user_id' => $user_id,'adv_criteria' =>$criteria));
}
if I call these functions from the controller like this delete, update works.
//language preferences
if($this->input->post('language') && count($this->input->post('language')) > 0):
$this->Bingo_advanced_preferences->deletePreference($user,'15');
for ($i=0; $i < count($this->input->post('language')); $i++) {
$language_options = array(
'bingo_user_id' => $user,
'adv_criteria' => 15, //languages
'adv_criteria_value' => $this->input->post('language')[$i],
'adv_date_created' => date('Y-m-d H:i:s')
);
$this->Bingo_advanced_preferences->savePreference($language_options);
}
endif;
However this doesn't cater for a scenario where somebody deselects everything so the correct order should be like this.
$this->Bingo_advanced_preferences->deletePreference($user,'15');
if($this->input->post('language') && count($this->input->post('language')) > 0):
for ($i=0; $i < count($this->input->post('language')); $i++) {
$language_options = array(
'bingo_user_id' => $user,
'adv_criteria' => 15, //languages
'adv_criteria_value' => $this->input->post('language')[$i],
'adv_date_created' => date('Y-m-d H:i:s')
);
$this->Bingo_advanced_preferences->savePreference($language_options);
}
endif;
This only deletes all the records with adv_criteria=15 and never updates(do the subsequent saving). Of course I have solved my problem with an if else statement so my question is why this doesn't work? How can we make this work?