Search code examples
phpmysqlflashcodeigniter

method to find actually data deleted or not in database in codeigniter


Hi every is there any way to check whether data is actually deleted from database or not in codeigniter ? i have a method to delete data from database in my model as

             $this->db->delete('product', array('category' => $id)); 

and i have set flash datain my controller the data from database doesn't gets deleted but the flash message is shown.

I need to show flash data only on actual delete from database.


Solution

  • i prefer this

    in model for example called 'products'

    function deleteProductBy( $id ) {
        $this->db->where( 'category', $id );
        $this->db->delete( 'product' );
    
        if ( $this->db->affected_rows() == '1' ) {return TRUE;}
        else {return FALSE;}
    
    } 
    

    in controller

      if( $this->products->deleteProductBy($id) == false ){
    
       // fail message is sad
    } 
    else{
       // success message is happy
    }
    

    so have the delete method in the model just return true or false -- then in the controller you decide how you are going to show the results. then when something changes for how you show the results - the delete method in the model remains the same. and while i'm rambling my suggestion is to check for false condition first.