yidas / 分页器
PHP 分页器,支持 Pager 小部件(纯PHP,CI,Yii,Laravel)
Requires
- php: >=5.4.0
README
PHP 分页器,支持 Pager 小部件(纯PHP,CI,Yii,Laravel)
特性
-
兼容纯PHP,Codeigniter,Yii & Laravel
-
SOLID原则,采用Yii 2模式
-
分页小部件(视图块)包含
概要
演示
纯PHP的PDO
// Get count of data set first $sql = "SELECT count(*) FROM `table`"; $count = $conn->query($sql)->fetchColumn(); // Initialize a Data Pagination with previous count number $pagination = new \yidas\data\Pagination([ 'totalCount' => $count, ]); // Get range data for the current page $sql = "SELECT * FROM `table` LIMIT {$pagination->offset}, {$pagination->limit}"; $sth = $conn->prepare($sql); $sth->execute(); $data = $sth->fetchAll();
Codeigniter 3框架
$query = $this->db->where('type', 'C'); // Clone same query for get total count $countQuery = clone $query; // Get total count from cloned query // Or you could use count_all_results('', false) to keep query instead of using `clone` $count = $countQuery->count_all_results(); // Initialize a Data Pagination with previous count number $pagination = new \yidas\data\Pagination([ 'totalCount' => $count, ]); // Get range data for the current page $records = $query ->offset($pagination->offset) ->limit($pagination->limit) ->get()->result_array();
小部件渲染
<div> <?=\yidas\widgets\Pagination::widget([ 'pagination' => $pagination ])?> </div>
$pagination
是yidas\data\Pagination
的对象。
要求
此库需要以下内容
- PHP 5.4.0+
安装
在您的项目中运行Composer
composer require yidas/pagination
然后在应用的引导(如配置文件)中初始化它
require __DIR__ . '/vendor/autoload.php';
Codeigniter 3
在您的Codeigniter项目中运行Composer,在 \application
文件夹下
composer require yidas/pagination
检查Codeigniter的 application/config/config.php
$config['composer_autoload'] = TRUE;
您可以将供应商路径自定义到
$config['composer_autoload']
配置
简单的配置和用法可以参考演示。
当你处理分页时,你可以用配置初始化 yidas\data\Pagination
以获取数据查询的分页信息。例如
// Get total rows from your query $count = $query->count(); // Initialize a Data Pagination $pagination = new \yidas\data\Pagination([ 'totalCount' => $count, 'pergpage' => 10, ]); // ...use $pagination offset/limit info for your query
更多参数可以参考API文档。
继承
你可以使用从 yidas\data\Pagination
继承样式的样式来构建你的应用数据分页。例如
namespace yidas\data; use yidas\data\Pagination as BasePagination; class Pagination extends BasePagination { // Name of the parameter storing the current page index public $pageParam = 'page'; // The number of items per page public $perPage = 10; // Name of the parameter storing the page size // false to turn off per-page input by client public $perPageParam = false; }
用法
当单页显示的数据量过大时,一种常见的策略是将它们分页显示,每页只显示一小部分数据。这种策略被称为 分页。
此库使用 yidas\data\Pagination
对象来表示分页方案的信息。特别是,
总数
指定数据项的总数。请注意,这通常比单页需要显示的数据项数量多得多。每页大小
指定每页包含的数据项数量。默认值为20。当前页
给出当前页码(非基于0)。默认值为1,表示第一页。
使用完全指定的 yidas\data\Pagination
对象,您可以检索和显示部分数据。例如,如果您正在从数据库获取数据,您可以使用分页提供的相应值指定DB查询的 OFFSET
和 LIMIT
子句。以下是一个示例
/** * Yii 2 Framework sample code */ use yidas\data\Pagination; // build a DB query to get all articles with status = 1 $query = Article::find()->where(['status' => 1]); // get the total number of articles (but do not fetch the article data yet) $count = $query->count(); // create a pagination object with the total count $pagination = new Pagination(['totalCount' => $count]); // limit the query using the pagination and retrieve the articles $articles = $query->offset($pagination->offset) ->limit($pagination->limit) ->all();
小部件
为了方便构建支持分页的UI元素,此库提供了 yii\widgets\Pagination
小部件,该小部件在用户可以点击以指示应显示哪些页数据的一组页面按钮上显示列表。该小部件接受一个分页对象,以便它知道当前页和应显示多少页面按钮。例如,
use yidas\widgets\Pagination; echo Pagination::widget([ 'pagination' => $pagination ]);
$pagination
是数据提供者的yidas\data\Pagination
对象。
自定义视图
默认小部件视图是Bootstrap(bootstrap
),您可以为分页小部件选择模板视图
echo \yidas\widgets\Pagination::widget([ 'pagination' => $pagination, 'view' => 'simple', ]);
您还可以为分页小部件使用自定义视图
echo \yidas\widgets\Pagination::widget([ 'pagination' => $pagination, 'view' => __DIR__ . '/../widgets/pagination_view.php', ]);
继承
您可以使用从 yidas\widgets\Pagination
继承样式的样式来构建您的应用分页小部件。例如
<?php namespace app\widgets; use yidas\widgets\Pagination as BaseWidget; /** * Pagination Widget */ class Pagination extends BaseWidget { // Set the Widget pager is center align or not public $alignCenter = false; // Maximum number of page buttons that can be displayed public $buttonCount = 7; // The text label for the "first" page button public $firstPageLabel = '<i class="fa fa-step-backward" aria-hidden="true"></i>'; // The text label for the "last" page button public $lastPageLabel = '<i class="fa fa-step-forward" aria-hidden="true"></i>'; // The text label for the "next" page button public $nextPageLabel = '<i class="fa fa-caret-right" aria-hidden="true"></i>'; // The text label for the "previous" page button public $prevPageLabel = '<i class="fa fa-caret-left" aria-hidden="true"></i>'; // <ul> class. For example, 'pagination-sm' for Bootstrap small size. public $ulCssClass = ''; }
构建URL
如果您想手动构建UI元素,可以使用yidas\data\Pagination::createUrl()
来创建指向不同页面的URL。此方法需要一个页面参数,并将创建一个包含页面参数的格式化URL。例如
// ex. https://yoursite.com/list/ // displays: https://yoursite.com/list/?page=100 echo $pagination->createUrl(100); // ex. https://yoursite.com/list/?sort=desc&type=a // displays: https://yoursite.com/list/?sort=desc&type=a&page=101 echo $pagination->createUrl(101);
格式化的URL模式为
//{current-host-uri}{parameters-with-pagination}
您还可以为更改设置per-page
的perPageParam
创建一个per-page
设置URL
// ex. https://yoursite.com/list/ // displays: https://yoursite.com/list/?page=1&per-page=50 echo $pagination->createUrl(1, 50);
示例
PDO与纯PHP
$conn = new PDO("mysql:host=localhost;dbname=database", 'username', 'password'); // Get count of data set first $sql = "SELECT count(*) FROM `table`"; $count = $conn->query($sql)->fetchColumn(); // Initialize a Data Pagination with previous count number $pagination = new \yidas\data\Pagination([ 'totalCount' => $count, ]); // Get range data for the current page $sql = "SELECT * FROM `table` LIMIT {$pagination->offset}, {$pagination->limit}"; $sth = $conn->prepare($sql); $sth->execute(); $data = $sth->fetchAll(); print_r($data);
LinkPager显示
echo yidas\widgets\Pagination::widget([ 'pagination' => $pagination ]);
Codeigniter 3框架
Codeigniter 3框架与yidas/codeigniter-model
$this->load->model('Post_model'); $query = $this->Post_model->find() ->where('type', 'C'); // Clone same query for get total count $countQuery = clone $query; // Get total count from cloned query // Or you could use count(false) to keep query instead of using `clone` $count = $countQuery->count(); // Initialize a Data Pagination with previous count number $pagination = new \yidas\data\Pagination([ 'totalCount' => $count, ]); // Get range data for the current page $records = $query ->offset($pagination->offset) ->limit($pagination->limit) ->get()->result_array();
LinkPager在视图中
<div> <?=yidas\widgets\Pagination::widget([ 'pagination' => $pagination ])?> </div>