繁体   English   中英

使用PHP和MySQL分页

[英]Pagination with PHP and MySQL

我已经尝试了许多不同的代码段,这些代码段用于在结果集中使用分页。 在每种情况下,我都有相同的问题。 在查询末尾使用LIMIT,我可以正确显示第一页以及正确链接的导航链接(因此,执行返回行数和要在每页上显示的数字的计算代码是正确的),但是当您单击以单击指向其他任何页面的链接时,它们将为空白。 那么,如何使更新后的查询再次运行并在后续页面上显示我的结果?

这是我尝试过的一段代码:

$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } 
else { $currentpage = 1; } 

// if current page is greater than total pages...
if ($currentpage > $totalpages) { $currentpage = $totalpages; } 

// if current page is less than first page...
if ($currentpage < 1) { $currentpage = 1; } 

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// run the query
$sql = "SELECT * FROM table WHERE keyword LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $link) or trigger_error("SQL", E_USER_ERROR);
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) 
{   
     //echo data for each record here
} 

// building pagination links 
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {   
    // show << link to go back to page 1   
    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";   
    // get previous page num   
    $prevpage = $currentpage - 1;   
    // show < link to go back to 1 page   
    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

由于href过多,不允许显示其余的导航代码,但链接显示正确。 问题似乎在于查询如何结转到下一页。

您必须使用limit offset, rowcount

通常用于nth page

offset= (n-1)*pagesize和行rowcount=pagesize

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM