HealMee
HealMee

Reputation: 85

Delete multiple table row - PHP Codeigniter

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

Upvotes: 0

Views: 443

Answers (1)

Daan van Berkel
Daan van Berkel

Reputation: 61

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.

Upvotes: 1

Related Questions