Advertisement
Guest User

Untitled

a guest
May 27th, 2018
3,701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Person_model extends CI_Model {     var $table = 'persons';     var $column_order = array('firstname','lastname','gender','address','dob',null); //set column field database for datatable orderable     var $column_search = array('firstname','lastname','address'); //set column field database for datatable searchable just firstname , lastname , address are searchable     var $order = array('id' => 'desc'); // default order
  2.  
  3.     public function __construct()
  4.     {
  5.         parent::__construct();
  6.         $this->load->database();
  7.     }
  8.  
  9.     private function _get_datatables_query()
  10.     {
  11.          
  12.         $this->db->from($this->table);
  13.  
  14.         $i = 0;
  15.      
  16.         foreach ($this->column_search as $item) // loop column
  17.         {
  18.             if($_POST['search']['value']) // if datatable send POST for search
  19.             {
  20.                  
  21.                 if($i===0) // first loop
  22.                 {
  23.                     $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
  24.                     $this->db->like($item, $_POST['search']['value']);
  25.                 }
  26.                 else
  27.                 {
  28.                     $this->db->or_like($item, $_POST['search']['value']);
  29.                 }
  30.  
  31.                 if(count($this->column_search) - 1 == $i) //last loop
  32.                     $this->db->group_end(); //close bracket
  33.             }
  34.             $i++;
  35.         }
  36.          
  37.         if(isset($_POST['order'])) // here order processing
  38.         {
  39.             $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
  40.         }
  41.         else if(isset($this->order))
  42.         {
  43.             $order = $this->order;
  44.             $this->db->order_by(key($order), $order[key($order)]);
  45.         }
  46.     }
  47.  
  48.     function get_datatables()
  49.     {
  50.         $this->_get_datatables_query();
  51.         if($_POST['length'] != -1)
  52.         $this->db->limit($_POST['length'], $_POST['start']);
  53.         $query = $this->db->get();
  54.         return $query->result();
  55.     }
  56.  
  57.     function count_filtered()
  58.     {
  59.         $this->_get_datatables_query();
  60.         $query = $this->db->get();
  61.         return $query->num_rows();
  62.     }
  63.  
  64.     public function count_all()
  65.     {
  66.         $this->db->from($this->table);
  67.         return $this->db->count_all_results();
  68.     }
  69.  
  70.     public function get_by_id($id)
  71.     {
  72.         $this->db->from($this->table);
  73.         $this->db->where('id',$id);
  74.         $query = $this->db->get();
  75.  
  76.         return $query->row();
  77.     }
  78.  
  79.     public function save($data)
  80.     {
  81.         $this->db->insert($this->table, $data);
  82.         return $this->db->insert_id();
  83.     }
  84.  
  85.     public function update($where, $data)
  86.     {
  87.         $this->db->update($this->table, $data, $where);
  88.         return $this->db->affected_rows();
  89.     }
  90.  
  91.     public function delete_by_id($id)
  92.     {
  93.         $this->db->where('id', $id);
  94.         $this->db->delete($this->table);
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement