Search code examples
jqueryajaxcodeigniterdatatabledatatables

CodeIgniter Datatables Server Side Date Range using Datepicker


I am in desperate need of help. For a few weeks, I have tried all the codes possible from the internet to get the Datatables serverside date range using the date picker in Codeigniter but no luck. If the dates are the same then I can get the range but if the range is between the day/month/year day I get a range of all the months or years that the day as. Please advise. Below is my codes for Codeigniter View, Controller and Model:-

View Section

    var table = $('.xtable').DataTable( {
     scrollY:        "400px",
    scrollX:        true,
     scrollCollapse: true,
    autoWidth:      true,
    paging:         false,
   fixedHeader: true,
   processing : true,
   serverSide : true,
   responsive: true,
    stateSave : true,
    deferRender: true,
    ajax: {
   "url": "<?php echo base_url(); ?>getallbillinglist",
   "type": "POST",
   "data": function ( data ) {
      console.log(data);
     data.startdate = $('#startdate').val();//$.trim(fdate1[0]);
      data.enddate = $('#enddate').val();//$.trim(fdate1[1]);
     console.log(data.startdate);
     console.log(data.enddate);
      return data;
    }
     }, ... code continues if not too long
  
   $('#startdate').datepicker({
      autoclose: true
    })

 $('#enddate').datepicker({
  autoclose: true
 })
 $('#startdate, #enddate').change(function () {
    console.log(this.value);
    table.draw();
    //table.columns(12).search(this.value ? '^'+this.value+'$' : '', true, false).draw(); 
    document.getElementById("sdate").value = document.getElementById("startdate").value;
    document.getElementById("edate").value = document.getElementById("enddate").value;
 });
 
 

Controller Section

       public function getallbillinglist()
        {
        if (!$this->ion_auth->logged_in())
         {
            redirect('/');
        }
         $startdate = $this->input->post('startdate');
       $enddate = $this->input->post('enddate');
       $data['data']=$this->Job->get_allbillinglist($startdate,$enddate);
        echo json_encode($data);    
        }

Model Section

          function get_allbillinglist($startdate,$enddate)
{
    $data = array();
    $multipleWhere = ['invoices.Approved' => 1,'invoices.xero' => 0];
    $this->db->select('Client_Name, Invoice_No, Invoice_Date, Due_Date, Item_Descript, Quantity, UCost, Amt, TAmt, Tax, Total, AccCode, AccName, Ship_Name');
    $this->db->from("invoices");
    // $this->db->where('Approved',1);
    $this->db->where($multipleWhere);
    if($startdate!='' && $enddate!=''){
        //$this->db->where('invoices.Invoice_Date BETWEEN "'.$startdate.'" AND "'.$enddate.'"');
        $this->db->where('invoices.Invoice_Date >=', $startdate);
        $this->db->where('invoices.Invoice_Date <=', $enddate);
    }
    $this->db->get();  
    $query1 = $this->db->last_query();
    $multipleWhere1 = ['cninvoices.Approved' => 1, 'cninvoices.xero' => 0];
    $this->db->select('Client_Name, Credit_No, Credit_Date, "", Item_Descript, Quantity, UCost, TISubTotal, TIGST, Tax, TITotal, AccCode, AccName, Ship_Name');
    $this->db->from("cninvoices");
    // $this->db->where('Approved',1);
    $this->db->where($multipleWhere1);
    if($startdate!='' && $enddate!=''){
        // $this->db->where('cninvoices.Credit_Date BETWEEN "'.$startdate.'" AND "'.$enddate.'"');
        $this->db->where('cninvoices.Credit_Date >=', $startdate);
        $this->db->where('cninvoices.Credit_Date <=', $enddate);
    }
    $this->db->get(); 
    $query2 =  $this->db->last_query();
    $Q = $this->db->query($query1." UNION ".$query2." ORDER BY Invoice_No ASC");
    
    if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
            $data[] = $row;
        }
    }
    $Q->free_result();
    return $data;
}

Solution

  • The problem is resolved. I had to change the column type in my MYSQL table in PHPMyAdmin from varchar to date in order to get the date range.