PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 29, 2022

[FIXED] DataTables Server-side Processing in CodeIgniter but data is not display in table

 January 29, 2022     codeigniter, datatables     No comments   

Issue

I can print the data out but data is not displayed in the table.
I'm trying to use Datatable to display the data.

Here are error messages:

DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

enter image description here

model

function allposts_count()
{   
    $query = $this
            ->db
            ->get('books');

    return $query->num_rows();  

}

function allposts($limit,$start,$col,$dir)
{   
    $query = $this
            ->db
            ->limit($limit,$start)
            ->order_by($col,$dir)
            ->get('books');

    if($query->num_rows()>0)
    {
        return $query->result(); 
    }
    else
    {
        return null;
    }

}

controller

function Book()
{
    $columns = array( 
                        0 =>'id', 
                        1 =>'title',

                    );

    $limit = $this->input->post('length');
    $start = $this->input->post('start');

    $columns=$this->input->post('order')[0]['column'];
    $order = $columns;
    $dir = $this->input->post('order')[0]['dir'];

    $totalData = $this->user_model->allposts_count();

    $totalFiltered = $totalData; 

    if(empty($this->input->post('search')['value']))
    {            
        $posts = $this->user_model->allposts($limit,$start,$order,$dir);
    }
    else {
        $search = $this->input->post('search')['value']; 

        $posts =  $this->user_model->posts_search($limit,$start,$search,$order,$dir);

        $totalFiltered = $this->user_model->posts_search_count($search);
    }

    $data = array();
    if(!empty($posts))
    {
        foreach ($posts as $post)
        {
            $nestedData['id'] = $post->book_id;
            $nestedData['title'] = $post->book_title;

            $data[] = $nestedData;
        }
    }

    $json_data = array(
                "draw"            => intval($this->input->post('draw')),  
                "recordsTotal"    => intval($totalData),  
                "recordsFiltered" => intval($totalFiltered), 
                "data"            => $data   
                );

    $json = json_encode($json_data); 
    echo $json;
    $this->load->view('templates/header');
    $this->load->view('users/Book',$json);
    $this->load->view('templates/footer');
}

view

<div class="container">
    <h1>Server Side Process Datatable</h1>
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>

        </tr>
        </thead>
        <tfoot>
        <tr>
            <th>ID</th>
            <th>Name</th>

        </tr>
        </tfoot>
    </table>

    <!--create modal dialog for display detail info for edit on button cell click-->
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <div id="content-data"></div>
        </div>
    </div>
</div>


<script>
    $(document).ready(function(){
        var dataTable=$('#example').DataTable({
            "processing": true,
            "serverSide":true,
            "ajax":{
             "url": "<?php echo base_url('users/Book') ?>",
         "dataType": "json",
         "type": "POST",
         "data":{  '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>' }
                       },
               "columns": [
              { "data": "book_id" },
              { "data": "book_title" },

           ]     
        });
    });
</script>

Solution

datatables expects a pure json response. if you followed the debugging steps in the link you posted you would have seen that your response also contains a view.

here you have json then a view:

$json = json_encode($json_data); 
      echo $json;
      $this->load->view('templates/header');
      $this->load->view('users/Book',$json);
      $this->load->view('templates/footer');

remove the views and/or move the json stuff to another url. then update "url": "<?php echo base_url('users/Book') ?>", to the new url.



Answered By - Alex
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing