首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分页-每页10页

分页-每页10页
EN

Stack Overflow用户
提问于 2010-06-14 19:43:01
回答 3查看 28.9K关注 0票数 11

我有一个分页脚本,它显示所有页面的列表,如下所示:

prev [1][2][3][4][5][6][7][8][9][10][11][12][13][14] next

但我想一次只显示其中的10个数字:

prev [3][4][5][6][7][8][9][10][11][12] next

我如何才能做到这一点呢?到目前为止,我的代码如下:

代码语言:javascript
复制
<?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;
?> 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-06-14 19:57:45

10下一页

代码语言:javascript
复制
for($i = $page + 1; $i <= min($page + 11, $total_pages); $i++)

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

代码语言:javascript
复制
for($i = max(1, $page - 5); $i <= min($page + 5, $total_pages); $i++)
票数 32
EN

Stack Overflow用户

发布于 2017-10-27 06:46:36

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

代码语言:javascript
复制
$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 . ']';
}

结果:

代码语言:javascript
复制
$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] 
票数 5
EN

Stack Overflow用户

发布于 2017-09-20 23:09:17

代码语言:javascript
复制
$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>';
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3036909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档