Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Datatable extends Controller {
- function Datatable()
- {
- parent::Controller();
- }
- function index()
- {
- //Check if $Post data is sent
- if($_POST)
- {
- ////////////////////////////////////////////////////////////
- ///////////////START//OF//VARIABLES//CREATION///////////////
- ////////////////////////////////////////////////////////////
- //Create the variable $object_name with the first letter captalized
- $object_name = ucwords($this->input->post('object'));
- //$this->firephp->fb($object_name, 'Object to be listed');
- //Create the arraay with the columns name based on the Post value
- $columns = explode(',', $this->input->post('sColumns'));
- //$this->firephp->fb($columns, 'Table columns');
- ////////////////////////////////////////////////////////////
- /////////////////END//OF//VARIABLES//CREATION///////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- //////////////START//OF//OBJECT//QUERY//CREATION////////////
- ////////////////////////////////////////////////////////////
- //Start the object to be listed in the table
- $obj = new $object_name();
- //Create the variable $n_columns with the number of columns in the table
- $n_columns = count($columns);
- //$this->firephp->fb($n_columns, 'Number of Columns in the table');
- //Check if Search data is sent
- if($this->input->post('sSearch'))
- {
- //Initiate a new SQL Query group
- $obj->group_start();
- //Create a loop to go over each column
- foreach($columns as $position => $column)
- {
- //Add a the SQL statment for searching in that column to the Query Group
- $obj->or_like($column, $this->input->post('sSearch'));
- }
- //Close the SQL Query Group
- $obj->group_end();
- }
- //Check if sorting data is sent
- if($this->input->post('iSortingCols'))
- {
- //Create the variable $iSortingCols with the number of columns being sorted
- $iSortingCols = $this->input->post('iSortingCols');
- //$this->firephp->fb($iSortingCols, 'Number of columns being sorted');
- //Create a loop to go over each column and identify witch columns to sort
- foreach($columns as $position => $column)
- {
- for($i = 0; $i < $iSortingCols; $i++)
- {
- if($this->input->post('iSortCol_'.$i) == $position)
- {
- //$this->firephp->fb($column, 'Column to be sort #'.$i);
- $obj->order_by($column, $this->input->post('sSortDir_'.$i));
- }
- }
- }
- }
- //Check if the table is paged
- if($this->input->post('iDisplayLength') == -1)
- {
- //Create the variable to say that the table is not paged
- $is_paged = FALSE;
- //Execute the query
- $obj->get();
- }
- else
- {
- //Create the variable to say that the table is paged
- $is_paged = TRUE;
- //Datatables send the registry start number while Datamapper requires the page number.
- //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)
- $page_number = ($this->input->post('iDisplayStart') + $this->input->post('iDisplayLength'))/($this->input->post('iDisplayLength'));
- //$this->firephp->fb($page_number, 'Number of displaying page');
- //Execute the paged query
- $obj->get_paged($page_number, $this->input->post('iDisplayLength'));
- }
- ////////////////////////////////////////////////////////////
- ///////////////END//OF//OBJECT//QUERY//CREATION/////////////
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- ///////////////START//OF//JSON//OUTPUT//CREATION////////////
- ////////////////////////////////////////////////////////////
- //
- //Required variables in JSON output
- //
- //sEcho - Security variable
- $sEcho = $this->input->post('sEcho');
- //$this->firephp->fb($sEcho, 'json: sEcho');
- //iTotalRecords - Variable with the number of records being displayed
- $iTotalRecords = $obj->result_count();
- //$this->firephp->fb($iTotalRecords, 'json: iTotalRecords');
- //If the table is paged
- if($is_paged)
- {
- //iTotalDisplayRecords - Variable with the total number of records in that table
- $iTotalDisplayRecords = $obj->paged->total_rows;
- //$this->firephp->fb($iTotalDisplayRecords, 'json: iTotalDisplayRecords');
- }
- else //If the table is not paged
- {
- //iTotalDisplayRecords - Variable with the total number of records in that table
- $iTotalDisplayRecords = $obj->result_count();
- //$this->firephp->fb($iTotalDisplayRecords, 'json: iTotalDisplayRecords');
- }
- //Check if the object exists
- if($obj->exists())
- {
- //Create the variable $aaData with JSON output
- $aaData = '[';
- //Create a loop to go over each object
- foreach($obj->all as $s)
- {
- //Open the JSON for that row
- $aaData .='[';
- //Create a loop to go over each column and add its value to that row
- foreach($columns as $position => $column)
- {
- $aaData .='"'.$s->$column.'",';
- }
- //Remove the comman from the last column
- $aaData = $this->removeCharFinal($aaData);
- //Close JSON for that row
- $aaData .='],';
- }
- //Remove the comman from the last row
- $aaData = $this->removeCharFinal($aaData);
- //Close the JSON
- $aaData .=']';
- }
- else//If the object doesnt exist
- {
- //Create a empty data
- $aaData = '[]';
- }
- //Create the json output based on Datatables documentation pattern
- $json = '{"sEcho": '.$sEcho.', "iTotalRecords": '.$iTotalRecords.', "iTotalDisplayRecords": '.$iTotalDisplayRecords.', "aaData": '.$aaData.'}';
- //$this->firephp->fb($json, 'Resposta JSON');
- //The pattern for this can be found here: http://www.datatables.net/usage/server-side
- ////////////////////////////////////////////////////////////
- ////////////////END//OF//JSON//OUTPUT//CREATION/////////////
- ////////////////////////////////////////////////////////////
- //Check if there is a $sEcho value
- if($this->input->post('sEcho'))
- {
- //Write the json RESULT
- echo $json;
- }
- }
- }
- function removeCharFinal($input, $q = 1)
- {
- return substr($input, 0, -$q);
- }
- }
- /* End of file datatable.php */
- /* Location: ./system/application/controllers/datatable.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement