php - 分页 - 每页 10 页

标签 php pagination

我有一个分页脚本,它显示所有页面的列表,如下所示:
上一个 [1][2][3][4][5][6][7][8][9][10][11][12][13][14] 下一个
但我想一次只显示十个数字:
上一个 [3][4][5][6][7][8][9][10][11][12] 下一个

我怎样才能做到这一点?到目前为止,这是我的代码:

<?php
/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */
$max_results = 2;

/* Calculate the offset */
$from = (($page * $max_results) - $max_results);

/* Query the db for total results. 
   You need to edit the sql to fit your needs */
$result = mysql_query("select title from topics");

$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $max_results);

$pagination = '';

/* Create a PREV link if there is one */
if($page > 1)
{
    $pagination .= '< a href="?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
    {
        $pagination .= $i;
    }
    else
    {
        $pagination .= '< a href="index.php?page='.$i.'">'.$i.'</a>';
    }
}

/* Print NEXT link if there is one */
if($page < $total_pages)
{
    $pagination .= '< a hr_ef="?page='.$next.'"> Next</a>';
}

/* Now we have our pagination links in a variable($pagination) ready to
   print to the page. I pu it in a variable because you may want to
   show them at the top and bottom of the page */

/* Below is how you query the db for ONLY the results for the current page */
$result=mysql_query("select * from topics LIMIT $from, $max_results ");

while ($i = mysql_fetch_array($result))
{
    echo $i['title'].'<br />';
}
echo $pagination;
?> 

最佳答案

10 个下页

for($i = $page + 1; $i <= min($page + 11, $total_pages); $i++)

或者如果你想要 5 个上一个和 5 个下一个

for($i = max(1, $page - 5); $i <= min($page + 5, $total_pages); $i++)

关于php - 分页 - 每页 10 页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3036909/

相关文章:

spring - 如何验证 Pageable 的大小?

php - 查询 mssql 数据库分页返回错误

php - Laravel 表单输入整数长度

php - 如果我使用以下内容,正则表达式是否用于表单验证?

c# - 在 .Net 4.0 中,DirectorySearch 能否以允许我翻页的方式返回 LDAP 结果?

grails - Grails中的分页概率

php - 操作后如何在表格上保持相同的分页页面

php - 如何将 url 变量传递到 php 分页中的下一页

php - laravel foreach 破坏了列

PHP 7.2.25 错误!= 错误?