Denny Kurniawan
Denny Kurniawan

Reputation: 1915

Datatables codeigniter when using filter not work

I write query to my model in codeigniter it's return error HTTP 500 when accessed by ajax, but when using browser its no HTTP error code:

 $query = 'select album.id,
                  album.name,
                  artist.name as artist,
                  artist.name as artistid,
                  album.photo,
                  album.active,
                  album.reasonactive from ' . $this->table . 
                  ' join artist on artist.id = album.ref_artist ';
      if(isset($_POST["search"]["value"])){
         $query .= '(artist.name LIKE "%'.$_POST["search"]["value"].'%" ';
         $query .= 'OR album.name LIKE "%'.$_POST["search"]["value"].'%" ';
      }
      if(isset($_POST["order"])){
         $query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
        }
        else
        {
         $query .= ' ORDER BY album.name asc ';
        }
        if($_POST['length'] != -1)
           $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
        $result = $this->db->query($query);
        return $result->result();

This is my ajax code :

$(document).ready(function() {
  table = $('#table').DataTable({

      "processing": true, //Feature control the processing indicator.
      "serverSide": true, //Feature control DataTables' server-side processing mode.
      "order": [], //Initial no order.

      // Load data for the table's content from an Ajax source
      "ajax": {
          "url": "<?php echo base_url('Manage/get_all')?>",
          "type": "POST"
      },

      //Set column definition initialisation properties.
      "columnDefs": [
          {
              "targets": [ 0 ], //first column
              "orderable": false, //set not orderable
          },
          {
              "targets": [ -1 ], //last column
              "orderable": false, //set not orderable
          },

      ],

  });

but when I change and remove the filter query :

   $query = 'select album.id,
                        album.name,
                        artist.name as artist,
                        artist.name as artistid,
                        album.photo,
                        album.active,
                        album.reasonactive from ' . $this->table . ' join artist on artist.id = album.ref_artist ';

code working successfully.

In my codeigniter message:

Notice: Undefined index: length

Notice: Undefined index: start

Notice: Undefined index: draw

And when I show $_POST :

{"draw":null,"recordsTotal":2,"recordsFiltered":2,"data":[]}

How do I solve this problem? Thank you very much for your time and assistance in this matter.

Upvotes: 1

Views: 514

Answers (1)

Vickel
Vickel

Reputation: 8007

There is a problem in your query, therefore the 500 error:

Inside your if clause you are missing a closing bracket ")", it should say

if(isset($_POST["search"]["value"])){
   $query .= ' (artist.name LIKE "%'.$_POST["search"]["value"].'%" ';
   $query .= ' OR album.name LIKE "%'.$_POST["search"]["value"].'%" )';
}

help on how to debug:

you can always print your last query and check with e.g. phpAdmin:

echo ($this->db->last_query());die();

turn on error-reporting in any environment with replacing in your root index.php the following:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

with this line of code

define('ENVIRONMENT', 'development');

which enables you to much more conclusive error messages in your browser's dev console

Upvotes: 2

Related Questions