/ Published in: PHP
Expand |
Embed | Plain Text
class News_model extends Model { function News_model(){ parent::Model(); $this->table = 'news'; $this->key = 'post_id'; $this->order = 'post_date desc'; $this->reset(); } foreach($this->fields as $field){ $this->$field = null; } } function select(){ $result = false; foreach($this->fields as $field){ if($this->$field){ $this->db->where($field, $this->$field); } } $this->db->order_by($this->order); $query = $this->db->get($this->table); if($this->post_id){ $result = $query->row(); } else{ $result = $query->result(); } return $result; } function update(){ $result = false; foreach($this->fields as $field){ $entry[$field] = $this->$field; } if($this->post_id){ $this->db->where($this->key, $this->post_id); $this->db->update($this->table, $entry); } else{ $this->db->insert($this->table, $entry); } return $result; } function insert(){ return $this->update(); } function delete(){ if($this->post_id){ $this->db->where($this->key, $this->post_id); $this->db->delete($this->table); } } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

looks like a realy nice template
if you change the delete function like bellow you wouldn't have to touch it at all.
oh!!! it really helped me !!! for object oriented approach its really nice code.... just needs some changes as insert and update function can be merged. i.e. call one function if id is set then update the record or else insert the record... and if fields are given extra space it creates a problem....
thats it !!!! else the function is really nice...thanks for developing.....:)