php 分页 - 每页 10 页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3036909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:29:49  来源:igfitidea点击:

pagination - 10 pages per page

phppagination

提问by arthur

I have a pagination script that displays a list of all pages like so:
prev [1][2][3][4][5][6][7][8][9][10][11][12][13][14] next
But I would like to only show ten of the numbers at a time:
prev [3][4][5][6][7][8][9][10][11][12] next

我有一个分页脚本,显示所有页面的列表,如下所示:
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

How can I accomplish this? Here is my code so far:

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

<?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;
?> 

回答by Svisstack

10 next pages

下一页 10

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

or if you want 5 prev and 5 next

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

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

回答by Will

I've just been looking for an answer to the same original question, and couldn't find it, so this is what I came up with. I hope someone else finds it useful.

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

$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 . ']';
}

Results:

结果:

$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] 

回答by Carson Myers

If you just want a quick fix, you might try modifying your forloop a little. For example, you won't want to start at 1, and you won't necessarily want to loop while $i <= $total_pages.

如果您只想快速修复,您可以尝试for稍微修改您的循环。例如,您不想从 1 开始,也不一定要循环 while $i <= $total_pages

Displaying an odd number of pagination links might make more sense: you would display the current page, then four to the left of it, and four to the right. Something like this:

显示奇数个分页链接可能更有意义:您将显示当前页面,然后在它的左侧显示四个,在右侧显示四个。像这样的东西:

for($i = $page_number - 4; $i <= $page_number + 4; $i++) {

but you would obviously need to do a little bit more to ensure you weren't displaying negative numbers, or displaying more links than there are pages.

但您显然需要做更多的工作,以确保您不会显示负数,或显示的链接数量不会超过页面数量。

回答by Awais Mustafa

$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>';
}