EllisLab text mark
Advanced Search
1 of 29
1
   
Ignited DataTables
Posted: 14 September 2010 12:27 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
90 posts

I’m not really sure what reference you are talking about but I did update the model regarding manipulation of data results and adding of other columns. I hope that can help you and others as well..

 Signature 

Ignited Datatables

 
Posted: 14 September 2010 02:31 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-06-29
7 posts

The reference that i was talking is about how use a server side php for codeigniter using querys instead of tables. I modified a code that i founded and now i have codeigniter working with server side tables and querys.

 
Posted: 15 September 2010 07:23 PM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2009-09-04
98 posts

If you get stuck with json_encode() returning null values, due to non-utf characters, then simply change this row.

FROM:

$aaData[$row_key][] = $col_val;


TO:


$aaData[$row_key][] = utf8_encode($col_val);

 
Posted: 15 September 2010 07:42 PM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2009-09-04
98 posts

If you get stuck with json_encode() returning null values, due to non-utf characters, then simply change this row.

FROM:

$aaData[$row_key][] = $col_val;


TO:


$aaData[$row_key][] = utf8_encode($col_val);

 
Posted: 01 October 2010 01:39 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2010-05-07
1 posts

First of all great code! Saved me a lot of time.

Found a small bug…

In the get_ordering() function I changed:

if ($this->input->post(“iSortCol_0”)) {

TO

if ($this->input->post(“iSortCol_0”) != NULL) {

If the integer value of iSortCol_0 is 0 this line will evaluate FALSE and therefore the first column will not sort properly.

 
Posted: 04 October 2010 05:47 AM   [ Ignore ]   [ # 16 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
90 posts

thank you for your feedback. changed the code already. btw, i’m planning to make this into a library when i do get the time off and add join functions as well. i kind of eventually found out it lacked this feature as the need for it arose in my previous project.

 Signature 

Ignited Datatables

 
Posted: 04 October 2010 05:57 AM   [ Ignore ]   [ # 17 ]   [ Rating: 0 ]
Joined: 2009-09-04
98 posts

Thanks for all the work you put in so far. To make this a Library is an excellent idea.

Can I please request a feature? The possibility of adding a custom filter.

So the final query would be like this:

WHERE (<INSERT AJAX DATATABLE FILTERS HERE>) AND (<INSERT CUSTOM FILTER HERE>)

I made the changes to the code already. All additions are in bold.

public function generate($table, $columns, $index, $cfilter)
  {     
    $sLimit = $this->get_paging();
    $sOrder = $this->get_ordering($columns);
    $sWhere = $this->get_filtering($columns);
    $sCustomWhere = $this->get_customfiltering($sWhere, $cfilter);
    $rResult = $this->get_display_data($table, $columns, $sWhere, $sCustomWhere, $sOrder, $sLimit);
    $rResultFilterTotal = $this->get_data_set_length();
    $aResultFilterTotal = $rResultFilterTotal->result_array();
    $iFilteredTotal = $aResultFilterTotal[0][“FOUND_ROWS()”];
    $rResultTotal = $this->get_total_data_set_length($table, $index, $sWhere);
    $aResultTotal = $rResultTotal->result_array();
    $iTotal = $aResultTotal[0][“COUNT($index)”];
    return $this->produce_output($columns, $iTotal, $iFilteredTotal, $rResult);
  }

protected function get_customfiltering($sWhere, $cfilter){
      if($cfilter==”“){return “”;}
      if($sWhere==”“){$sCustomWhere = “WHERE (”;}
      else{$sCustomWhere =” AND (”;}
      $sCustomWhere.=$cfilter.”) “;
      return $sCustomWhere;
  }

protected function get_display_data($table, $columns, $sWhere, $sCustomWhere, $sOrder, $sLimit)
  {
    $sql = “SELECT SQL_CALC_FOUND_ROWS ” . implode(”, “, $columns) . “
      FROM $table
      $sWhere
      $sCustomWhere
      $sOrder
      $sLimit
    “;
    return $this->db->query($sql);
  }

 
Posted: 05 October 2010 05:14 AM   [ Ignore ]   [ # 18 ]   [ Rating: 0 ]
Joined: 2010-06-29
7 posts

hi emorling,

    the query that you ask for i resolved it. I hope this code that i attached could solve your doubt.

<?php
  
class Datatables_model_query extends Model
  {
    
public function __construct()
    
{
      parent
::__construct();
    
}

    
public function generate($table$columns$index$joins$where$search$groupby)
    
{

      $sLimit 
$this->get_paging();
      
$sOrder $this->get_ordering($columns);
      
$sWhere $this->get_filtering($columns,$where,$search);
      
$rResult $this->get_display_data($table$columns$sWhere$sOrder$sLimit$joins$groupby);
      
$rResultFilterTotal $this->get_data_set_length();
      
$aResultFilterTotal $rResultFilterTotal->result_array();
      
$iFilteredTotal $aResultFilterTotal[0]["FOUND_ROWS()"];
      
$rResultTotal $this->get_total_data_set_length($table$index$sWhere$joins$where$groupby$columns);
      if (
$groupby=="")
      
{
         $aResultTotal 
$rResultTotal->result_array();
         
$iTotal $aResultTotal[0]["COUNT($index)"];
      
}
      
else {
             $iTotal 
strval($rResultTotal);
           
}
      $DatoSalida 
$this->produce_output($columns$iTotal$iFilteredTotal$rResult);
      return 
$DatoSalida;
    
}

    
protected function get_paging()
    
{
      $sLimit 
"";

      if(
$this->input->post("iDisplayStart") && $this->input->post("iDisplayLength") != "-1")
        
$sLimit "LIMIT " $this->input->post("iDisplayStart") . ", " $this->input->post("iDisplayLength");
      else
      
{
        $sLimit 
"LIMIT " "0" ", " $this->input->post("iDisplayLength");
      
}

      
return $sLimit;
    
}

    
protected function get_ordering($columns)
    
{
      $sOrder 
"";

      if(
$this->input->post("iSortCol_0"))
      
{
        $sOrder 
"ORDER BY ";

        for(
$i 0$i intval($this->input->post("iSortingCols")); $i++)
          
$sOrder .= $columns[intval($this->input->post("iSortCol_" $i))" " $this->input->post("sSortDir_" $i) . ", ";

        
$sOrder substr_replace($sOrder"", -2);
      
}

      
return $sOrder;
    
}

    
protected function get_filtering($columns$where$search)
    
{
      $sWhere
="";
      
$TieneParentesis=0;
      if (
$where!="")
      
{
         $sWhere 
"WHERE ".$where;
      
}
      

      
if($this->input->post("sSearch") != "")
      
{

        
if ($sWhere!="")
        
{
            $sWhere
.=" AND (";
            
$TieneParentesis=1;
        
}

        
for($i 0$i count($columns); $i++)
          
$sWhere .= $columns[$i] " LIKE '%" $this->input->post("sSearch") . "%' OR ";

        
$sWhere substr_replace($sWhere"", -3);
        if (
$TieneParentesis==1)
            
$sWhere.=")";
      
}

      
return $sWhere;
    
}

    
protected function get_display_data($table$columns$sWhere$sOrder$sLimit$joins$groupby)
    
{
      $Consulta 
" SELECT SQL_CALC_FOUND_ROWS " implode(", "$columns) . " FROM $table $joins $sWhere $groupby $sOrder $sLimit ";
      
$DatoSalida $this->db->query($Consulta);
      return 
$DatoSalida;
    
}

    
protected function get_data_set_length()
    
{
      $DatoSalida 
$this->db->query("SELECT FOUND_ROWS()");
      return 
$DatoSalida;
    
}

    
protected function get_total_data_set_length($table$index$sWhere,$joins$where$groupby$columns)
    
{

      
if ($groupby=="")
      
{
          $Consulta 
"SELECT COUNT(" $index ") FROM $table $joins $sWhere ";
          
$DatoSalida $this->db->query($Consulta);
      
}
      
else
      
{
          $ConsultaSql 
"SELECT " implode(", "$columns) . " FROM $table $joins $sWhere $groupby ";
          
$Consulta $this->db->query($ConsultaSql);
          
$DatoSalida $Consulta->num_rows();
      
}
      
return $DatoSalida;
    
}

    
protected function produce_output($columns$iTotal$iFilteredTotal$rResult)
    
{
      $aaData 
= array();

      foreach(
$rResult->result_array() as $row_key => $row_val)
      
{
        
foreach($row_val as $col_key => $col_val)
        
{
          
if($row_val[$col_key] == "version")
            
$aaData[$row_key][$col_key] = ($aaData[$row_key][$col_key] == 0)? "-" $col_val;
          else
          
{
            
switch($row_val[$col_key])
            
{
              
default: $aaData[$row_key][] $col_val; break;
            
}
          }
        }
      }

      $sOutput 
= array
      (
        
"sEcho"                => intval($this->input->post("sEcho")),
        
"iTotalRecords"        => $iTotal,
        
"iTotalDisplayRecords" => $iFilteredTotal,
        
"aaData"               => $aaData
      
);

      return 
json_encode($sOutput);
    
}
  }
?> 
 
Posted: 05 October 2010 05:21 AM   [ Ignore ]   [ # 19 ]   [ Rating: 0 ]
Joined: 2009-09-04
98 posts

Thanks! You are the best

 
Posted: 05 October 2010 05:54 AM   [ Ignore ]   [ # 20 ]   [ Rating: 0 ]
Joined: 2010-10-05
2 posts

Great post and contributions.

I am at the point where have to build lots of reports and this looks like the best and easy way to accomplish it. Just found it today and I’m so happy. So I can’t wait to see the joins development.

Thanks, nick

 
Posted: 05 October 2010 06:23 AM   [ Ignore ]   [ # 21 ]   [ Rating: 0 ]
Joined: 2010-10-05
2 posts

Hi ruizpi,

There is a small mistake on get_filtering. The search is not working. Here is the working updated version.

protected function get_filtering($columns$where$search)
    
{
      $sWhere
="";
      
$TieneParentesis=0;
      if (
$where!="")
      
{
         $sWhere 
.=$where;
      
}


      
if($this->input->post("sSearch") != "")
      
{

        
if ($sWhere!="")
        
{
            $sWhere
.=" AND (";
            
$TieneParentesis=1;
        
}

        
for($i 0$i count($columns); $i++)
          
$sWhere .= $columns[$i] " LIKE '%" $this->input->post("sSearch") . "%' OR ";

        
$sWhere substr_replace($sWhere"", -3);
        if (
$TieneParentesis==1)
            
$sWhere.=")";
      
}

      
if ($sWhere <>"")
          
$sWhere "WHERE " $sWhere;
      return 
$sWhere;
    
 
Posted: 05 October 2010 07:51 AM   [ Ignore ]   [ # 22 ]   [ Rating: 0 ]
Joined: 2010-06-29
7 posts

hi nicusors,

  I try to test your modification but when i use a group by select it doesn’t work. Test it.

 
Posted: 05 October 2010 08:22 AM   [ Ignore ]   [ # 23 ]   [ Rating: 0 ]
Joined: 2010-06-29
7 posts

At this point my code that i share with you works fine with querys. I try to filter for 1 column like some examples that create some boxes at the bottom of the table. It doesn’t work with server side processing like my code do. Can anybody help me?

 
Posted: 12 October 2010 05:58 AM   [ Ignore ]   [ # 24 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
90 posts

hey guys, i updated the first post and added an initial draft for the “library” version… i haven’t tested it out yet so i was hoping some kind soul can do the testing for me raspberry i haven’t added joins on this one yet as im still pretty much preoccupied… i did add a zip download too if you want…

do send me feedbacks on this…

cheers

 Signature 

Ignited Datatables

 
Posted: 23 October 2010 12:39 AM   [ Ignore ]   [ # 25 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-06
511 posts

I used your code and with a regular request the json file seems fine however when using a post request the following error is generated

An Error Was Encountered.
The action you have requested is not allowed

It’s my bedtime now. I’ll dive into this tomorrow or maybe someone has a solution for me when I wake up :D

 Signature 

In need for a good host for your Codeigniter Apps? Try Webfaction! It’s fast, reliable, technically sound, in the USA, EU and Asia.

 
1 of 29
1