我的分页页面链接是这样的。然后页面就变成了
1、2、3、4、5、6、7、8、9、10等等。
但是我想限制这个,所以如果有超过5个页面,它应该只显示5个链接,像这样:
1 2 3 4 5…97 98 9999是最后一页
如果你转到下一页,它只会像这样改变第一页:
3 4 5…97 98 99
function pagination($current_page_number, $total_records_found, $query_string = null)
{
$page = 1;
echo "Page: ";
for ($total_pages = ($total_records_found/NUMBER_PER_PAGE); $total_pages > 0; $total_pages--)
{
if ($page != $current_page_number)
echo "<a href='"?page=$page" . (($query_string) ? "&$query_string" : "") . "'">";
echo "$page ";
require_once('inc/database.php');
define("NUMBER_PER_PAGE", 5); //number of records per page of the search results
$page = ($_GET['page']) ? $_GET['page'] : 1;
$start = ($page-1) * NUMBER_PER_PAGE;
$sql = "SELECT * FROM members WHERE 1=1";
$total_records = mysql_num_rows(mysql_query($sql));
//we limit our query to the number of results we want per page
$sql .= " LIMIT $start, " . NUMBER_PER_PAGE;
// we display our pagination at the top of our search results
pagination($page, $total_records, "id=$id&username=$username&email=$email");
$loop = mysql_query($sql)
or die ('cannot run the query because: ' . mysql_error());
while ($record = mysql_fetch_assoc($loop))
echo "<br/>{$record['id']}) " . stripslashes($record['username']) . " - {$record['email']}";
echo "<center>" . number_format($total_records) . " search results found</center>";
if ($page != $current_page_number)
echo "</a>";
$page++;
分页是其中一个已经做了很多次的事情你最好弄清楚如何使用像这样的预制类http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928
你需要做的基本是计算你想在省略号的两边看到多少个链接…然后用循环来创建这些链接。ceil()函数对您来说可能是新的,但它返回的总是四舍五入的分数。
$buffer = 3;
$results_per_page = 5;
$page = ($_GET['page']) ? $_GET['page'] : 1;
$start = ($page-1) * $results_per_page;
$sql = "SELECT * FROM members WHERE 1=1";
$total_records = mysql_num_rows(mysql_query($sql));
$total_pages = ceil(intval($total_records) / $results_per_page);
for ($x = $page - $buffer; $x < $page; $x++){
echo "<a href='"?page=$x" . (($query_string) ? "&$query_string" : "") . "'"> $x</a>";
}
echo " ... ";
for ($x = $total_pages - $buffer; $x <= $total_pages; $x++){
echo "<a href='"?page=$x" . (($query_string) ? "&$query_string" : "") . "'"> $x</a>";
}
这段简短的代码没有考虑到结果集小到足以将所有链接放在一行中。这就是为什么我建议使用已经存在的分页类。