sql server语言 - 帮助分页



我已经创建了分页,它目前正在工作,但唯一的问题是,如果我有成千上万的结果…显示将是:

[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18]等等。

我只是想显示的页面显示看起来像这样:

以前[1][2][3][4][5]…下一个

之前…[5][6][7][8][9][10]…下一个

谁能给建议或提供一些示例代码,会给我一个像上面这样的结果?如果有什么不清楚的,请让我知道!

欢呼,尼尔

我当前使用的代码如下:

<?php
/*data base connection */
include "datebase connection";
/* SQL query */
$tsql = ("  SELECT   TOP 100  tie_parent_id, CAST(geo_post AS varchar(6)) + '.' + CAST(geo_sample AS varchar(6)) AS Mile, gps_lat, gps_long, rotten, split, wheel_cut, broken, quality
FROM  database
");
$stmt = sqlsrv_query($conn,$tsql, array(), array( "Scrollable" => 'static'));
if( $stmt === false)
{
     echo "Error in query preparation/execution.n";
     die( print_r( sqlsrv_errors(), true));
}
/* DETERMINING THE NUMBER OF ROWS (AND PAGES) */
// Set the number of rows to be returned on a page. 
$rowsPerPage = 10;
// Get the total number of rows returned by the query.  
$rowsReturned = sqlsrv_num_rows($stmt);

if($rowsReturned === false) 
    die( print_r( sqlsrv_errors(), true)); 
elseif($rowsReturned == 0) 
{ 
    echo "No rows returned."; 
    exit(); 
} 
else 
{     
    /* Calculate number of pages. */ 
    $numOfPages = ceil($rowsReturned/$rowsPerPage); 
}

/* FUNCTION FOR PAGING */

function getPage($stmt, $pageNum, $rowsPerPage ) 
{ 
    $offset = ($pageNum - 1) * $rowsPerPage; 
    $rows = array(); 
    $i = 0; 
    while($row = sqlsrv_fetch_array($stmt, 
                                    SQLSRV_FETCH_NUMERIC, 
                                    SQLSRV_SCROLL_ABSOLUTE, 
                                    $offset + $i) 
           && $i < $rowsPerPage) 
    { 
        array_push($rows, $row); 
        $i++; 
    } 
    $row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_NUMERIC,SQLSRV_SCROLL_ABSOLUTE, $offset -1);
    return $rows; 
}

// Display the selected page of data. 
echo "<table width='800' border='0'>";
echo "<tr> <th>Tie ID</th> <th>Mile/Yard</th> <th>GPS Lat</th><th>GPS Long</th><th>Rotten</th><th>Split</th><th>WheelCut</th> <th>Broken</th><th>Quality</th> </tr>";
// keeps getting the next row until there are no more to get
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1; 
$page = getPage($stmt, $pageNum, $rowsPerPage);

$color1 = "#ffffff"; 
$color2 = "#edf5fa"; 
$row_count = "0";

while($row_count<10 ) {
$row=sqlsrv_fetch_array($stmt);
$tie_parent_id = $row["tie_parent_id"];
$geo_post = $row["Mile"];
$lat =$row["gps_lat"];
$long =$row["gps_long"];
$rotten =$row["rotten"];
$split =$row["split"];
$wheelcut =$row["wheel_cut"];
$broken =$row["broken"];
$quality =$row["quality"];
$row_color = ($row_count % 2) ? $color1 : $color2; 

?>
<tr> 
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["tie_parent_id"]; ?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["Mile"];?> </td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["gps_lat"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["gps_long"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["rotten"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["split"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["wheel_cut"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["broken"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["quality"];?></td>
</td></tr>

<?php
    $row_count++; 
}
?>

<?php
/* PREVIOUS PAGE NAVIGATION TOP OF PAGE */
// Display Previous Page link if applicable. 
if($pageNum > 1) 
{ 
    $prevPageLink = "?pageNum=".($pageNum - 1); 
    echo "<a href='$prevPageLink'>Previous Page</a>&nbsp"; 
}
/*DISPLAYING LINKS TO PAGES TOP OF PAGE*/
for($i = 1; $i<=$numOfPages; $i++)  
{  
    $pageLink = "?pageNum=$i";  
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");  
}
/* NEXT PAGE NAVIGATION TOP OF PAGE */

// Display Next Page link if applicable. 
if($pageNum < $numOfPages) 
{ 
    $nextPageLink = "?pageNum=".($pageNum + 1); 
    echo "&nbsp;&nbsp;<a href='$nextPageLink'>Next Page</a>"; 
}
?>
</form>
<?php
/* Close the connection. */
sqlsrv_close( $conn);
?>

这个变化实际上并不太疯狂——你已经拥有了它的大部分。为了页面的显示,你的算法需要稍微改变一下——所以不要像下面这样运行循环:

<>之前 /*DISPLAYING LINKS TO PAGES TOP OF PAGE*/ for($i = 1; $i<=$numOfPages; $i++) { $pageLink = "?pageNum=$i"; print("$i  "); } 之前

你需要从你当前的页码开始…

<>之前 /*first check to make sure the number of pages don't exceed maximum*/ $totalPagesToLoop = $pageNum + $numOfPages; if($totalPagesToLoop > ceiling(total number of pages required to show all the records ([number of total rows]/[number of rows to show per page]) { $totalPagesToLoop = ceiling(total number of pages required to show all the records ([number of total rows]/[number of rows to show per page]); } /*DISPLAYING LINKS TO PAGES TOP OF PAGE*/ for($i = $pageNum; $i<=$totalPagesToLoop; $i++) { $pageLink = "?pageNum=$i"; print("$i  "); } 之前

但是-这不是全部,你现在必须编码特殊的"上一个"one_answers"下一个"按钮(你决定多少增量当你点击它-谷歌增量1,直到它达到20,然后只是显示20一次,一个接一个地移动。你必须测试你的变量来决定是否显示'previous'或'next'按钮…

我使用下面的代码解决了这个问题:

for($i = $pageNum; $i<=$numOfPages&&$pagesadded<=9; $i++)  
{  
    $pagesadded+=1;
    $pageLink = "?pageNum=$i";  
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");  
}

一次显示10页。

欢呼,尼尔

最新更新