Welcome Guest, Not a member yet? Register   Sign In
Datatables for Codeigniter 3
#1

Hi
Does anyone have datatables library or anything to use with CI3, and to support joins because of search in database table.
Main problem with this i have is that i need to make model for each database table and new controller. For example i have Vehicles controller and model, and under vehicles model i want to have vehicles insurance, vehicles services tables but i don't want to copy/paste same code for datatables in same file.
Currently i am using it like this
Model:
PHP Code:
    var $table 'vehicle';
    var $select_column = array('Id''VehicleName''DateOfInsurance''Insurer''PolicyNumber''Amount''Bill''NextInsuranceDate''Remark');
    var $order_column = array('Id''VehicleName''DateOfInsurance''Insurer''PolicyNumber''Amount''Bill''NextInsuranceDate''Remark');

    public function make_query()
    {
        $this->db->select($this->select_column);
        $this->db->from($this->table);
        // sent request for search
        if (isset($_POST['search']['value'])) {
            $this->db->group_start();
            $this->db->like('Id'$_POST['search']['value']);
            $this->db->or_like('VehicleName'$_POST['search']['value']);
            $this->db->or_like('DateOfInsurance'$_POST['search']['value']);
            $this->db->or_like('Insurer'$_POST['search']['value']);
            $this->db->or_like('PolicyNumber'$_POST['search']['value']);
            $this->db->or_like('Amount'$_POST['search']['value']);
            $this->db->or_like('Bill'$_POST['search']['value']);
            $this->db->or_like('NextInsuranceDate'$_POST['search']['value']);
            $this->db->or_like('Remark'$_POST['search']['value']);
            $this->db->group_end();
        }

        // sent request for ordering
        if (isset($_POST['order'])) {
            $this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else {
            $this->db->order_by('Id''desc');
        }
    }

    public function make_datatables()
    {
        $this->make_query();
        if ($_POST['length'] != -1) {
            $this->db->limit($_POST['length'], $_POST['start']);
        }
        $query $this->db->get();
        return $query->result();
    }

    public function get_filtered_data()
    {
        $this->make_query();
        $query $this->db->get();
        return $query->num_rows();
    }

    public function get_all_data()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    

Controller:
PHP Code:
$data = array();

 
// fetch vehicles records
 
$fetch_data $this->My_model->make_datatables();

 foreach (
$fetch_data as $veh) {

    $data[] = array(
       $veh->VehicleName,
       (!empty($veh->DateOfInsurance) ? date('d.m.Y'strtotime($veh->DateOfInsurance)) : ''),
       $veh->Amount,
       $veh->Insurer,
       $veh->Remark,
       (!empty($veh->NextInsuranceDate) ? date('d.m.Y'strtotime($veh->NextInsuranceDate)) : '')
    );
 }

 
$output = array(
   'draw' => intval($_POST['draw']),
   'recordsTotal' => $this->My_Model->get_all_data(),
   'recordsFiltered' => $this->My_model->get_filtered_data(),
   'data' => $data
 
);

 echo 
json_encode($output); 
Reply


Messages In This Thread
Datatables for Codeigniter 3 - by brianjamestorr - 11-30-2022, 10:44 AM
RE: Datatables for Codeigniter 3 - by superior - 11-30-2022, 12:30 PM
RE: Datatables for Codeigniter 3 - by InsiteFX - 12-01-2022, 12:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB