Following reusable model codes are mentioned for codeignitor framework
function findByField($field, $value, $tbl){ $this->db->from($tbl); $this->db->where($field, $value); $query = $this->db->get(); $result = ( $query->num_rows() >= 1 ) ? $query->row() : NULL; return $result; } function findRows( $tbl , $where = null ){ //$this->db->where($field, $value); if(empty($where)){ $this->db->from($tbl); $query = $this->db->get(); $result = ( $query->num_rows() >= 1 ) ? $query->result() : NULL; }else{ $query = $this->db->get_where($tbl, $where); $result = $query->result(); } return $result; } function queryDb( $query ){ $query = $this->db->query($query); $result = $query->result(); //echo '<pre>'; print_r($rows); exit; return $result; } /* * create / update */ function updateRow($data, $id = null, $tbl){ //echo '<pre>';print_r($data); exit; if($id != null){ $this->db->update($tbl, $data, array('id' => $id)); return (( $this->db->affected_rows() == 1 ) ? true : false); }else{ $this->db->insert($tbl, $data); return $this->db->insert_id(); } } function deleteRow($id,$tbl){ $where = array('id' => $id); $this->db->delete($tbl, $where); return (( $this->db->affected_rows() == 1 ) ? true : false); } function deleteByField( $id, $field, $tbl ){ $where = array( $field => $id); $this->db->delete($tbl, $where); return (( $this->db->affected_rows() == 1 ) ? true : false); }