【问题标题】:pagination - 10 pages per page分页 - 每页 10 页
【发布时间】:2010-06-14 11:43:01
【问题描述】:

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

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

<?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 条记录后自动移动页面。你需要 JS 你知道
  • 为什么你的href中间有一个“_”
  • 因为这是我第一次在这里发帖,第一次发帖的时候是在喊链接限制
  • 我已为您修复了 href,但我不愿意再进行编辑,以免这个问题落入社区 wiki 的厄运深渊。我认为它大喊链接限制的原因是因为您的代码格式不正确,所以它认为您实际上是在尝试在帖子中添加链接。

标签: php pagination


【解决方案1】:

接下来的 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++)

【讨论】:

  • 感谢您的快速回答,这真的很有帮助.. 没想到会这么快! :)
  • 啊.. 不知道接受的事情。只是最后一个问题..如何始终显示恒定数量的页面(数字)。 - 打开时显示第1-6页,每次按next时增加1-1-7; 1-8; 1-9;并关闭到最后它会一一删除...
  • @arthur: 使用第二个,如果你在第 5 页,它就像 1-10,如果你点击下一步,你将进入第 6 页并有 2-11,在第 7 页你有 3-12 个。您可以通过点击我帖子左侧投票数下方的标记来接受问题。
  • 我这样做了,谢谢你,但我只是想知道如何让它在开头显示 10 页(在第一页),在中间显示 10 页(在第 6 或第 7 页) 和最后一页的 10 页.. 无论如何谢谢!
【解决方案2】:

我一直在寻找同一个原始问题的答案,但找不到,所以这就是我想出的。我希望其他人觉得它有用。

$totalPages  = 20;
$currentPage = 1;

if ($totalPages <= 10) {
    $start = 1;
    $end   = $totalPages;
} else {
    $start = max(1, ($currentPage - 4));
    $end   = min($totalPages, ($currentPage + 5));

    if ($start === 1) {
        $end = 10;
    } elseif ($end === $totalPages) {
        $start = ($totalPages - 9);
    }
}

for ($page = $start; $page <= $end; $page++) {
    echo '[' . $page . ']';
}

结果:

$currentPage = 1;  // [1][2][3][4][5][6][7][8][9][10]
$currentPage = 4;  // [1][2][3][4][5][6][7][8][9][10]
$currentPage = 10; // [6][7][8][9][10][11][12][13][14][15]
$currentPage = 17; // [11][12][13][14][15][16][17][18][19][20]
$currentPage = 20; // [11][12][13][14][15][16][17][18][19][20] 

【讨论】:

    【解决方案3】:
    $page = 3;
    $totalPages = 33;
    $count = 9;
    $startPage = max(1, $page - $count);
    $endPage = min( $totalPages, $page + $count);
    
    if($page-1 > 0){
        echo '<a class="btn btn-default" href="/search-results?page="'.($page-
    1).'"><< Prev</a>';
    } 
    
    for($i = $startPage; $i < $endPage; $i++): if($i <= $totalPages):
    
        echo '<a class="btn btn-<?=$i == $page || $i == 1 && $page == "" ? 
        'success' : 'primary';?>"style="margin-right:2px;" href="/search-
        results?page="'.$i.'">'.$i.'</a>';
    
    endif; endfor;
    if($page < $totalPages){
        echo '<a class="btn btn-default" href="/search-results?page="'.
        ($page+1).'">Next >></a>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多