Pagination breaks on 2nd page with SQL Server
Pagination breaks on 2nd page with SQL Server
tcash21
Posts: 13Questions: 0Answers: 0
I thought I had everything working for pagination using SQL Server. The Next button is no longer grayed out and the iTotal #s are correct but when I click Next to go to page 2 there are no rows!
Debugger page:
http://debug.datatables.net/adinix
[code]
/*
* Paging modified for SQL Server
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = " row > " .$_GET['iDisplayStart']." and row <= ".
$_GET['iDisplayLength'];
}
[/code]
I removed the WHERE clause for now for testing purposes:
[code]
/*
* SQL queries
* Get filtered data to display without pagination
*/
$sQuery1 = "
SELECT *
FROM
$sTable
$sOrder
";
// Query with filtering AND pagination
$sQuery2 = "
SELECT *
FROM
( SELECT *, ROW_NUMBER() OVER (ORDER BY NumDays) as row FROM
$sTable) $sTable WHERE
$sLimit
$sOrder
";
$rResult = mssql_query( $sQuery1, $gaSql['link'] ) or die(mssql_get_last_message());
/* Data set length after filtering */
$sQuery1 = "
SELECT @@ROWCOUNT
";
$rResultFilterTotal = mssql_query( $sQuery1, $gaSql['link'] ) or die(mssql_get_last_message());
$aResultFilterTotal = mssql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT('".$sIndexColumn."')
FROM $sTable
";
$rResultTotal = mssql_query( $sQuery, $gaSql['link'] ) or die(mssql_get_last_message());
$aResultTotal = mssql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
$rResult2 = mssql_query( $sQuery2, $gaSql['link'] ) or die(mssql_get_last_message());
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mssql_fetch_array( $rResult2 ) )
{
$row = array();
for ( $i=0 ; $i
[/code]
Debugger page:
http://debug.datatables.net/adinix
[code]
/*
* Paging modified for SQL Server
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = " row > " .$_GET['iDisplayStart']." and row <= ".
$_GET['iDisplayLength'];
}
[/code]
I removed the WHERE clause for now for testing purposes:
[code]
/*
* SQL queries
* Get filtered data to display without pagination
*/
$sQuery1 = "
SELECT *
FROM
$sTable
$sOrder
";
// Query with filtering AND pagination
$sQuery2 = "
SELECT *
FROM
( SELECT *, ROW_NUMBER() OVER (ORDER BY NumDays) as row FROM
$sTable) $sTable WHERE
$sLimit
$sOrder
";
$rResult = mssql_query( $sQuery1, $gaSql['link'] ) or die(mssql_get_last_message());
/* Data set length after filtering */
$sQuery1 = "
SELECT @@ROWCOUNT
";
$rResultFilterTotal = mssql_query( $sQuery1, $gaSql['link'] ) or die(mssql_get_last_message());
$aResultFilterTotal = mssql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT('".$sIndexColumn."')
FROM $sTable
";
$rResultTotal = mssql_query( $sQuery, $gaSql['link'] ) or die(mssql_get_last_message());
$aResultTotal = mssql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
$rResult2 = mssql_query( $sQuery2, $gaSql['link'] ) or die(mssql_get_last_message());
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mssql_fetch_array( $rResult2 ) )
{
$row = array();
for ( $i=0 ; $i
[/code]
This discussion has been closed.
Replies
Here is an article outlining usage of ROW_NUMBER() in MS Sql Server 2005 (not sure of your version). Get the query working directly first and then you should have no problems back porting it into your code!
http://www.databasejournal.com/features/mssql/article.php/3572301/RowNumber-function-in-SQL-Server-2005.htm
[code]
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY NumDays) as row FROM
leadActivity) leadActivity WHERE row > 1 and row <= 100 AND (Company like '%Studio%') ORDER BY Company
[/code]
And it returns 4 rows of data.
The next query also works and returns the #4.
[code]
SELECT @@ROWCOUNT;
[/code]
Is that the desired behavior?
Thanks,
Tanya
$sQuery2 = "
SELECT *
FROM
( SELECT *, ROW_NUMBER() OVER (ORDER BY NumDays) as row FROM
$sTable) $sTable WHERE
$sLimit
$sOrder
";
[/code]
After that put:
[code]
die($sQuery2);
[/code]
Then in firebug the result should be your query -- your table won't load correctly until you remove the die statement but this will allow you to see what the query being generated is.
Had to update my limit to be:
[code]
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = " row > " .$_GET['iDisplayStart']." and row <= ".
($_GET['iDisplayLength'] + $_GET['iDisplayStart']);
}
[/code]
I just added an offset to iDisplayLength.