使用composer安装
composer require "jasongrimes/paginator:~1.0"
使用
<?php
require '../vendor/autoload.php';
use JasonGrimes\Paginator;
$totalItems = 1000;
$itemsPerPage = 50;
$currentPage = 8;
$urlPattern = '/foo/page/(:num)';
$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);
?>
<html>
<head>
<!-- The default, built-in template supports the Twitter Bootstrap pagination styles. -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<?php
// Example of rendering the pagination control with the built-in template.
// See below for information about using other templates or custom rendering.
echo $paginator;
?>
</body>
</html>
或者自定义显示格式
<?php if ($paginator->getNumPages() > 1): ?>
<ul class="pagination">
<?php if ($paginator->getPrevUrl()): ?>
<li><a href="<?php echo $paginator->getPrevUrl(); ?>">« Previous</a></li>
<?php endif; ?>
<?php foreach ($paginator->getPages() as $page): ?>
<?php if ($page['url']): ?>
<li <?php echo $page['isCurrent'] ? 'class="active"' : ''; ?>>
<a href="<?php echo $page['url']; ?>"><?php echo $page['num']; ?></a>
</li>
<?php else: ?>
<li class="disabled"><span><?php echo $page['num']; ?></span></li>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($paginator->getNextUrl()): ?>
<li><a href="<?php echo $paginator->getNextUrl(); ?>">Next »</a></li>
<?php endif; ?>
</ul>
<?php endif; ?>
跳转页参数问题
$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);
上述变量中$urlPattern
格式一般为:'/foo/page/(:num)'
如果参数为多个,则可以通过拼接参数来生成$urlPattern
,如下
/**
* 将url数组参数转为url字符串
* @param array $params
* @return string
*/
private function buildUrlStr(array $params) :string
{
$str = '?';
foreach ($params as $p_k =>$p_v) {
if ($p_k == 'page') {
// 'page'代表当前所在页
$str .= "$p_k=(:num)&";
} else {
$str .= "$p_k=$p_v&";
}
}
return $str;
}
然后将$urlPattern
中的(:num)
替换为该方法,即'/foo/page/' . $this->buildUrlStr($params)
github地址
网友评论