0

so i have this controller to run the model function

controller:

public function delete()
{
    if ($this->phome_model->delete_expired()) {
        $this->session->set_flashdata('pesan', 'Success. Back to '. anchor('program', 'home.', 'class="alert-link"'));
        redirect('program/admin/user/sukses');
    } else {
        $this->session->set_flashdata('pesan_error', 'Error. Back to '. anchor('program', 'home.', 'class="alert-link"'));
        redirect('program/admin/user/error');
    }
}

And this is the model:

public function delete_expired()
{
    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_History) >=', 5);
    $this->db->delete('tb_medical_history');

    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_Fisik) >=', 5);
    $this->db->delete('tb_fisik_jiwa');

    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_Radiologi) >=', 5);
    $this->db->delete('tb_radiologi');

    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_Lab) >=', 5);
    return $this->db->delete('tb_laboratorium');
}

what i'm trying to do here is delete those multiple tables where the data is expired (5 years late). on the model function, i need to check if the data on each table is deleted successfuly using the return function and pass it to controller to show the message. but function can have only 1 return, so what do i do to fix this? thanks

3
  • Do you want to know that all queries were successful or that rows were deleted? Commented Jul 24, 2016 at 9:36
  • @splash58 i wanted to know that the rows were deleted succesfuly Commented Jul 25, 2016 at 8:04
  • Query may be successful but does not delete any row due to where condition. So you can check that row is successful and check the amount of deleted rows. These are two different test Commented Jul 25, 2016 at 8:22

1 Answer 1

1

You can use the following code for your model:

public function delete_expired()
{
    $del = array();

    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_History) >=', 5);
    $del[] = (bool) $this->db->delete('tb_medical_history');

    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_Fisik) >=', 5);
    $del[] = (bool) $this->db->delete('tb_fisik_jiwa');

    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_Radiologi) >=', 5);
    $del[] = (bool) $this->db->delete('tb_radiologi');

    $this->db->where('YEAR(CURDATE()) - YEAR(Tanggal_Periksa_Lab) >=', 5);
    $del[] = (bool) $this->db->delete('tb_laboratorium');

    return in_array(false, $del);
}

With this function you store all the return values of the queries in an array. The in_array function searches for false in the $del array, if there is a false in the array that means that there is a queries that has failed.

Sign up to request clarification or add additional context in comments.

Comments

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.