PHP Paginator(页面导航)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Paginator(页面导航)相关的知识,希望对你有一定的参考价值。
function paginator($items, $perpage = 10, $maxlinks = 10) {
$delitel = $maxlinks;
$totalpages = ceil($items / $perpage);
if(isset($_GET['page']) && is_numeric($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page = 1;
}
if($items != 0 && $page >= 1 && $page <= $totalpages) {
if($totalpages > 0 && $totalpages <= $maxlinks) {
$maxlinks = $totalpages;
$c = 1;
}
else {
$c = $page;
$min = floor($maxlinks /2);
$max = $totalpages - $min;
if(($page > $min) && ($page < $max)) {
$c = $page - $min;
}
elseif($page > $min && $page >= $max) {
$c = $max;
}
else {
$c = 1;
}
}
$link = '';
if(isset($_SERVER['PATH_INFO'])) {
$link .= $_SERVER['PATH_INFO'];
}
$link .= '?';
foreach($_GET as $g => $v) {
if($g != 'page') {
$link .= $g.'='.$v.'&';
}
}
$out = '';
if($totalpages != 1 && $page != 1) {
$out .= "<a class=\"page\" href=\"".$link."page=1\"><span>First page</span></a>";
}
if($page != 1) {
$out .= "<a class=\"page\" href=\"".$link."page=".($page - 1)."\"><span>&laquo;</span></a>";
}
for($i=1; $i <= $maxlinks; $i++) {
if($c <= $totalpages && $totalpages != 1) {
if($c == $page) {
$out .= "<a class=\"current\" href=\"".$link."page=".$c."\"><span>".$c."</span></a>";
}
else {
$out .= "<a class=\"page\" href=\"".$link."page=".$c."\"><span>".$c."</span></a>";
}
$c++;
}
}
if($totalpages == 1) {
$out .= '<a href="#" class="current"><span>Page 1 of 1</span></a>&nbsp;';
}
if($page != $totalpages && $page < $totalpages) {
$out .= "<a class=\"page\" href=\"".$link."page=".($page + 1)."\"><span>&raquo;</span></a>&nbsp;";
}
if($totalpages != 1 && $page != $totalpages) {
$out .= "<a class=\"page\" href=\"".$link."page=".$totalpages."\"><span>Last page</span></a>";
}
}
return $out;
}
以上是关于PHP Paginator(页面导航)的主要内容,如果未能解决你的问题,请参考以下文章
PHP 使Cakephp paginator记住传递的参数
php [cakephp:Paginator示例] CakePHP上PaginatorComponent的示例代码。 #cakephp
Symfony2 KNP PAGINATOR for php 模板
ruby ruhoh的Paginator助手,当有很多帖子时折叠页面链接。
从react-js-pagination获取paginator到页面上显示
导航作为 PHP 中的单独页面