Break Page链接最多5个

时间:2012-12-31 08:23:03

标签: php

  

可能重复:
  Break up PHP Pagination links

我在网页上使用了一个简单的分页,它正在运行。但是,我的问题是,当分页链接达到很多数量时,如

Page:[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[ 12],[13],[14],[15]等。

我想将其剪切为五个链接,然后显示下一个5个链接的按钮,如:

Page: [1] ,[2],[3],[4],[5] [Next 5]。

当我点击[Next 5]时,会显示[上一页5] [6] ,[7],[8],[9],[10] [下一页5]当前页面位于第6页。

(粗体字符表示当前页面。)

以下是要修改的代码:

$perpage = 10;

if(isset($_GET["pagenum"])) {
  $page = intval($_GET["pagenum"]);
} else {
  $page = 1;
}
$calc = $perpage * $page;
$start = $calc - $perpage;

$orders_count = 150;
$rowss = $orders_count;

if($rowss) {
   $total = $orders_count;
}

$totalPages = ceil($total / $perpage);

if($page <=1 ) {
  $feedbacks .= "";
} else {
  $j = $page - 1;
  $feedbacks .= "<a class='first-page' title='Go to the first page' href='" . $path . "&pagenum=$j'>&laquo;</a>";
}

for($i=1; $i <= $totalPages; $i++) {
  if($i<>$page) {
     $feedbacks .= "<a class='first-page' title='Go to the first page' href='" . $path . "&pagenum=$i'>$i</a>";
  } else {
     $feedbacks .= "<a class='first-page disabled' title='Go to the first page' href='#'>$i</a>";
  }
}

if($page == $totalPages ) {
  $feedbacks .= "";
} else {
  $j = $page + 1;
  $feedbacks .= "<a class='last-page' title='Go to the first page' href='" . $path . "&pagenum=$j'>&raquo;</a></span>";
}

$feedbacks .= "</div>";
echo $feedbacks;

1 个答案:

答案 0 :(得分:1)

这是一个有趣的问题:)

希望它有所帮助。

<?php
//set params
$total = 283;
$maxPerPage = 10;
$cutStarting = 5;
$totalPages = ceil($total / $maxPerPage);
$prevPageNum = 0;

//set currentPageId
$currentPage = isset($_GET['pagenum']) ? intval($_GET['pagenum']) : 1;

if ($currentPage > $totalPages && $currentPage < 1) {
    $currentPage = 1;
}

//set first row (maybee for limits in mysql query)
$firstRow = ($currentPage * $maxPerPage) - ($maxPerPage) + 1;

//calculate prev link pageId
if ($currentPage > $cutStarting) {
    $prevPageNum = floor(($currentPage - 1) / $cutStarting) * $cutStarting;
    echo '[ <a href="?pagenum='.$prevPageNum.'">Prev 5</a> ] ';
}

//generate number page links
$links = '';
$first = ($prevPageNum + 1);
$last = $first + $cutStarting;

for ($i=($prevPageNum + 1); $i < $last; $i++) {
    if ($i <= $totalPages) {
        if ($currentPage == $i) {
            $links .= '[ <b>'.$i.'</b> ] ';
        } else {
            $links .= '[ <a href="?pagenum='.$i.'">'.$i.'</a> ] ';
        }
    }
}

echo ' '.$links;

//calculate next link pageId
$nextPageNum = ( ceil( $currentPage / $cutStarting ) * $cutStarting ) + 1;
if ($nextPageNum <= $totalPages) {
    echo '[ <a href="?pagenum='.$nextPageNum.'">Next 5</a> ] ';
}


//get some infos for testing
echo '<br /><br />Testing<hr>';
echo 'Total Pages: '.$totalPages.'<br />';
echo 'Current Page: '.$currentPage.'<br />';
echo 'First Row: '.$firstRow;
?>

注意:在javascipt中执行此类操作,对服务器更好;)