nikserg / pagination

分页,无需数据库依赖

4.0.1 2021-04-14 08:28 UTC

README

Build Status codecov.io Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

📖 分页器

分页,无需(数据库)依赖。

使用 "composer require" 安装

composer require voku/pagination

用法

  1. 包含 composer-autoloader
  2. 创建一个新对象,传入每页的项目数和实例标识符,这用于类似 ?p=2 的 GET 参数
  3. 传入 set_total 方法总记录数
  4. 显示记录
  5. 调用 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());

带数组的 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());