Pagination issue. in the next page returned 20records(11 to 30

Pagination issue. in the next page returned 20records(11 to 30

Ramzi55Ramzi55 Posts: 6Questions: 2Answers: 0
edited January 12 in Free community support

everything is good .. the issue is only on pagination . in the first page displayed 10 records, but when user clicked next page it shows 20 records (from 11 to 30 ) idk why ..
my js script:

$(document).ready(function() {
    $('#Table1').DataTable( {
        "processing": true,
        "serverSide": true,
         "deferRender": true,
        "ajax":{
          "url": "<?php echo base_url().'Pembelian/get_ajax';?>",
          "type": "POST"
                } 
    } );
} );

this is my controller to fetch data :

  function get_ajax() {
        $list = $this->Pembelian_model->get_datatables();
        $data = array();
        $no = @$_POST['start'];
        foreach ($list as $item) {
            $no++;
            $row = array();
            $row[] = $no.".";
            $row[] = $item->no_bukti;
            $row[] = $item->tgl_beli;
            $row[] = $item->kode_ctr;
            $row[] = $item->nama_ctr;
            // add html for action
            $row[] = '<button type="button" class="btn btn-info btn-xs" data-toggle="modal" value="'.$item->no_bukti.'" id="view">
                      <i class="fa fa-eye"></i></button>
                      <button type="button" class="btn btn-danger btn-xs" value="'.$item->no_bukti.'" id="delete"><i class="fa fa-trash"></i></button>';
            $data[] = $row;
        }
        $output = array(
                    "draw" => @$_POST['draw'],
                    "recordsTotal" => $this->Pembelian_model->count_all(),
                    "recordsFiltered" => $this->Pembelian_model->count_filtered(),
                    "data" => $data,
                );
        // output to json format
        echo json_encode($output);
    }

this is my model :

 // start datatables
    var $column_order = array(null, 'no_bukti', 'tgl_beli', 'kode_ctr', 'nama_ctr'); //set column field database for datatable orderable
    var $column_search = array('no_bukti', 'tgl_beli', 'a.kode_ctr', 'nama_ctr'); //set column field database for datatable searchable
    var $order = array('no_bukti' => 'asc'); // default order 
 
    private function _get_datatables_query() {
        $this->db->select('a.*,b.nama_ctr');
        $this->db->from('mcmbelih a');
        $this->db->join('mcmcenter b', 'b.kode_ctr = a.kode_ctr');
        $i = 0;
        foreach ($this->column_search as $item) { // loop column 
            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)]);
        }
    }
    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();
    }
    function count_filtered() {
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    }
    function count_all() {
        $this->db->from('mcmbelih');
        return $this->db->count_all_results();
    }
    // end datatables

i hope that i can get somehelp or advise to solve this issu ?

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 53,561Questions: 1Answers: 8,303 Site admin
    Answer ✓

    It sounds like your server-side processing isn't taking into account the start / page length correctly. If you look at the JSON response from the server, I suspect you'll see that it is returning 20 records on the second page.

    To debug that, I'd suggest you have a look at the SQL statement your PHP code is building for that second page of data. I'm guessing there will be something wrong with the limit / offset.

    Allan

  • Ramzi55Ramzi55 Posts: 6Questions: 2Answers: 0

    thank you soo much for ur reply and ur help .. yes as u suggested .. i did as u asked me to do and exactly as u said .. i found something wrong in my php ...

    thanks again sir :) :) :)

Sign In or Register to comment.