2

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?

8
  • I didn't understand this part from the last paragraph - "This only deletes all the records and never updates". Commented Jun 11, 2016 at 7:49
  • I have clarified the question Commented Jun 11, 2016 at 7:59
  • Okay let me try, if nothing is selected then, there is nothing to save subsequently. if something is selected i don't see any issue with subsequent saving. so everything looks good to me :o correct me if i'm wrong Commented Jun 11, 2016 at 10:47
  • The second arrangement will not save even if there is something selected which does not make sense Commented Jun 11, 2016 at 14:08
  • Well the code seems fine! it should work in ideal scenario, check if there is any error. i don't seem to catch anything. The code fail might happen only if the data being saved is dependent on the data which was deleted from the db. it doesn't seem like it does in the above code. Commented Jun 13, 2016 at 8:49

1 Answer 1

1

For Delete , You can do this on view file like

<input type='checkbox' name='tableId[]' value='<?php echo $value->tableId; ?>"'>

Then on controller like this

function delete_multiple() {
    // Retrive Multiple Id List From Form
    $check_id = $this->input->post('tableId[]');

       // Multiple Delete Loop For Id
       foreach($check_id as $checkId) {
          $this->db->where('tableId', $checkId);
          $this->db->delete('TableName');
        }
}

For Insert , you can do this on view file same as above Then on controller like this

function multiple_add() {
  $table_Id = $this->input->post('tableId[]');

  foreach($table_Id as $tableId) {

    $data_some = array(
    'tableId' => $tableId,
    'blah' => $this->input->post('blah')
    );

    // Insert Data Function Call
    $this->db->insert('TableName', $data_some);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

no queries in controller! it is a "Controller" not a "Model". First get the basics right .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.