File: //var/www/html/pmw24/pmw_live_testing/app/application/models/admin/Mclient.php
<?php
class Mclient extends CI_Model {
var $table = 'client';
var $column_order = array('client_name','client_address','client_phone_no',null); //set column field database for datatable orderable
var $column_search = array('client_name','client_address','client_phone_no'); //set column field database for datatable searchable just firstname , lastname , address are searchable
var $order = array('client_id' => 'asc'); // default order
public function __construct() {
parent::__construct();
}
private function _get_datatables_query(){
$this->db->select('*');
$this->db->join('city','client.city_id=city.city_id','left');
$this->db->from('client');
//$this->db->group_by("parent_id");
$i = 0;
foreach ($this->column_search as $item){ // loop column
//echo $_POST['search']['value'];exit;
if($_POST['search']['value']){ // if datatable send POST for search
if($i===0){ // first loop
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])){ // here order processing
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}else if(isset($this->order)){
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
public function get_datatables(){
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
public function count_filtered(){
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all(){
$this->db->from($this->table);
return $this->db->count_all_results();
}
public function check_city_exsits($city_name){
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('name',$city_name);
$query=$this->db->get();
if($query->result())
{
return 1;
}else{
return 0;
}
}
public function add($data){
$this->db->insert($this->table,$data);
return $this->db->insert_id();
}
public function get_details($client_id){
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('client_id',$client_id);
$query=$this->db->get();
return $query->row_array();
}
public function update($condition,$data){
$this->db->where($condition);
$this->db->update($this->table,$data);
return 1;
}
public function delete($condition){
$this->db->delete($this->table,$condition);
return 1;
}
}