Advertisement
darmariduan

models/Resis.php

Jul 31st, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Resis extends CI_Model {
  5.  
  6.    var $table = 't_resis';
  7.    var $column_order = array(null, 'nama','no_resi','Pengiriman', null); //set column field database for datatable orderable
  8.    var $column_search = array('nama','no_resi','Pengiriman'); //set column field database for datatable searchable
  9.    var $order = array('id_resi' => 'asc'); // default order
  10.  
  11.    public function __construct()
  12.    {
  13.       parent::__construct();
  14.    }
  15.  
  16.    private function _get_datatables_query()
  17.    {
  18.  
  19.       $this->db->from($this->table);
  20.  
  21.       $i = 0;
  22.  
  23.       foreach ($this->column_search as $resi) // loop column
  24.       {
  25.          if($_POST['search']['value']) // if datatable send POST for search
  26.          {
  27.  
  28.             if( $i === 0 ) // first loop
  29.             {
  30.                $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
  31.                $this->db->like($resi, $_POST['search']['value']);
  32.             } else {
  33.                $this->db->or_like($resi, $_POST['search']['value']);
  34.             }
  35.  
  36.             if(count($this->column_search) - 1 == $i) {//last loop
  37.                $this->db->group_end(); //close bracket
  38.             }
  39.          }
  40.          $i++;
  41.       }
  42.  
  43.       if(isset($_POST['order'])) // here order processing
  44.       {
  45.  
  46.          $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
  47.  
  48.       } else if(isset($this->order)) {
  49.  
  50.          $order = $this->order;
  51.          $this->db->order_by(key($order), $order[key($order)]);
  52.  
  53.       }
  54.    }
  55.  
  56.    function get_datatables()
  57.    {
  58.       $this->_get_datatables_query();
  59.  
  60.       if($_POST['length'] != -1)
  61.       {
  62.          $this->db->limit($_POST['length'], $_POST['start']);
  63.          $query = $this->db->get();
  64.          return $query->result();
  65.       }
  66.    }
  67.  
  68.    function count_filtered()
  69.    {
  70.       $this->_get_datatables_query();
  71.       $query = $this->db->get();
  72.       return $query->num_rows();
  73.    }
  74.  
  75.    function count_all()
  76.    {
  77.       $this->db->from($this->table);
  78.       return $this->db->count_all_results();
  79.    }
  80.  
  81.    function insert($table = '', $data = '')
  82.    {
  83.       $this->db->insert($table, $data);
  84.    }
  85.  
  86.     function insert_last($table = '', $data = '')
  87.    {
  88.       $this->db->insert($table, $data);
  89.  
  90.         return $this->db->insert_id();
  91.    }
  92.  
  93.     function get_all($table)
  94.     {
  95.         $this->db->from($table);
  96.  
  97.         return $this->db->get();
  98.     }
  99.  
  100.     function get_where($table = null, $where = null)
  101.     {
  102.         $this->db->from($table);
  103.         $this->db->where($where);
  104.  
  105.         return $this->db->get();
  106.     }
  107.  
  108.    function update($table = null, $data = null, $where = null)
  109.    {
  110.     $this->db->update($table, $data, $where);
  111.    }
  112.  
  113.    function delete($table = null, $where = null)
  114.    {
  115.     $this->db->where($where);
  116.     $this->db->delete($table);
  117.    }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement