Server Side processing with Datatables v1.10.0 - php

Hi I'm having some issues getting the server side processing functionality of data tables to work with SQL Server.
I've got a test page that should display two columns from a database table(for now).
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="Stylesheet" type="text/css" href="DataTables-1.10.0/media/css/jquery.dataTables.min.css" />
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th align="center">PK</th>
<th align="center">Network</th>
</tr>
</thead>
<tfoot>
<tr>
<th align="center">PK</th>
<th align="center">Network</th>
</tr>
</tfoot>
</table>
</body>
<script type="text/javascript" src="DataTables-1.10.0/media/js/jquery.js"></script>
<script type="text/javascript" src="DataTables-1.10.0/media/js/jquery.dataTables.min.js">
</script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$('#example').dataTable({
"processing": true,
"bServerSide": true,
"ajax": "PHP/testGetArchive.php"
});
});
</script>
</html>
I'm using the example code found on the website Here for the server side functions:
http://next.datatables.net/examples/server_side/simple.html
This is my version of the php page being called:
<?php
/*
* DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simply to show how
* server-side processing can be implemented, and probably shouldn't be used as
* the basis for a large complex system. It is suitable for simple use cases as
* for learning.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* #license MIT - http://datatables.net/license_mit
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
// DB table to use
$table = 'tblViews';
// Table's primary key
$primaryKey = 'PK';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'PK', 'dt' => 0 ),
array( 'db' => 'Network', 'dt' => 1 )
);
// SQL server connection information
$sql_details = array(
'user' => '******',
'pass' => '******',
'db' => '******db',
'host' => '******\SQLEXPRESS'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
This then calls the second example PHP found here:
https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php
Here is my Copy of it. The only Modification I performed was to remove the block of code that is required for the examples.
<?php
/*
* Helper functions for building a DataTables server-side processing SQL query
*
* The static functions in this class are just helper functions to help build
* the SQL used in the DataTables demo server-side processing scripts. These
* functions obviously do not represent all that can be done with server-side
* processing, they are intentionally simple to show how it works. More complex
* server-side processing operations will likely require a custom script.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* #license MIT - http://datatables.net/license_mit
*/
class SSP {
/**
* Create the data output array for the DataTables rows
*
* #param array $columns Column information array
* #param array $data Data from the SQL get
* #return array Formatted data in a row based format
*/
static function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
else {
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
}
$out[] = $row;
}
return $out;
}
/**
* Paging
*
* Construct the LIMIT clause for server-side processing SQL query
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #return string SQL limit clause
*/
static function limit ( $request, $columns )
{
$limit = '';
if ( isset($request['start']) && $request['length'] != -1 ) {
$limit = "LIMIT ".intval($request['start']).", ".intval($request['length']);
}
return $limit;
}
/**
* Ordering
*
* Construct the ORDER BY clause for server-side processing SQL query
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #return string SQL order by clause
*/
static function order ( $request, $columns )
{
$order = '';
if ( isset($request['order']) && count($request['order']) ) {
$orderBy = array();
$dtColumns = SSP::pluck( $columns, 'dt' );
for ( $i=0, $ien=count($request['order']) ; $i<$ien ; $i++ ) {
// Convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.$column['db'].'` '.$dir;
}
}
$order = 'ORDER BY '.implode(', ', $orderBy);
}
return $order;
}
/**
* Searching / Filtering
*
* Construct the WHERE clause for server-side processing SQL query.
*
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here performance on large
* databases would be very poor
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #param array $bindings Array of values for PDO bindings, used in the
* sql_exec() function
* #return string SQL where clause
*/
static function filter ( $request, $columns, &$bindings )
{
$globalSearch = array();
$columnSearch = array();
$dtColumns = SSP::pluck( $columns, 'dt' );
if ( isset($request['search']) && $request['search']['value'] != '' ) {
$str = $request['search']['value'];
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['searchable'] == 'true' ) {
$binding = SSP::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$globalSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
}
// Individual column filtering
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
$str = $requestColumn['search']['value'];
if ( $requestColumn['searchable'] == 'true' &&
$str != '' ) {
$binding = SSP::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$columnSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
// Combine the filters into a single string
$where = '';
if ( count( $globalSearch ) ) {
$where = '('.implode(' OR ', $globalSearch).')';
}
if ( count( $columnSearch ) ) {
$where = $where === '' ?
implode(' AND ', $columnSearch) :
$where .' AND '. implode(' AND ', $columnSearch);
}
if ( $where !== '' ) {
$where = 'WHERE '.$where;
}
return $where;
}
/**
* Perform the SQL queries needed for an server-side processing requested,
* utilising the helper functions of this class, limit(), order() and
* filter() among others. The returned array is ready to be encoded as JSON
* in response to an SSP request, or can be modified if needed before
* sending back to the client.
*
* #param array $request Data sent to server by DataTables
* #param array $sql_details SQL connection details - see sql_connect()
* #param string $table SQL table to query
* #param string $primaryKey Primary key of the table
* #param array $columns Column information array
* #return array Server-side processing response array
*/
static function simple ( $request, $sql_details, $table, $primaryKey, $columns )
{
$bindings = array();
$db = SSP::sql_connect( $sql_details );
// Build the SQL query string from the request
$limit = SSP::limit( $request, $columns );
$order = SSP::order( $request, $columns );
$where = SSP::filter( $request, $columns, $bindings );
// Main query to actually get the data
$data = SSP::sql_exec( $db, $bindings,
"SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", SSP::pluck($columns, 'db'))."`
FROM `$table`
$where
$order
$limit"
);
// Data set length after filtering
$resFilterLength = SSP::sql_exec( $db,
"SELECT FOUND_ROWS()"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = SSP::sql_exec( $db,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table`"
);
$recordsTotal = $resTotalLength[0][0];
/*
* Output
*/
return array(
"draw" => intval( $request['draw'] ),
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => SSP::data_output( $columns, $data )
);
}
/**
* Connect to the database
*
* #param array $sql_details SQL server connection details array, with the
* properties:
* * host - host name
* * db - database name
* * user - user name
* * pass - user password
* #return resource Database connection handle
*/
static function sql_connect ( $sql_details )
{
try {
$db = #new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
$sql_details['user'],
$sql_details['pass'],
array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
);
}
catch (PDOException $e) {
SSP::fatal(
"An error occurred while connecting to the database. ".
"The error reported by the server was: ".$e->getMessage()
);
}
return $db;
}
/**
* Execute an SQL query on the database
*
* #param resource $db Database handler
* #param array $bindings Array of PDO binding values from bind() to be
* used for safely escaping strings. Note that this can be given as the
* SQL query string if no bindings are required.
* #param string $sql SQL query to execute.
* #return array Result from the query (all rows)
*/
static function sql_exec ( $db, $bindings, $sql=null )
{
// Argument shifting
if ( $sql === null ) {
$sql = $bindings;
}
$stmt = $db->prepare( $sql );
//echo $sql;
// Bind parameters
if ( is_array( $bindings ) ) {
for ( $i=0, $ien=count($bindings) ; $i<$ien ; $i++ ) {
$binding = $bindings[$i];
$stmt->bindValue( $binding['key'], $binding['val'], $binding['type'] );
}
}
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
SSP::fatal( "An SQL error occurred: ".$e->getMessage() );
}
// Return all
return $stmt->fetchAll();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Internal methods
*/
/**
* Throw a fatal error.
*
* This writes out an error message in a JSON string which DataTables will
* see and show to the user in the browser.
*
* #param string $msg Message to send to the client
*/
static function fatal ( $msg )
{
echo json_encode( array(
"error" => $msg
) );
exit(0);
}
/**
* Create a PDO binding key which can be used for escaping variables safely
* when executing a query with sql_exec()
*
* #param array &$a Array of bindings
* #param * $val Value to bind
* #param int $type PDO field type
* #return string Bound key to be used in the SQL where this parameter
* would be used.
*/
static function bind ( &$a, $val, $type )
{
$key = ':binding_'.count( $a );
$a[] = array(
'key' => $key,
'val' => $val,
'type' => $type
);
return $key;
}
/**
* Pull a particular property from each assoc. array in a numeric array,
* returning and array of the property values from each item.
*
* #param array $a Array to get data from
* #param string $prop Property to read
* #return array Array of property values
*/
static function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ ) {
$out[] = $a[$i][$prop];
}
return $out;
}
}
I keep getting an error saying that the code cannot find the driver though I've got the sqlserv and pdo_sqlsrv Drivers installed din my php environment. Is there something wrong on the code that's causing this error? Are my drivers incorrect? Any help with this would be appreciated. I've got upwards of 65,000 rows of data to process and to send that all to the client in one go will be impossible.

It took me a while But I've found out where I was going wrong and I now have DataTables working with SQL Server through server side scripts. I'm posting this solution in the hope that it will help anyone else like me who was having issues. I've broken my answer into parts.
The PHP Environment
The SQLSRV drivers for php can be found here. Download the SQLSRV30.EXE installer package. You may find that when you try and run this executable that you get the error "This is not a valid win32 application" If this is the case unzip the executable with something like 7-zip. The resulting file will have the files you require inside.
When you've unzipped the package you need to select the right driver. Most windows installations use the non thread safe drivers these are:
php version 5.3:
php_sqlsrv_53_nts.dll
php_pdo_sqlsrv_53_nts.dll
php version 5.4:
php_sqlsrv_54_nts.dll
php_pdo_sqlsrv_54_nts.dll
Copy the appropriate files to the ext folder within your php directory. Now modify your php.ini file to have a reference to these files. Do this by adding an entry under the dynamic extensions section. The result would be something like this:
extension=php_sqlsrv_54_nts.dll
And then add a section for the driver under the Module section settings like this:
[sqlsrv]
sqlsrv.LogSubSystems=-1
sqlsrv.LogSeverity=-1
sqlsrv.WarningsReturnAsErrors=0
The documentation for these settings can be found here.
Once you have added these drivers and added a reference to them in the php.ini file you must also ensure that the Microsoft SQL Server Client Profile 2012 is also installed.
These Links have been taken from the PHP.net website:
Microsoft SQL Server Client Profile 2012 x86
Microsoft SQL Server Client profile 2012 x64
Once you have performed these steps restart your web server. The driver should now be installed and ready to use. You can check this using your info.php page.
The Server Side Script:
Now that the web-server has been configured to use the SQL SRV driver we can now use it to query a SQL Server database. I've used the server side script available here. Here are some issues I found with it:
<?php
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "";
/* DB table to use */
$sTable = "";
/* Database connection information */
$gaSql['user'] = "";
$gaSql['password'] = "";
$gaSql['db'] = "";
$gaSql['server'] = "";
/*
* Columns
* If you don't want all of the columns displayed you need to hardcode $aColumns array with your elements.
* If not this will grab all the columns associated with $sTable
*/
$aColumns = array();
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* ODBC connection
*/
$connectionInfo = array("UID" => $gaSql['user'], "PWD" => $gaSql['password'], "Database"=>$gaSql['db'],"ReturnDatesAsStrings"=>true);
$gaSql['link'] = sqlsrv_connect( $gaSql['server'], $connectionInfo);
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
/* Ordering */
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) ) {
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ ) {
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" ) {
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".addslashes( $_GET['sSortDir_'.$i] ) .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" ) {
$sOrder = "";
}
}
/* Filtering */
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" ) {
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
$sWhere .= $aColumns[$i]." LIKE '%".addslashes( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' ) {
if ( $sWhere == "" ) {
$sWhere = "WHERE ";
} else {
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." LIKE '%".addslashes($_GET['sSearch_'.$i])."%' ";
}
}
/* Paging */
$top = (isset($_GET['iDisplayStart']))?((int)$_GET['iDisplayStart']):0 ;
$limit = (isset($_GET['iDisplayLength']))?((int)$_GET['iDisplayLength'] ):10;
$sQuery = "SELECT TOP $limit ".implode(",",$aColumns)."
FROM $sTable
$sWhere ".(($sWhere=="")?" WHERE ":" AND ")." $sIndexColumn NOT IN
(
SELECT $sIndexColumn FROM
(
SELECT TOP $top ".implode(",",$aColumns)."
FROM $sTable
$sWhere
$sOrder
)
as [virtTable]
)
$sOrder";
$rResult = sqlsrv_query($gaSql['link'],$sQuery) or die("$sQuery: " . sqlsrv_errors());
$sQueryCnt = "SELECT * FROM $sTable $sWhere";
$rResultCnt = sqlsrv_query( $gaSql['link'], $sQueryCnt ,$params, $options) or die (" $sQueryCnt: " . sqlsrv_errors());
$iFilteredTotal = sqlsrv_num_rows( $rResultCnt );
$sQuery = " SELECT * FROM $sTable ";
$rResultTotal = sqlsrv_query( $gaSql['link'], $sQuery ,$params, $options) or die(sqlsrv_errors());
$iTotal = sqlsrv_num_rows( $rResultTotal );
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = sqlsrv_fetch_array( $rResult ) ) {
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
if ( $aColumns[$i] != ' ' ) {
$v = $aRow[ $aColumns[$i] ];
$v = mb_check_encoding($v, 'UTF-8') ? $v : utf8_encode($v);
$row[]=$v;
}
}
If (!empty($row)) { $output['aaData'][] = $row; }
}
echo json_encode( $output );
?>
The Indexed Column
When you specify an indexed column to use for searches make sure that it is included in the column array! if you leave it out when specifying which columns to use the paging will not work. The paging of datatables with this code works be performing a select query of all primary keys when not in the TOP X results from another query.
The Connection Parameters
Ensure that the connection parameters are complete and correct. These are necessary to allow the script to connect to the database. If there aren't any parameters or the parameters are not correct to a SQL server login then the script will never be able to connect to the database.
The Column Array
I found that using this code without specified columns returned incorrect or NULL data. The best way to stop this was to fill the array with the column names I wanted to select each enclosed by quotes and separated by commas. It also stands to reason as why send anything but the required data back to the client?
The Client Side
HTML
DataTables requires a well formed html table to operate. This means having a table with full tags. If all of the tags are not there for the data being returned then DataTables will return an error. If you have columns that you want to return but not show then you can use the ColVis exntension and set a default column view setting in the java script.
Datatable uses its own CCS file so make sure you include it!
The java script
DataTables uses Jquery and its own Javascrpt file so make sure you include references to them within your script tags!
//Datatables Basic server side initilization
$(document).ready(function () {
//Datatable
var table = $('#tableID').DataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "serverSideScript.php"
});
});
These are the basic functions required for this server side script to work. It will get the top 10 rows on initial draw using the database parameters you specify in the php page. From here you can add the extensions you want such as ColVis and TableTools. Full Documentation for these Extensions and other initialization options for data tables can be found here.
I Hope this answer helps anyone else who is having the same issues that I had.

Related

Invalid JSON response reading mysql table

I'm trying to learn to work with server side database. I have a mysql database with tables of about 1000 rows. Using bootstrap grid with load, unload, edit and delete for each row, results slow, so i wan't to try server side.
I'm using the following code:
index.html
<html lang = "it">
<head>
<title>Datatable Server Side Processing with PHP</title>
<meta charset = "utf-8">
<!-- DataTables CSS library -->
<link rel="stylesheet" type="text/css" href="DataTables/datatables.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- DataTables JS library -->
<script type="text/javascript" src="DataTables/datatables.min.js"></script>
<script>
$(document).ready(function(){
$('#memListTable').DataTable({
"processing": true,
"serverSide": true,
"ajax": "getData.php"
});
});
</script>
<style>
.container {padding: 20px;}
</style>
</head>
<body>
<div class = "container">
<table id="memListTable" class="display" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Codice</th>
<th>Codice Fornitore</th>
<th>Ubicazione</th>
<th>Descrizione</th>
<th>Package</th>
<th>Quantità</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Codice</th>
<th>Codice Fornitore</th>
<th>Ubicazione</th>
<th>Descrizione</th>
<th>Package</th>
<th>Quantità</th>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
getData.php
<?php
// Database connection info
$dbDetails = array(
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db' => 'company',
'port' => '3308' // aggiunto
);
// DB table to use
$table = 'maglab';
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database.
// The `dt` parameter represents the DataTables column identifier.
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array( 'db' => 'codice', 'dt' => 1 ),
array( 'db' => 'cod_forn', 'dt' => 2 ),
array( 'db' => 'ubicazione', 'dt' => 3 ),
array( 'db' => 'descrizione', 'dt' => 4 ),
array( 'db' => 'package', 'dt' => 5 ),
array( 'db' => 'quantita', 'dt' => 6 )
);
// Include SQL query processing class
require( 'ssp.class.php' );
// Output data as json format
echo json_encode(
SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns )
);
?>
ssp.class.php
<?php
/*
* Helper functions for building a DataTables server-side processing SQL query
*
* The static functions in this class are just helper functions to help build
* the SQL used in the DataTables demo server-side processing scripts. These
* functions obviously do not represent all that can be done with server-side
* processing, they are intentionally simple to show how it works. More complex
* server-side processing operations will likely require a custom script.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* #license MIT - http://datatables.net/license_mit
*/
// Please Remove below 4 lines as this is use in Datatatables test environment for your local or live environment please remove it or else it will not work
$file = $_SERVER['DOCUMENT_ROOT'].'/datatables/pdo.php';
if ( is_file( $file ) ) {
include( $file );
}
class SSP {
/**
* Create the data output array for the DataTables rows
*
* #param array $columns Column information array
* #param array $data Data from the SQL get
* #return array Formatted data in a row based format
*/
static function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
if(empty($column['db'])){
$row[ $column['dt'] ] = $column['formatter']( $data[$i] );
}
else{
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
}
else {
if(!empty($column['db'])){
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
else{
$row[ $column['dt'] ] = "";
}
}
}
$out[] = $row;
}
return $out;
}
/**
* Database connection
*
* Obtain an PHP PDO connection from a connection details array
*
* #param array $conn SQL connection details. The array should have
* the following properties
* * host - host name
* * db - database name
* * user - user name
* * pass - user password
* #return resource PDO connection
*/
static function db ( $conn )
{
if ( is_array( $conn ) ) {
return self::sql_connect( $conn );
}
return $conn;
}
/**
* Paging
*
* Construct the LIMIT clause for server-side processing SQL query
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #return string SQL limit clause
*/
static function limit ( $request, $columns )
{
$limit = '';
if ( isset($request['start']) && $request['length'] != -1 ) {
$limit = "LIMIT ".intval($request['start']).", ".intval($request['length']);
}
return $limit;
}
/**
* Ordering
*
* Construct the ORDER BY clause for server-side processing SQL query
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #return string SQL order by clause
*/
static function order ( $request, $columns )
{
$order = '';
if ( isset($request['order']) && count($request['order']) ) {
$orderBy = array();
$dtColumns = self::pluck( $columns, 'dt' );
for ( $i=0, $ien=count($request['order']) ; $i<$ien ; $i++ ) {
// Convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.$column['db'].'` '.$dir;
}
}
if ( count( $orderBy ) ) {
$order = 'ORDER BY '.implode(', ', $orderBy);
}
}
return $order;
}
/**
* Searching / Filtering
*
* Construct the WHERE clause for server-side processing SQL query.
*
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here performance on large
* databases would be very poor
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #param array $bindings Array of values for PDO bindings, used in the
* sql_exec() function
* #return string SQL where clause
*/
static function filter ( $request, $columns, &$bindings )
{
$globalSearch = array();
$columnSearch = array();
$dtColumns = self::pluck( $columns, 'dt' );
if ( isset($request['search']) && $request['search']['value'] != '' ) {
$str = $request['search']['value'];
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['searchable'] == 'true' ) {
if(!empty($column['db'])){
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$globalSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
}
}
// Individual column filtering
if ( isset( $request['columns'] ) ) {
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
$str = $requestColumn['search']['value'];
if ( $requestColumn['searchable'] == 'true' &&
$str != '' ) {
if(!empty($column['db'])){
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$columnSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
}
}
// Combine the filters into a single string
$where = '';
if ( count( $globalSearch ) ) {
$where = '('.implode(' OR ', $globalSearch).')';
}
if ( count( $columnSearch ) ) {
$where = $where === '' ?
implode(' AND ', $columnSearch) :
$where .' AND '. implode(' AND ', $columnSearch);
}
if ( $where !== '' ) {
$where = 'WHERE '.$where;
}
return $where;
}
/**
* Perform the SQL queries needed for an server-side processing requested,
* utilising the helper functions of this class, limit(), order() and
* filter() among others. The returned array is ready to be encoded as JSON
* in response to an SSP request, or can be modified if needed before
* sending back to the client.
*
* #param array $request Data sent to server by DataTables
* #param array|PDO $conn PDO connection resource or connection parameters array
* #param string $table SQL table to query
* #param string $primaryKey Primary key of the table
* #param array $columns Column information array
* #return array Server-side processing response array
*/
static function simple ( $request, $conn, $table, $primaryKey, $columns )
{
$bindings = array();
$db = self::db( $conn );
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
"SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
FROM `$table`
$where
$order
$limit"
);
// Data set length after filtering
$resFilterLength = self::sql_exec( $db, $bindings,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table`
$where"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table`"
);
$recordsTotal = $resTotalLength[0][0];
/*
* Output
*/
return array(
"draw" => isset ( $request['draw'] ) ?
intval( $request['draw'] ) :
0,
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
/**
* The difference between this method and the `simple` one, is that you can
* apply additional `where` conditions to the SQL queries. These can be in
* one of two forms:
*
* * 'Result condition' - This is applied to the result set, but not the
* overall paging information query - i.e. it will not effect the number
* of records that a user sees they can have access to. This should be
* used when you want apply a filtering condition that the user has sent.
* * 'All condition' - This is applied to all queries that are made and
* reduces the number of records that the user can access. This should be
* used in conditions where you don't want the user to ever have access to
* particular records (for example, restricting by a login id).
*
* #param array $request Data sent to server by DataTables
* #param array|PDO $conn PDO connection resource or connection parameters array
* #param string $table SQL table to query
* #param string $primaryKey Primary key of the table
* #param array $columns Column information array
* #param string $whereResult WHERE condition to apply to the result set
* #param string $whereAll WHERE condition to apply to all queries
* #return array Server-side processing response array
*/
static function complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null )
{
$bindings = array();
$db = self::db( $conn );
$localWhereResult = array();
$localWhereAll = array();
$whereAllSql = '';
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
$whereResult = self::_flatten( $whereResult );
$whereAll = self::_flatten( $whereAll );
if ( $whereResult ) {
$where = $where ?
$where .' AND '.$whereResult :
'WHERE '.$whereResult;
}
if ( $whereAll ) {
$where = $where ?
$where .' AND '.$whereAll :
'WHERE '.$whereAll;
$whereAllSql = 'WHERE '.$whereAll;
}
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
"SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
FROM `$table`
$where
$order
$limit"
);
// Data set length after filtering
$resFilterLength = self::sql_exec( $db, $bindings,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table`
$where"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db, $bindings,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table` ".
$whereAllSql
);
$recordsTotal = $resTotalLength[0][0];
/*
* Output
*/
return array(
"draw" => isset ( $request['draw'] ) ?
intval( $request['draw'] ) :
0,
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
/**
* Connect to the database
*
* #param array $sql_details SQL server connection details array, with the
* properties:
* * host - host name
* * db - database name
* * user - user name
* * pass - user password
* #return resource Database connection handle
*/
static function sql_connect ( $sql_details )
{
try {
$db = #new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']};port={$sql_details['port']}",
$sql_details['user'],
$sql_details['pass'],
array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
);
}
catch (PDOException $e) {
self::fatal(
"An error occurred while connecting to the database. ".
"The error reported by the server was: ".$e->getMessage()
);
}
return $db;
}
/**
* Execute an SQL query on the database
*
* #param resource $db Database handler
* #param array $bindings Array of PDO binding values from bind() to be
* used for safely escaping strings. Note that this can be given as the
* SQL query string if no bindings are required.
* #param string $sql SQL query to execute.
* #return array Result from the query (all rows)
*/
static function sql_exec ( $db, $bindings, $sql=null )
{
// Argument shifting
if ( $sql === null ) {
$sql = $bindings;
}
$stmt = $db->prepare( $sql );
//echo $sql;
// Bind parameters
if ( is_array( $bindings ) ) {
for ( $i=0, $ien=count($bindings) ; $i<$ien ; $i++ ) {
$binding = $bindings[$i];
$stmt->bindValue( $binding['key'], $binding['val'], $binding['type'] );
}
}
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
self::fatal( "An SQL error occurred: ".$e->getMessage() );
}
// Return all
return $stmt->fetchAll( PDO::FETCH_BOTH );
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Internal methods
*/
/**
* Throw a fatal error.
*
* This writes out an error message in a JSON string which DataTables will
* see and show to the user in the browser.
*
* #param string $msg Message to send to the client
*/
static function fatal ( $msg )
{
echo json_encode( array(
"error" => $msg
) );
exit(0);
}
/**
* Create a PDO binding key which can be used for escaping variables safely
* when executing a query with sql_exec()
*
* #param array &$a Array of bindings
* #param * $val Value to bind
* #param int $type PDO field type
* #return string Bound key to be used in the SQL where this parameter
* would be used.
*/
static function bind ( &$a, $val, $type )
{
$key = ':binding_'.count( $a );
$a[] = array(
'key' => $key,
'val' => $val,
'type' => $type
);
return $key;
}
/**
* Pull a particular property from each assoc. array in a numeric array,
* returning and array of the property values from each item.
*
* #param array $a Array to get data from
* #param string $prop Property to read
* #return array Array of property values
*/
static function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ ) {
if(empty($a[$i][$prop])){
continue;
}
//removing the $out array index confuses the filter method in doing proper binding,
//adding it ensures that the array data are mapped correctly
$out[$i] = $a[$i][$prop];
}
return $out;
}
/**
* Return a string from an array or a string
*
* #param array|string $a Array to join
* #param string $join Glue for the concatenation
* #return string Joined string
*/
static function _flatten ( $a, $join = ' AND ' )
{
if ( ! $a ) {
return '';
}
else if ( $a && is_array($a) ) {
return implode( $join, $a );
}
return $a;
}
}
?>
now running index.php i receive DataTables warning: table id=memListTable - Invalid JSON response On Firefox, doing Web Developer > Network > XHR i see the following Json file
{"draw":1,"recordsTotal":907,"recordsFiltered":907,"data":[["1","CONN 4 POLI"," prova","C4"," Connettore 4 poli maschio stampato ","conn","1865"],["2","PE140-VIPER17",null,"A2","Off Line Converter","SMD","12"],["3","0003-XU-FAN7382008XSOPCP","FAN7382MX ","A2","Driver di porta Half Bridge Gate Dvr ","SOP-8","10"],["4","0004-XQ-0A20040XNPSOT2BJ","BC847BLT1G ","A2","Transistor bipolari - BJT 100mA 50V NPN VBE min 580mV ","SOT23-3","100"],["5","PE140-SFH6106",null,"A2","Phototransistor out","SMD","7"],["6","0006-XQ-40A00k6XXNDPAKIG","STGB19NC60HDT4 ","A2","Transistori IGBT N Ch 600V 19A ","D2PAK-3","273"],["7","PE140-BZX84C10V",null,"A2","Diodo Zener 9.4-10.6V 250mW","SMD","100"],["8","PE140-BAT54S",null,"A2","Schottky 30V Dual BAT54","SOT-323","4"],["10","000A-XD-1A501K0XXSDO21RF","US2MA ","A2","Raddrizzatori HER SMA GPPN 1.5A 600V ","DO-214AC-2","120"],["11","PE140-MMBD914",null,"A2","Diodo 100V","SMD","10"]]}
the grid is empty due to the error message. I don't understand why the table as 907 rows, we can see it on Json "recordsTotal":907,"recordsFiltered":907 but the json have only 10 random rows

Passing Array from AJAX to PHP, then passing data back as JSON

I am having a strange issue with my PHP / JSON data being returned by PHP. Here is my PHP:
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
// add your columns here!!!
$aColumns = array( 'Action', 'TimeOccurred', 'UserName', 'IPv4From', 'ShareName', 'FullFilePath', 'NewPathName', 'FromServer' );
//$aColumns = $_POST['selcolumns'];
//$aColumns = explode("-", $aColumns);
foreach ($aColumns as $col) {
file_put_contents( '../php/php-debug.txt', $col." ", FILE_APPEND );
}
$server = "";
$database = array("Database" => "");
$conn = sqlsrv_connect($server, $database);
if ($conn === false) die("<pre>".print_r(sqlsrv_errors(), true));
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "GUID";
/* DB table to use */
$sTable = $_POST['table'];
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* Local functions
*/
function fatal_error ( $sErrorMessage = '' ) {
header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
die( $sErrorMessage );
}
/* Ordering */
$sOrder = "";
if ( isset( $_POST['order'] ) ) {
$sOrder = "ORDER BY ";
if ( $_POST['columns'][0]['orderable'] == "true" ) {
$sOrder .= "".$aColumns[ intval( $_POST['order'][0]['column'] ) ]." ".
($_POST['order'][0]['dir']==='asc' ? 'asc' : 'desc');
}
}
/* escape function */
function mssql_escape($data) {
if(is_numeric($data))
return $data;
$unpacked = unpack('H*hex', $data);
return '0x' . $unpacked['hex'];
}
/* Filtering */
$sWhere = "";
if ( isset($_POST['search']['value']) && $_POST['search']['value'] != "" ) {
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
$sWhere .= $aColumns[$i]." LIKE '%".addslashes( $_POST['search']['value'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
if ( isset($_POST['columns'][$i]) && $_POST['columns'][$i]['searchable'] == "true" && $_POST['columns'][$i]['search']['value'] != '' ) {
if ( $sWhere == "" ) {
$sWhere = "WHERE ";
}
else {
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." LIKE '%".addslashes($_POST['columns'][$i]['search']['value'])."%' ";
}
}
/* Add the custom Date/Time filter */
if ( $sWhere == "" ) {
$sWhere = "WHERE (TimeOccurred >= "."'".$_POST['datestart']."'"." AND TimeOccurred <= "."'".$_POST['dateend']."')";
}
else {
$sWhere .= " AND (TimeOccurred >= "."'".$_POST['datestart']."'"." AND TimeOccurred <= "."'".$_POST['dateend']."')";
}
/* Paging */
$top = (isset($_POST['start']))?((int)$_POST['start']):0 ;
$limit = (isset($_POST['length']))?((int)$_POST['length'] ):5;
$sQuery = "SELECT TOP $limit ".implode(', ', $aColumns)." FROM $sTable $sWhere ".(($sWhere=="")?" WHERE ":" AND ")." $sIndexColumn NOT IN ( SELECT TOP $top $sIndexColumn FROM $sTable $sOrder ) $sOrder";
$rResult = sqlsrv_query($conn, $sQuery);
if($rResult === false){
die(sqlsrv_errors(SQLSRV_ERR_ERRORS));
}
/* Data set length after filtering */
$sQueryCnt = "SELECT * FROM $sTable $sWhere";
$rResultCnt = sqlsrv_query($conn, $sQueryCnt, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
$iFilteredTotal = sqlsrv_num_rows( $rResultCnt );
/* Total data set length */
$sQuery = "SELECT COUNT(GUID) FROM $sTable";
$rResultTotal = sqlsrv_query($conn, $sQuery, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
$aResultTotal = sqlsrv_fetch_array($rResultTotal, SQLSRV_FETCH_NUMERIC);
$iTotal = $aResultTotal[0];
/* Output */
$output = array(
"draw" => intval($_POST['draw']),
"recordsTotal" => $iTotal,
"recordsFiltered" => $iFilteredTotal,
"data" => array()
);
while ( $aRow = sqlsrv_fetch_array( $rResult, SQLSRV_FETCH_ASSOC) ) {
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
$row[$aColumns[$i]] = $aRow[ $aColumns[$i] ];
}
$output['data'][] = $row;
}
echo json_encode( $output );
?>
The part of the code that has me stuck:
$aColumns = array( 'Action', 'TimeOccurred', 'UserName', 'IPv4From', 'ShareName', 'FullFilePath', 'NewPathName', 'FromServer' );
//$aColumns = $_POST['selcolumns'];
//$aColumns = explode("-", $aColumns);
foreach ($aColumns as $col) {
file_put_contents( '../php/php-debug.txt', $col." ", FILE_APPEND );
}
If I leave my code as is and run through the process I get the data back as expected and everything works. I also get this output in my php-debug.txt:
Action TimeOccurred UserName IPv4From ShareName FullFilePath NewPathName FromServer
If I modify these lines of my code to this:
//$aColumns = array( 'Action', 'TimeOccurred', 'UserName', 'IPv4From', 'ShareName', 'FullFilePath', 'NewPathName', 'FromServer' );
$aColumns = $_POST['selcolumns'];
$aColumns = explode("-", $aColumns);
foreach ($aColumns as $col) {
file_put_contents( '../php/php-debug.txt', $col." ", FILE_APPEND );
}
I do not get the data back as expected. I get a warning stating invalid JSON response and in my php-debug.txt I get this content:
Action TimeOccurred UserName IPv4From ShareName FullFilePath NewPathName FromServer
It is driving me bonkers that the array has the same values either way and yet it doesn't work. There is an extra space on the end of the php-debug.txt the 2nd time around, not sure where that comes from or if it is the problem.
Hopefully someone can point me in the right direction.
I figured out the problem. For some reason there is an extra element being added to my array, but its an empty element so when I print there is nothing there. I added this to my code array_pop($aColumns); and for now that resolved the problem. Its kind of a dirty workout though, and I can't help but think that eventually there will come a scenario where the last element isn't blank and its something I need. Would love to figure out a cleaner solution.

UTF-8 encoding - impossible decode html table characters

Before ask this question I search deeply but unsuccessfully.
HTML page can't decode correcly characters (russian, chinese..)
Please follow me
server_processing connect to database and relative table (ajax).
server_processing.php
<?php
/*
* DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simply to show how
* server-side processing can be implemented, and probably shouldn't be used as
* the basis for a large complex system. It is suitable for simple use cases as
* for learning.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* #license MIT - http://datatables.net/license_mit
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
// DB table to use
$table = 'ajax';
// Table's primary key
$primaryKey = 'engine';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'engine', 'dt' => 0 ),
array( 'db' => '.', 'dt' => 1 ),
array( 'db' => 'browser', 'dt' => 2 ),
array( 'db' => 'platform', 'dt' => 3 ),
array( 'db' => 'version', 'dt' => 4 ),
array( 'db' => 'grade', 'dt' => 5 ),
);
// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'my_nathan3001',
'host' => 'localhost'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
ssp.class.php
<?php
/*
* Helper functions for building a DataTables server-side processing SQL query
*
* The static functions in this class are just helper functions to help build
* the SQL used in the DataTables demo server-side processing scripts. These
* functions obviously do not represent all that can be done with server-side
* processing, they are intentionally simple to show how it works. More complex
* server-side processing operations will likely require a custom script.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* #license MIT - http://datatables.net/license_mit
*/
// REMOVE THIS BLOCK - used for DataTables test environment only!
$file = $_SERVER['DOCUMENT_ROOT'].'/datatables/mysql.php';
if ( is_file( $file ) ) {
include( $file );
}
class SSP {
/**
* Create the data output array for the DataTables rows
*
* #param array $columns Column information array
* #param array $data Data from the SQL get
* #return array Formatted data in a row based format
*/
static function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
else {
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
}
$out[] = $row;
}
return $out;
}
/**
* Database connection
*
* Obtain an PHP PDO connection from a connection details array
*
* #param array $conn SQL connection details. The array should have
* the following properties
* * host - host name
* * db - database name
* * user - user name
* * pass - user password
* #return resource PDO connection
*/
static function db ( $conn )
{
if ( is_array( $conn ) ) {
return self::sql_connect( $conn );
}
return $conn;
}
/**
* Paging
*
* Construct the LIMIT clause for server-side processing SQL query
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #return string SQL limit clause
*/
static function limit ( $request, $columns )
{
$limit = '';
if ( isset($request['start']) && $request['length'] != -1 ) {
$limit = "LIMIT ".intval($request['start']).", ".intval($request['length']);
}
return $limit;
}
/**
* Ordering
*
* Construct the ORDER BY clause for server-side processing SQL query
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #return string SQL order by clause
*/
static function order ( $request, $columns )
{
$order = '';
if ( isset($request['order']) && count($request['order']) ) {
$orderBy = array();
$dtColumns = self::pluck( $columns, 'dt' );
for ( $i=0, $ien=count($request['order']) ; $i<$ien ; $i++ ) {
// Convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.$column['db'].'` '.$dir;
}
}
$order = 'ORDER BY '.implode(', ', $orderBy);
}
return $order;
}
/**
* Searching / Filtering
*
* Construct the WHERE clause for server-side processing SQL query.
*
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here performance on large
* databases would be very poor
*
* #param array $request Data sent to server by DataTables
* #param array $columns Column information array
* #param array $bindings Array of values for PDO bindings, used in the
* sql_exec() function
* #return string SQL where clause
*/
static function filter ( $request, $columns, &$bindings )
{
$globalSearch = array();
$columnSearch = array();
$dtColumns = self::pluck( $columns, 'dt' );
if ( isset($request['search']) && $request['search']['value'] != '' ) {
$str = $request['search']['value'];
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['searchable'] == 'true' ) {
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$globalSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
}
// Individual column filtering
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
$str = $requestColumn['search']['value'];
if ( $requestColumn['searchable'] == 'true' &&
$str != '' ) {
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$columnSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
// Combine the filters into a single string
$where = '';
if ( count( $globalSearch ) ) {
$where = '('.implode(' OR ', $globalSearch).')';
}
if ( count( $columnSearch ) ) {
$where = $where === '' ?
implode(' AND ', $columnSearch) :
$where .' AND '. implode(' AND ', $columnSearch);
}
if ( $where !== '' ) {
$where = 'WHERE '.$where;
}
return $where;
}
/**
* Perform the SQL queries needed for an server-side processing requested,
* utilising the helper functions of this class, limit(), order() and
* filter() among others. The returned array is ready to be encoded as JSON
* in response to an SSP request, or can be modified if needed before
* sending back to the client.
*
* #param array $request Data sent to server by DataTables
* #param array|PDO $conn PDO connection resource or connection parameters array
* #param string $table SQL table to query
* #param string $primaryKey Primary key of the table
* #param array $columns Column information array
* #return array Server-side processing response array
*/
static function simple ( $request, $conn, $table, $primaryKey, $columns )
{
$bindings = array();
$db = self::db( $conn );
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
"SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."`
FROM `$table`
$where
$order
$limit"
);
// Data set length after filtering
$resFilterLength = self::sql_exec( $db,
"SELECT FOUND_ROWS()"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table`"
);
$recordsTotal = $resTotalLength[0][0];
/*
* Output
*/
return array(
"draw" => intval( $request['draw'] ),
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
/**
* The difference between this method and the `simple` one, is that you can
* apply additional `where` conditions to the SQL queries. These can be in
* one of two forms:
*
* * 'Result condition' - This is applied to the result set, but not the
* overall paging information query - i.e. it will not effect the number
* of records that a user sees they can have access to. This should be
* used when you want apply a filtering condition that the user has sent.
* * 'All condition' - This is applied to all queries that are made and
* reduces the number of records that the user can access. This should be
* used in conditions where you don't want the user to ever have access to
* particular records (for example, restricting by a login id).
*
* #param array $request Data sent to server by DataTables
* #param array|PDO $conn PDO connection resource or connection parameters array
* #param string $table SQL table to query
* #param string $primaryKey Primary key of the table
* #param array $columns Column information array
* #param string $whereResult WHERE condition to apply to the result set
* #param string $whereAll WHERE condition to apply to all queries
* #return array Server-side processing response array
*/
static function complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null )
{
$bindings = array();
$db = self::db( $conn );
$localWhereResult = array();
$localWhereAll = array();
$whereAllSql = '';
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
$whereResult = self::_flatten( $whereResult );
$whereAll = self::_flatten( $whereAll );
if ( $whereResult ) {
$where = $where ?
$where .' AND '.$whereResult :
'WHERE '.$whereResult;
}
if ( $whereAll ) {
$where = $where ?
$where .' AND '.$whereAll :
'WHERE '.$whereAll;
$whereAllSql = 'WHERE '.$whereAll;
}
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
"SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."`
FROM `$table`
$where
$order
$limit"
);
// Data set length after filtering
$resFilterLength = self::sql_exec( $db,
"SELECT FOUND_ROWS()"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db, $bindings,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table` ".
$whereAllSql
);
$recordsTotal = $resTotalLength[0][0];
/*
* Output
*/
return array(
"draw" => intval( $request['draw'] ),
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
/**
* Connect to the database
*
* #param array $sql_details SQL server connection details array, with the
* properties:
* * host - host name
* * db - database name
* * user - user name
* * pass - user password
* #return resource Database connection handle
*/
static function sql_connect ( $sql_details )
{
try {
$db = #new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
$sql_details['user'],
$sql_details['pass'],
array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )
);
}
catch (PDOException $e) {
self::fatal(
"An error occurred while connecting to the database. ".
"The error reported by the server was: ".$e->getMessage()
);
}
return $db;
}
/**
* Execute an SQL query on the database
*
* #param resource $db Database handler
* #param array $bindings Array of PDO binding values from bind() to be
* used for safely escaping strings. Note that this can be given as the
* SQL query string if no bindings are required.
* #param string $sql SQL query to execute.
* #return array Result from the query (all rows)
*/
static function sql_exec ( $db, $bindings, $sql=null )
{
// Argument shifting
if ( $sql === null ) {
$sql = $bindings;
}
$stmt = $db->prepare( $sql );
//echo $sql;
// Bind parameters
if ( is_array( $bindings ) ) {
for ( $i=0, $ien=count($bindings) ; $i<$ien ; $i++ ) {
$binding = $bindings[$i];
$stmt->bindValue( $binding['key'], $binding['val'], $binding['type'] );
}
}
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
self::fatal( "An SQL error occurred: ".$e->getMessage() );
}
// Return all
return $stmt->fetchAll();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Internal methods
*/
/**
* Throw a fatal error.
*
* This writes out an error message in a JSON string which DataTables will
* see and show to the user in the browser.
*
* #param string $msg Message to send to the client
*/
static function fatal ( $msg )
{
echo json_encode( array(
"error" => $msg
) );
exit(0);
}
/**
* Create a PDO binding key which can be used for escaping variables safely
* when executing a query with sql_exec()
*
* #param array &$a Array of bindings
* #param * $val Value to bind
* #param int $type PDO field type
* #return string Bound key to be used in the SQL where this parameter
* would be used.
*/
static function bind ( &$a, $val, $type )
{
$key = ':binding_'.count( $a );
$a[] = array(
'key' => $key,
'val' => $val,
'type' => $type
);
return $key;
}
/**
* Pull a particular property from each assoc. array in a numeric array,
* returning and array of the property values from each item.
*
* #param array $a Array to get data from
* #param string $prop Property to read
* #return array Array of property values
*/
static function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ ) {
$out[] = $a[$i][$prop];
}
return $out;
}
/**
* Return a string from an array or a string
*
* #param array|string $a Array to join
* #param string $join Glue for the concatenation
* #return string Joined string
*/
static function _flatten ( $a, $join = ' AND ' )
{
if ( ! $a ) {
return '';
}
else if ( $a && is_array($a) ) {
return implode( $join, $a );
}
return $a;
}
}
Try to include $db_connect->set_charset("utf8"); in your PHP code when connecting to the database.
Example for connection:
<?php
$db_connect = new mysqli("server_xyz", "user_abc", "pw_123", "databasename_456");
$db_connect->set_charset("utf8");
?>
Did you set PHP's Charset also to UTF-8?
header( 'content-type: text/html; charset=utf-8' );

Adding custom field jQuery Datatables

I am using jQuery Datatables to populate my data. I am using the server side method which I need coz I am fetching hundred thousand records. However I don't know how to add a custom field. For example
|Action|
________
http://localhost/qms/public/customer/1/edit
Which the 1 should be the ID
Because on the datatables, you only declaring column tables that you need so I don't know how to add a custom one.
I currently have this:
I need to put action column to edit those customers. I am using Laravel 5.1
HTML View:
<table id="CustomerList" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th colspan="7"> <center>Customer Information<center></th>
<!-- <th colspan="2"> <center>Actions<center></th> -->
</tr>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Country</th>
<th>Postcode</th>
<!-- <th>Edit</th>
<th>Delete</th> -->
</tr>
</thead>
<tbody>
</tbody>
</table>
Ajax:
<script type="text/javascript">
$(document).ready(function() {
$.fn.dataTable.ext.legacy.ajax = true;
$('#CustomerList').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "api/customer/all",
"paging" : true,
"searching" : true,
"ordering" : true,
} );
var tt = new $.fn.dataTable.TableTools( $('#CustomerList').DataTable() );
$( tt.fnContainer() ).insertBefore('div.dataTables_wrapper');
});
</script>
Controller:
public function apiGetCustomers()
{
/*=================================================================*/
/*
* Script: DataTables server-side script for PHP and PostgreSQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array('id', 'firstname', 'lastname', 'gender', 'phone_num', 'country', 'postcode' );
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "phone_num";
/* DB table to use */
$sTable = "customers";
/* Database connection information */
$gaSql['user'] = "postgres";
$gaSql['password'] = "postgres";
$gaSql['db'] = "qms";
$gaSql['server'] = "localhost";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* DB connection
*/
$gaSql['link'] = pg_connect(
" host=".$gaSql['server'].
" dbname=".$gaSql['db'].
" user=".$gaSql['user'].
" password=".$gaSql['password']
) or die('Could not connect: ' . pg_last_error());
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayLength'] )." OFFSET ".
intval( $_GET['iDisplayStart'] );
}
/*
* Ordering
*/
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE This assumes that the field that is being searched on is a string typed field (ie. one
* on which ILIKE can be used). Boolean fields etc will need a modification here.
*/
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" )
{
if($aColumns[$i] != 'id') // Exclude ID for filtering
{
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ")";
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
";
$rResultTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iTotal = pg_num_rows($rResultTotal);
pg_free_result( $rResultTotal );
if ( $sWhere != "" )
{
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
$sWhere
";
$rResultFilterTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iFilteredTotal = pg_num_rows($rResultFilterTotal);
pg_free_result( $rResultFilterTotal );
}
else
{
$iFilteredTotal = $iTotal;
}
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = pg_fetch_array($rResult, null, PGSQL_ASSOC) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
// Free resultset
pg_free_result( $rResult );
// Closing connection
pg_close( $gaSql['link'] );
}
Add headers for the custom fields, as you have above <th>Edit</th><th>Delete</th>
Use column rendering to add the content of the custom fields. Since you are using an "array of arrays" as datasource, you must do it this way :
small demonstration -> http://jsfiddle.net/pqgynvys/
var table = $('#example').DataTable({
data : data.data,
columns : [
null,
null,
null,
null,
null,
null,
null,
{ render : function() {
return '<button>edit</button>'
}
},
{ render : function() {
return '<button>delete</button>'
}
}
]
})
SOLUTION
In HTML add one column in the table header for Action.
In DataTables initialization options add columnDefs to target that 8th column ("targets": 7, zero-based index) and use render option to produce content for that column.
In the render function, you can use row variable to access the data for the row. Since you're returning array of arrays in your PHP script, you can access your ID by using row[0] (1st column, zero-based index).
var table = $('#CustomerList').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "api/customer/all"
"columnDefs": [
{
"targets": 7,
"render": function(data, type, row, meta){
return 'Edit';
}
}
]
});
DEMO
See this jsFiddle for code and demonstration.

How to populate a jquery datatable using server side processing (PHP)?

I'm trying to populate a datatable with a server side PHP script which echoes the data from a postgres table (~75K rows). I followed the steps given in the datatable page and implemented it, but the table doesn't show any data. This is what I have so long:
table definition in a jsp file:
<table id="myTable" class="table table-striped" width="100%">
<thead>
<tr>
<th>idpersona</th>
<th>primerapellido</th>
<th>primernombre</th>
<th>numeroidentificacion</th>
<th>fechanacimiento</th>
</tr>
</thead>
<tfoot>
<tr>
<th>idpersona</th>
<th>primerapellido</th>
<th>primernombre</th>
<th>numeroidentificacion</th>
<th>fechanacimiento</th>
</tr>
</tfoot>
</table>
Here is my function to initialise the table. I tried for hours (I'm a newbie programmer) to find the right folder where I must place the PHP file. Right now it is in the htdocs folder of my apache server (so I can access it from /localhost/tablabd.php). Is this the right way to do it?
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
$('#myTable').dataTable( {
"Processing": true,
"ServerSide": true,
"sAjaxSource": "http://localhost/tablabd.php"
} );
} );
</script>
And finally the PHP script. When I type localhost/tablabd.php in my browser, all the data is fetched correctly. But when I execute my Java project, it doesn't show anything in the table 'myTable'.
<?php
/*
* Script: DataTables server-side script for PHP and PostgreSQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array("idpersona", "primerapellido","primernombre", "numeroidentificacion", "fechanacimiento");
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = '"idpersona"';
/* DB table to use */
$sTable = '"tpersonas"';
/* Database connection information */
$gaSql['user'] = "postgres";
$gaSql['password'] = "******";
$gaSql['db'] = "sisben";
$gaSql['server'] = "localhost";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* DB connection
*/
$gaSql['link'] = pg_connect(
" host=".$gaSql['server'].
" dbname=".$gaSql['db'].
" user=".$gaSql['user'].
" password=".$gaSql['password']
) or die('Could not connect: ' . pg_last_error());
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] )." OFFSET ".
intval( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE This assumes that the field that is being searched on is a string typed field (ie. one
* on which ILIKE can be used). Boolean fields etc will need a modification here.
*/
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" )
{
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ")";
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
";
$rResultTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iTotal = pg_num_rows($rResultTotal);
pg_free_result( $rResultTotal );
if ( $sWhere != "" )
{
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
$sWhere
";
$rResultFilterTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iFilteredTotal = pg_num_rows($rResultFilterTotal);
pg_free_result( $rResultFilterTotal );
}
else
{
$iFilteredTotal = $iTotal;
}
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = pg_fetch_array($rResult, null, PGSQL_ASSOC) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == 'idpersona' )
{
/* Special output formatting for 'ID' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
// Free resultset
pg_free_result( $rResult );
// Closing connection
pg_close( $gaSql['link'] );
?>
and a sample of the output of the script in the broswer: maybe I'm missing a column mapping somewhere?
{"sEcho":0,"iTotalRecords":74047,"iTotalDisplayRecords":74047,"aaData":[["e71657b3-a7f5-4a10-bc43-d0edbeb5cdab","PEREZ","ABDON","4299249","1947-07-10 00:00:00"],["796db2d4-fee3-4cca-ae06-429a2ea6c5af","TORREZ","MARIA","24240762","1951-09-17 00:00:00"]]}
Here is the info Firebug shows when I access the page on my application which contains the table:
_ 1440905636814
columns[0][data] 0
columns[0][name]
columns[0][orderable] true
columns[0][search][regex] false
columns[0][search][value]
columns[0][searchable] true
columns[1][data] 1
columns[1][name]
columns[1][orderable] true
columns[1][search][regex] false
columns[1][search][value]
columns[1][searchable] true
columns[2][data] 2
columns[2][name]
columns[2][orderable] true
columns[2][search][regex] false
columns[2][search][value]
columns[2][searchable] true
columns[3][data] 3
columns[3][name]
columns[3][orderable] true
columns[3][search][regex] false
columns[3][search][value]
columns[3][searchable] true
columns[4][data] 4
columns[4][name]
columns[4][orderable] true
columns[4][search][regex] false
columns[4][search][value]
columns[4][searchable] true
draw 1
length 20
order[0][column] 0
order[0][dir] asc
search[regex] false
search[value]
start 0
Thanks in advance.
SOLUTION
Correct option names are bProcessing and bServerSide. Your DataTables initialization code should be:
$('#myTable').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/tablabd.php"
});
NOTES
I have changed URL to /tablabd.php because if your HTML and PHP are on different domain, Ajax calls may fail unless you allow cross-domain requests. Make sure you have HTML and PHP on the same domain.

Categories