Advertisement
Guest User

Datatable CI Controller

a guest
Apr 4th, 2010
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.54 KB | None | 0 0
  1. <?php
  2.  
  3. class Datatable extends Controller {
  4.  
  5.     function Datatable()
  6.     {
  7.         parent::Controller();  
  8.     }
  9.    
  10.     function index()
  11.     {
  12.         //Check if $Post data is sent
  13.         if($_POST)
  14.         {
  15.             ////////////////////////////////////////////////////////////
  16.             ///////////////START//OF//VARIABLES//CREATION///////////////
  17.             ////////////////////////////////////////////////////////////
  18.            
  19.             //Create the variable $object_name with the first letter captalized
  20.             $object_name = ucwords($this->input->post('object'));
  21.             //$this->firephp->fb($object_name, 'Object to be listed');
  22.            
  23.             //Create the arraay with the columns name based on the Post value
  24.             $columns = explode(',', $this->input->post('sColumns'));
  25.             //$this->firephp->fb($columns, 'Table columns');
  26.            
  27.             ////////////////////////////////////////////////////////////
  28.             /////////////////END//OF//VARIABLES//CREATION///////////////
  29.             ////////////////////////////////////////////////////////////
  30.            
  31.             ////////////////////////////////////////////////////////////
  32.             //////////////START//OF//OBJECT//QUERY//CREATION////////////
  33.             ////////////////////////////////////////////////////////////
  34.            
  35.             //Start the object to be listed in the table
  36.             $obj = new $object_name();     
  37.            
  38.             //Create the variable $n_columns with the number of columns in the table
  39.             $n_columns = count($columns);
  40.             //$this->firephp->fb($n_columns, 'Number of Columns in the table');    
  41.            
  42.             //Check if Search data is sent
  43.             if($this->input->post('sSearch'))
  44.             {              
  45.                 //Initiate a new SQL Query group
  46.                 $obj->group_start();
  47.                
  48.                 //Create a loop to go over each column
  49.                 foreach($columns as $position => $column)
  50.                 {
  51.                     //Add a the SQL statment for searching in that column to the Query Group
  52.                     $obj->or_like($column, $this->input->post('sSearch'));
  53.                 }
  54.                
  55.                 //Close the SQL Query Group
  56.                 $obj->group_end();
  57.             }
  58.            
  59.             //Check if sorting data is sent
  60.             if($this->input->post('iSortingCols'))
  61.             {
  62.                 //Create the variable $iSortingCols with the number of columns being sorted
  63.                 $iSortingCols = $this->input->post('iSortingCols');        
  64.                 //$this->firephp->fb($iSortingCols, 'Number of columns being sorted');         
  65.    
  66.                 //Create a loop to go over each column and identify witch columns to sort
  67.                 foreach($columns as $position => $column)
  68.                 {
  69.                     for($i = 0; $i < $iSortingCols; $i++)
  70.                     {
  71.                         if($this->input->post('iSortCol_'.$i) == $position)
  72.                         {
  73.                             //$this->firephp->fb($column, 'Column to be sort #'.$i);   
  74.                             $obj->order_by($column, $this->input->post('sSortDir_'.$i));
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.            
  80.             //Check if the table is paged
  81.             if($this->input->post('iDisplayLength') == -1)
  82.             {
  83.                 //Create the variable to say that the table is not paged
  84.                 $is_paged = FALSE;
  85.                
  86.                 //Execute the query
  87.                 $obj->get();
  88.             }
  89.             else
  90.             {
  91.                 //Create the variable to say that the table is paged
  92.                 $is_paged = TRUE;
  93.                
  94.                 //Datatables send the registry start number while Datamapper requires the page number.
  95.                 //To calculate the page number dividing the Initial Registry by the (iDisplayStart) + the Size of the page (iDisplayLength) by the saze of the page (iDisplayLength)
  96.                
  97.                 $page_number = ($this->input->post('iDisplayStart') + $this->input->post('iDisplayLength'))/($this->input->post('iDisplayLength'));
  98.                 //$this->firephp->fb($page_number, 'Number of displaying page');           
  99.                
  100.                 //Execute the paged query
  101.                 $obj->get_paged($page_number, $this->input->post('iDisplayLength'));
  102.             }
  103.             ////////////////////////////////////////////////////////////
  104.             ///////////////END//OF//OBJECT//QUERY//CREATION/////////////
  105.             ////////////////////////////////////////////////////////////
  106.            
  107.             ////////////////////////////////////////////////////////////
  108.             ///////////////START//OF//JSON//OUTPUT//CREATION////////////
  109.             ////////////////////////////////////////////////////////////
  110.            
  111.             //
  112.             //Required variables in JSON output
  113.             //
  114.            
  115.             //sEcho - Security variable
  116.             $sEcho = $this->input->post('sEcho');
  117.             //$this->firephp->fb($sEcho, 'json: sEcho');
  118.            
  119.             //iTotalRecords - Variable with the number of records being displayed
  120.             $iTotalRecords = $obj->result_count();
  121.             //$this->firephp->fb($iTotalRecords, 'json: iTotalRecords');
  122.            
  123.             //If the table is paged
  124.             if($is_paged)
  125.             {
  126.                 //iTotalDisplayRecords - Variable with the total number of records in that table
  127.                 $iTotalDisplayRecords = $obj->paged->total_rows;
  128.                 //$this->firephp->fb($iTotalDisplayRecords, 'json: iTotalDisplayRecords');
  129.             }
  130.             else //If the table is not paged
  131.             {
  132.                 //iTotalDisplayRecords - Variable with the total number of records in that table
  133.                 $iTotalDisplayRecords = $obj->result_count();
  134.                 //$this->firephp->fb($iTotalDisplayRecords, 'json: iTotalDisplayRecords');
  135.             }
  136.            
  137.             //Check if the object exists
  138.             if($obj->exists())
  139.             {
  140.                 //Create the variable $aaData with JSON output
  141.                 $aaData  = '[';
  142.                
  143.                 //Create a loop to go over each object
  144.                 foreach($obj->all as $s)
  145.                 {
  146.                     //Open the JSON for that row
  147.                     $aaData .='[';
  148.                    
  149.                     //Create a loop to go over each column and add its value to that row
  150.                     foreach($columns as $position => $column)
  151.                     {
  152.                         $aaData .='"'.$s->$column.'",';
  153.                     }              
  154.                        
  155.                     //Remove the comman from the last column
  156.                     $aaData = $this->removeCharFinal($aaData);
  157.                    
  158.                     //Close JSON for that row
  159.                     $aaData .='],';
  160.                 }
  161.                
  162.                 //Remove the comman from the last row
  163.                 $aaData = $this->removeCharFinal($aaData);
  164.                
  165.                 //Close the JSON
  166.                 $aaData .=']';
  167.             }
  168.             else//If the object doesnt exist
  169.             {
  170.                 //Create a empty data
  171.                 $aaData = '[]';
  172.             }
  173.            
  174.             //Create the json output based on Datatables documentation pattern
  175.             $json = '{"sEcho": '.$sEcho.', "iTotalRecords": '.$iTotalRecords.', "iTotalDisplayRecords": '.$iTotalDisplayRecords.', "aaData": '.$aaData.'}';
  176.             //$this->firephp->fb($json, 'Resposta JSON');
  177.             //The pattern for this can be found here: http://www.datatables.net/usage/server-side
  178.            
  179.             ////////////////////////////////////////////////////////////
  180.             ////////////////END//OF//JSON//OUTPUT//CREATION/////////////
  181.             ////////////////////////////////////////////////////////////
  182.  
  183.  
  184.             //Check if there is a $sEcho value
  185.             if($this->input->post('sEcho'))
  186.             {
  187.                 //Write the json RESULT
  188.                 echo $json;
  189.             }
  190.         }
  191.     }
  192.    
  193.     function removeCharFinal($input, $q = 1)
  194.     {
  195.         return substr($input, 0, -$q); 
  196.     }
  197. }
  198.  
  199. /* End of file datatable.php */
  200. /* Location: ./system/application/controllers/datatable.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement