voku / pagination
分页,无需数据库依赖
4.0.0
2020-03-20 11:07 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ~6.0|~7.0
README
📖 分页器
分页,无需(数据库)依赖。
通过 "composer require" 安装
composer require voku/pagination
用法
- 包含 composer-autoloader
- 实例化一个新对象,传入每页的项目数量和实例标识符,这用于像 ?p=2 这样的 GET 参数
- 传递 set_total 方法总记录数
- 显示记录
- 调用 page_links 方法来创建导航链接
use voku\helper\Paginator; // include the composer-autoloader require_once __DIR__ . '/vendor/autoload.php'; $pages = new Paginator(10, 'p'); $pages->set_total(100); // or a number of records // display the records here echo $pages->page_links();
如果使用数据库,您可以通过在查询中放置 $pages->get_limit() 来限制记录,这将限制记录数
SELECT * FROM table $pages->get_limit()
默认情况下,page_links 方法创建以 ? 开头的链接,可以通过传递参数到方法来更改此行为
echo $pages->page_links('&');
此方法还允许您传递额外的数据,例如一系列 GET
echo $pages->page_links('?' . 'status=' . $_GET['status'] . '&active=' . $_GET['active'] . '&');
数据库示例
use voku\helper\Paginator; // include the composer-autoloader require_once __DIR__ . '/vendor/autoload.php'; // create new object pass in number of pages and identifier $pages = new Paginator(10, 'p'); // get number of total records $rowCount = $db->query('SELECT count(*) FROM table'); // pass number of records to $pages->set_total($rowCount); $data = $db->query('SELECT * FROM table ' . $pages->get_limit()); foreach($data as $row) { // display the records here } // create the page links echo $pages->page_links();
MVC 示例
在 MVC 环境中使用此类几乎相同,只是数据库或数据集的调用来自模型而不是页面本身。
在控制器中
use voku\helper\Paginator; // create a new object $pages = new Paginator(10, 'p'); // set the total records, calling a method to get the number of records from a model $pages->set_total( $this->_model->get_all_count() ); // calling a method to get the records with the limit set $data['records'] = $this->_model->get_all( $pages->get_limit() ); // create the nav menu $data['page_links'] = $pages->page_links(); // then pass this to the view, may be different depending on the system $this->_view->render('index', $data);
带有数据库的 API 示例
use voku\helper\Paginator; // include the composer-autoloader require_once __DIR__ . '/vendor/autoload.php'; // create new object pass in number of pages and identifier $pages = new Paginator(10, 'p'); // get number of total records $rowCount = $db->query('SELECT COUNT(*) FROM table'); // pass number of records to $pages->set_total($rowCount); $data = $db->query('SELECT * FROM table ' . $pages->get_limit()); foreach($data as $row) { // display the records here } // create the api-call header('Content-Type: application/json'); echo json_encode($pages->page_links_raw());
带有数组(Array)的 API 示例
use voku\helper\Paginator; // include the composer-autoloader require_once __DIR__ . '/vendor/autoload.php'; $page = (int)$_GET['page']; $perPage = (int)$_GET['per_page']; $data = array('some', 'kind', 'of', 'data'); // use the helper-class to reduce the number of pages $result = PaginatorHelper::reduceData($data, $perPage, $page); // create the api-call header('Content-Type: application/json'); echo json_encode($pages->page_links_raw());