我的分页页面链接如下。页面增长如下:
1 2 3 4 5 6 7 ,8,9,10 等等。
但我想限制这一点,所以如果页面超过 5 个,它只会显示 5 个链接,如下所示:
1 2 3 4 5... 97 98 99 其中 99 是最后一页。
如果您转到下一页,它只会更改第一页,如下所示:
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>";
}
这段快速的一小段代码没有考虑到足够小的结果集以将所有链接放入一行。 这就是为什么我建议使用已经存在的分页类。