【发布时间】:2011-03-19 02:01:44
【问题描述】:
我浏览了网站上所有的分页问题和答案,在所有冗长的代码和OO解决方案中,这段代码是最短和最简单的:
<?php
// insert your mysql connection code here
$perPage = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $perPage * ($page - 1);
$query = "SELECT COUNT(*) as total FROM table";
$r = mysql_fetch_assoc(mysql_query($query));
$totalPages = ceil($r['total'] / $perPage);
$links = "";
for ($i = 1; $i <= $totalPages; $i++) { $links .= ($i != $page ) ? "<a href='index.php?page=$i'>Page $i</a> " : "$page "; }
$query = "SELECT * FROM table ORDER BY title LIMIT $startAt, $perPage";
$r = mysql_query($query);
// display results here the way you want
echo $links; // show links to other pages
但我仍然看不到如何在后续页面上使用更新的LIMIT 重新生成查询。这些消息都没有说明这一点,无论我尝试哪种代码,我都会继续得到空白的第二页。如果有人能解释如何将查询结果带到下一页,我将不胜感激。
【问题讨论】:
标签: php mysql pagination