[英]SQL Server 2008 Pagination PHP
I am trying to display 10 records per page by using ROW_NUMBER function of SQL SERVER 2008. 我试图通过使用SQL SERVER 2008的ROW_NUMBER函数每页显示10条记录。
I think there is something wrong with my query because I only define where to start and where to end displaying records (from 1 to 10), but not the amount of records displayed per page. 我认为查询有问题,因为我仅定义显示记录的起点和终点(从1到10),而不是每页显示的记录数。
As I go to the next page I get no results displayed at all because I don't know how to add $per_page variable to my SQL query properly. 当我转到下一页时,根本没有显示任何结果,因为我不知道如何向我的SQL查询中正确添加$ per_page变量。
All I want is be able to display the first 10 product IDs on page 1, and once I click on the "next" button, the next 10 product IDs will be displayed, etc. 我所希望的是能够在第1页上显示前10个产品ID,一旦单击“下一步”按钮,将显示下10个产品ID,依此类推。
This is the code that I have right now: 这是我现在拥有的代码:
$per_page = 10;
if(!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}
if($page<=1)
{
$start = 0;
}
else
{
$start = $page * $per_page - $per_page;
}
$tsql = " SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY productID) AS
rownum, productID FROM products) AS products1
WHERE rownum >= $start AND rownum <= $per_page";
$num_rows = sqlsrv_num_rows(sqlsrv_query($conn,$tsql));
$num_pages = ceil($num_rows / $per_page);
$stmt = sqlsrv_query($conn,$tsql);
while($row = sqlsrv_fetch_array($stmt)){
echo $row['productID']. "<br/>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
//prev
if($prev > 0)
{
echo "<a href='?page=$prev'>prev</a> ";
}
//numbers
$number = 1;
for($number; $number <= $num_pages; $number +=1)
{
if($page==$number)
{
echo " <b>[$number]</b> ";
}
else
{
echo "<a href='?page=$number'>$number</a> ";
}
}
//next
echo "<a href='?page=$next'>next</a>";
I'm not familiar with sql server, but it seems to me you just need the end point: 我对sql server不熟悉,但是在我看来您只需要终点即可:
$tsql = " SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY productID) AS
rownum, productID FROM products) AS products1
WHERE rownum >= $start AND rownum < ($start + $per_page)";
^ changed ^
And if $page
is supposed to be an integer, it's always best to make sure that it is: 并且如果
$page
应该是整数,则始终最好确保它是:
$page = (int) $_GET['page'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.