1

可能重复:
分解 PHP 分页链接

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

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

我想把它剪成五个链接,然后为接下来的 5 个链接显示一个按钮,例如:

页:[1] ,[2],[3],[4],[5] [下一个 5]。

当我点击 时[Next 5],它会显示 [Prev 5] [6] ,[7],[8],[9],[10] [Next 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;
4

1 回答 1

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 中做这样的事情,对服务器更好;)

于 2012-12-31T08:35:45.187 回答