title: Datatables server side processing with using Code Igniter
body: <p>I'm building a forum using Code Igniter framework. I'm trying to implement Datatables server side processing. I know how to use datatables  and JQuery for actions such as sorting and searching, but my database table has the potential to grow to thousands and pulling all the rows at once will not make users very happy, so I'm trying to combine both server side (Code Igniter) and client side (AJAX) processing in order to use limits and offsets. So I dug into the web and found an awesome tutorial. This is what I have:</p>
  
  <p><strong>Model: Posts_model.php</strong></p>
  
  <pre><code>&lt;?php
      var $table = 'general_posts';
      var $column_order = array(null, 'title','body','username','time','category'); //set column field database for datatable orderable
      var $column_search = array('title','body','username','time','category'); //set column field database for datatable searchable 
      var $order = array('id' =&gt; 'desc'); // default descending order 
  
      private function get_posts_query() {        
          $this-&gt;db-&gt;from($this-&gt;table);
          $i = 0; 
          foreach ($this-&gt;column_search as $item) 
          {
              if($_POST['search']['value']) 
              {               
                  if($i===0) // first loop
                  {
                      $this-&gt;db-&gt;group_start(); 
                      $this-&gt;db-&gt;like($item, $_POST['search']['value']);
                  } else {
                      $this-&gt;db-&gt;or_like($item, $_POST['search']['value']);
                  }
                  if(count($this-&gt;column_search) - 1 == $i) //last loop
                      $this-&gt;db-&gt;group_end(); 
              }
              $i++;
          }       
          if(isset($_POST['order'])) { 
              $this-&gt;db-&gt;order_by($this-&gt;column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
          } else if(isset($this-&gt;order)) {
              $order = $this-&gt;order;
              $this-&gt;db-&gt;order_by(key($order), $order[key($order)]);
          }
      }
  
      function get_gen_posts($category) {
          $this-&gt;get_posts_query();
          if($_POST['length'] != -1)
          $this-&gt;db-&gt;limit($_POST['length'], $_POST['start']);
          $this-&gt;db-&gt;where(array('category' =&gt; $category, 'display' =&gt; 'true'));
          $query = $this-&gt;db-&gt;get();
          return $query-&gt;result();
      }
  
      function count_filtered_gen_posts($category) {
          $this-&gt;get_posts_query();
          $this-&gt;db-&gt;where(array('category' =&gt; $category, 'display' =&gt; 'true'));
          $query = $this-&gt;db-&gt;get();
          return $query-&gt;num_rows();
      }
  
      public function count_all($category) {
          $this-&gt;db-&gt;where(array('category' =&gt; $category, 'display' =&gt; 'true'));
          $this-&gt;db-&gt;from($this-&gt;table);
          return $this-&gt;db-&gt;count_all_results();
      }
  
   } ?&gt;
  </code></pre>
  
  <p><strong>Controller: Posts.php</strong></p>
  
  <pre><code>&lt;?php
      public function __construct() {
          parent::__construct();
          $this-&gt;load-&gt;model('posts_model','general_posts');
      }
  
      public function posts($category) {
          $data['category'] = $category;      
          $this-&gt;load-&gt;view('posts', $data);
      }
  
      public function posts_ajax($category)
      {
          $list = $this-&gt;general_posts-&gt;get_gen_posts($category);
          $data = array();
          foreach ($list as $post) {
              $row = array();
              $row[] = $post-&gt;title;
              $row[] = $post-&gt;body;
              $row[] = $post-&gt;category;
              $row[] = $post-&gt;poster;
              $row[] = $post-&gt;time;
              $data[] = $row; 
          }           
          $output = array(
              "draw" =&gt; $_POST['draw'],
              "recordsTotal" =&gt; $this-&gt;general_posts-&gt;count_all($category),
              "recordsFiltered" =&gt; $this-&gt;general_posts-&gt;count_filtered_gen_posts($category),
              "data" =&gt; $data,
          );
          //output to json format
          echo json_encode($output);
      }
  
  } ?&gt;
  </code></pre>
  
  <p><strong>View: posts.php</strong>     </p>
  
  <pre><code>    &lt;table id="table" class="table table-no-border" cellspacing="0" width="100%" style="text-align: left"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
  
  &lt;script type="text/javascript"&gt;
  var table;
  $(document).ready(function() {
      //datatables
      table = $('#table').DataTable({ 
          "paging": true,
          "pageLength" : 10,
          "lengthChange": true, 
          "searching": true,
          "info": false,
          "autoWidth": true,
          "ordering": false,
          "stateSave": true,
          "processing": true, 
          "serverSide": true, 
          "order": [], //Initial no order.
          // Load data for the table's content from an Ajax source
          "ajax": {
              "url": "&lt;?php echo base_url('posts/posts_ajax/'.$category)?&gt;",
              "dataType": "json",
              "type": "POST",
              "data":{  '&lt;?php echo $this-&gt;security-&gt;get_csrf_token_name(); ?&gt;' : '&lt;?php echo $this-&gt;security-&gt;get_csrf_hash(); ?&gt;' }
          },
          //Set column definition initialisation properties.
          "columnDefs": [
          { 
              "targets": [ 0 ], //first column / numbering column
              "orderable": false, //set not orderable
          },
          ],
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>So far, everything works as expected, but it does not entirely suit my needs. Look at these lines: </p>
  
  <pre><code>foreach ($list as $post) {
          $row = array();
          $row[] = $post-&gt;title;
          $row[] = $post-&gt;body;
          $row[] = $post-&gt;category;
          $row[] = $post-&gt;poster;
          $row[] = $post-&gt;time;
          $data[] = $row; 
  }   
  </code></pre>
  
  <p>I'd like the results of each row to display in ONE COLUMN, not in 5 colums. This is because I intend to use Bootstrap panel to display the result of each row, and customise it to suit my needs. 
  I want something like this for each result:</p>
  
  <p>Post Title: bla bla bla <br/>
  The body goes here <br/>
  Posted in: category name <br/>
  Posted by: name of poster <br/>
  Time: 9th Sep, 2017 at 10: 30PM </p>
  
  <p>I would like to control how each field is displayed, with stylings and formatings, such as converting the time field into something more human-readable (eg 9th Sep, 2017 at 10: 30PM). The problem is, since the loop is created inside the controller (rather than in view, which I'm used to), I do not know how to go about doing this. I know if I can get the loop between the table body tags ( here ), I can do what I want, but I don't think AJAX will appreciate it if I did (I tried it, 'he' didn't). This is my first dabble into AJAX.</p>
  
  <p>EDIT: I am wondering if there is a way to use the contents of the post_ajax function inside view so that I can put the foreach loop inside the body tag of table. 
  So I need help. Please help! Sorry it's so long...</p>