How do i find error with datatables serverside processing
How do i find error with datatables serverside processing
vsoftforyou
Posts: 1Questions: 0Answers: 0
I created a datatable with serverside processing in my site.
My script is,
[code]$('.mytable').dataTable({
"bProcessing": true,
"bServerSide": true,
"sPaginationType": "full_numbers",
"sAjaxSource": "my_data.php"
});[/code]
It does not work for me. Its working fine when i remove the line, [code]"bServerSide": true[/code] But I want the serverside processing to reduce the loading time.
PHP CODE in my_data.php:
[code]$aColumns = array( 'varFile', 'Questions', 'Answer', 'answer', 'descriptionofanswer', 'varTag1' );
$sLimit = "";
if ( isset( $_REQUEST['iDisplayStart'] ) && $_REQUEST['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".( $_REQUEST['iDisplayStart'] ).", ".
( $_REQUEST['iDisplayLength'] );
}
if ( isset( $_REQUEST['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $iquery();
$sel_data=$dataReader->readAll();
$connection = Yii::app()->db;
$iFilteredTotal = $connection->createCommand('SELECT FOUND_ROWS()')->queryScalar();
$sel_total_records = Yii::app()->db->createCommand()
->select('COUNT(aid) as tot_count')
->from('mytable')
->queryRow();
$itot_val = $sel_total_records['tot_count'];
$output = array(
"sEcho" => intval($_REQUEST['sEcho']),
"iTotalRecords" => $itot_val,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
$c = 0;
foreach($sel_data as $key=>$value) {
$c++;
$row[] = $c;
$row[] = $value['val1'];
$row[] = $value['val2'];
$row[] = $value['val3'];
$row[] = $value['val4'];
$row[] = $value['val5'];
$row[] = "Edit link";
$row[] = "Delete link";
}
echo json_encode( $output ); [/code]
my_data.php returns the data in the following format
[code] {"sEcho":0,"iTotalRecords":89,"iTotalDisplayRecords":"89","aaData":[
[1,"filename1.csv","question21","Answer21",""," testtag1, testtag2","Edit link","Delete link"],
[2,"filename2.csv","question22","Answer22",""," testtag1, testtag2","Edit link","Delete link"],
[3,"filename3.csv","question23","Answer23",""," testtag1, testtag2","Edit link","Delete link"],
[4,"filename4.csv","question24","Answer24",""," testtag1, testtag2","Edit link","Delete link"],
[5,"filename5.csv","question25","Answer25",""," testtag1, testtag2","Edit link","Delete link"],
...........
[89,"filename89.csv","question89","Answer89",""," testtag1, testtag2","Edit link","Delete link"]
]}
[/code]
And my site is integrated with the Yii framework. I am not sure whether the problem is in json data creation.
The page does not alert any error, and it shows only the table headings. Please help me to detect the error.
My script is,
[code]$('.mytable').dataTable({
"bProcessing": true,
"bServerSide": true,
"sPaginationType": "full_numbers",
"sAjaxSource": "my_data.php"
});[/code]
It does not work for me. Its working fine when i remove the line, [code]"bServerSide": true[/code] But I want the serverside processing to reduce the loading time.
PHP CODE in my_data.php:
[code]$aColumns = array( 'varFile', 'Questions', 'Answer', 'answer', 'descriptionofanswer', 'varTag1' );
$sLimit = "";
if ( isset( $_REQUEST['iDisplayStart'] ) && $_REQUEST['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".( $_REQUEST['iDisplayStart'] ).", ".
( $_REQUEST['iDisplayLength'] );
}
if ( isset( $_REQUEST['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $iquery();
$sel_data=$dataReader->readAll();
$connection = Yii::app()->db;
$iFilteredTotal = $connection->createCommand('SELECT FOUND_ROWS()')->queryScalar();
$sel_total_records = Yii::app()->db->createCommand()
->select('COUNT(aid) as tot_count')
->from('mytable')
->queryRow();
$itot_val = $sel_total_records['tot_count'];
$output = array(
"sEcho" => intval($_REQUEST['sEcho']),
"iTotalRecords" => $itot_val,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
$c = 0;
foreach($sel_data as $key=>$value) {
$c++;
$row[] = $c;
$row[] = $value['val1'];
$row[] = $value['val2'];
$row[] = $value['val3'];
$row[] = $value['val4'];
$row[] = $value['val5'];
$row[] = "Edit link";
$row[] = "Delete link";
}
echo json_encode( $output ); [/code]
my_data.php returns the data in the following format
[code] {"sEcho":0,"iTotalRecords":89,"iTotalDisplayRecords":"89","aaData":[
[1,"filename1.csv","question21","Answer21",""," testtag1, testtag2","Edit link","Delete link"],
[2,"filename2.csv","question22","Answer22",""," testtag1, testtag2","Edit link","Delete link"],
[3,"filename3.csv","question23","Answer23",""," testtag1, testtag2","Edit link","Delete link"],
[4,"filename4.csv","question24","Answer24",""," testtag1, testtag2","Edit link","Delete link"],
[5,"filename5.csv","question25","Answer25",""," testtag1, testtag2","Edit link","Delete link"],
...........
[89,"filename89.csv","question89","Answer89",""," testtag1, testtag2","Edit link","Delete link"]
]}
[/code]
And my site is integrated with the Yii framework. I am not sure whether the problem is in json data creation.
The page does not alert any error, and it shows only the table headings. Please help me to detect the error.
This discussion has been closed.
Replies
sEcho should never be 0.
From the documentation ( http://datatables.net/usage/server-side ):
> An unaltered copy of sEcho sent from the client side. This parameter will change with each draw (it is basically a draw count) - so it is important that this is implemented. Note that it strongly recommended for security reasons that you 'cast' this parameter to an integer in order to prevent Cross Site Scripting (XSS) attacks.
Also `iTotalDisplayRecords` should be an integer. Beyond that we'd need a link to the page.
Allan