SHARE
TWEET

dataTableModel

Novinaldi Jun 30th, 2020 (edited) 1,008 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class Modelku extends CI_Model
  3. {
  4.     var $table = ''; //nama tabel dari database
  5.     var $column_order = array(); //Sesuaikan dengan field
  6.     var $column_search = array(); //field yang diizin untuk pencarian
  7.     var $order = array('' => ''); // default order
  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) // looping awal
  17.         {
  18.             if ($_POST['search']['value']) // jika datatable mengirimkan pencarian dengan metode POST
  19.             {
  20.  
  21.                 if ($i === 0) // looping awal
  22.                 {
  23.                     $this->db->group_start();
  24.                     $this->db->like($item, $_POST['search']['value']);
  25.                 } else {
  26.                     $this->db->or_like($item, $_POST['search']['value']);
  27.                 }
  28.  
  29.                 if (count($this->column_search) - 1 == $i)
  30.                     $this->db->group_end();
  31.             }
  32.             $i++;
  33.         }
  34.  
  35.         if (isset($_POST['order'])) {
  36.             $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
  37.         } else if (isset($this->order)) {
  38.             $order = $this->order;
  39.             $this->db->order_by(key($order), $order[key($order)]);
  40.         }
  41.     }
  42.  
  43.     function get_datatables()
  44.     {
  45.         $this->_get_datatables_query();
  46.         if ($_POST['length'] != -1)
  47.             $this->db->limit($_POST['length'], $_POST['start']);
  48.         $query = $this->db->get();
  49.         return $query->result();
  50.     }
  51.  
  52.     function count_filtered()
  53.     {
  54.         $this->_get_datatables_query();
  55.         $query = $this->db->get();
  56.         return $query->num_rows();
  57.     }
  58.  
  59.     public function count_all()
  60.     {
  61.         $this->db->from($this->table);
  62.         return $this->db->count_all_results();
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top