PHPackages                             yidas/pagination - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yidas/pagination

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yidas/pagination
================

PHP Paginator with Pager Widget (pure PHP, CI, Yii, Laravel support)

1.1.0(2y ago)2516.4k↓15.2%3[1 PRs](https://github.com/yidas/php-pagination/pulls)MITPHPPHP &gt;=5.4.0

Since Oct 13Pushed 6mo ago2 watchersCompare

[ Source](https://github.com/yidas/php-pagination)[ Packagist](https://packagist.org/packages/yidas/pagination)[ Docs](https://github.com/yidas/php-pagination)[ RSS](/packages/yidas-pagination/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)DependenciesVersions (9)Used By (0)

 [ ![](https://camo.githubusercontent.com/703a3ff7b5b3b598396dbb36bd62a2ed58a06175d7acf428d20a0c56cb8b5b6b/68747470733a2f2f7777772e7068702e6e65742f696d616765732f6c6f676f732f7068702d6c6f676f2d77686974652e737667) ](https://www.php.net/)

*php* Pagination
================

[](#php-pagination)

PHP Paginator with Pager Widget (pure PHP, CI, Yii, Laravel support)

[![Latest Stable Version](https://camo.githubusercontent.com/7be9b60acde5a6670778e3a851e48af085cc679853a9d70602409ee5d8d8a7a8/68747470733a2f2f706f7365722e707567782e6f72672f79696461732f706167696e6174696f6e2f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/yidas/pagination)[![License](https://camo.githubusercontent.com/d02b7106ee06d3c52922a845e124b5a81927fa61049f1fcccffb7850d48d6580/68747470733a2f2f706f7365722e707567782e6f72672f79696461732f706167696e6174696f6e2f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/yidas/pagination)

Features
--------

[](#features)

- *Compatible with pure PHP, Codeigniter, Yii &amp; Laravel*
- ***SOLID principle** with Yii 2 pattern like*
- ***Pagination Widget** (View Block) included*

---

OUTLINE
-------

[](#outline)

- [Demonstration](#demonstration)
- [Requirements](#requirements)
    - [PDO with pure PHP](#pdo-with-pure-php)
    - [Codeiginter 3 Framework](#codeiginter-3-framework)
    - [Widget Render](#widget-render)
- [Installation](#installation)
    - [Codeigniter 3](#codeigniter-3)
- [Configuration](#configuration)
    - [Inheritance](#inheritance)
- [Usage](#usage)
    - [Widget](#widget)
        - [Customized View](#customized-view)
        - [Inheritance](#inheritance-1)
    - [Build URL](#build-url)
- [API Documentation](#api-documentation)
- [Examples](#examples)
    - [PDO with pure PHP](#pdo-with-pure-php-1)
    - [Codeiginter 3 Framework](#codeiginter-3-framework-1)
- [References](#references)

---

DEMONSTRATION
-------------

[](#demonstration)

### PDO with pure PHP

[](#pdo-with-pure-php)

```
// 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();
```

### Codeiginter 3 Framework

[](#codeiginter-3-framework)

```
$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();
```

### Widget Render

[](#widget-render)

```

```

[![](https://raw.githubusercontent.com/yidas/php-pagination/master/img/widget.png)](https://raw.githubusercontent.com/yidas/php-pagination/master/img/widget.png)

> `$pagination` is the object of `yidas\data\Pagination`.

---

REQUIREMENTS
------------

[](#requirements)

This library requires the following:

- PHP 5.4.0+

---

INSTALLATION
------------

[](#installation)

Run Composer in your project:

```
composer require yidas/pagination

```

Then initialize it at the bootstrap of application such as config file:

```
require __DIR__ . '/vendor/autoload.php';
```

### Codeigniter 3

[](#codeigniter-3)

Run Composer in your Codeigniter project under the folder `\application`:

```
composer require yidas/pagination

```

Check Codeigniter `application/config/config.php`:

```
$config['composer_autoload'] = TRUE;
```

> You could customize the vendor path into `$config['composer_autoload']`

---

CONFIGURATION
-------------

[](#configuration)

The simple config and usage could refer to [Demonstration](#demonstration).

When you are dealing with pagination, you could new `yidas\data\Pagination` with configuration to get pager information for data query. For example:

```
// 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
```

For more parameters, you could refer to [API Documentation](#api-documentation).

### Inheritance

[](#inheritance)

You could build your application data Pagination with styles Inherited from `yidas\data\Pagination`. For example:

```
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;
}
```

---

USAGE
-----

[](#usage)

When there are too much data to be displayed on a single page, a common strategy is to display them in multiple pages and on each page only display a small portion of the data. This strategy is known as *pagination*.

This library uses a `yidas\data\Pagination` object to represent the information about a pagination scheme. In particular,

- `total count` specifies the total number of data items. Note that this is usually much more than the number of data items needed to display on a single page.
- `page size` specifies how many data items each page contains. The default value is 20.
- `current page` gives the current page number (not zero-based). The default value is 1, meaning the first page.

With a fully specified `yidas\data\Pagination object`, you can retrieve and display data partially. For example, if you are fetching data from a database, you can specify the `OFFSET` and `LIMIT` clause of the DB query with the corresponding values provided by the pagination. Below is an example:

```
/**
 * 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();
```

### Widget

[](#widget)

To facilitate building the UI element that supports pagination, This library provides the `yii\widgets\Pagination` widget that displays a list of page buttons upon which users can click to indicate which page of data should be displayed. The widget takes a pagination object so that it knows what is the current page and how many page buttons should be displayed. For example,

```
use yidas\widgets\Pagination;

echo  Pagination::widget([
    'pagination' => $pagination
]);
```

> `$pagination` is a `yidas\data\Pagination` object for data provider.

#### Customized View

[](#customized-view)

The default widget view is for Bootstrap(`bootstrap`), you could choose a template view for your Pagination Widget:

```
echo  \yidas\widgets\Pagination::widget([
    'pagination' => $pagination,
    'view' => 'simple',
]);
```

TemplateDescriptionbootstrapDefault view, supports for Bootstrap 3 and 4simpleSimple `` with `` tags for pure HTML/CSS styleYou can also use your customized view for Pagination widget:

```
echo  \yidas\widgets\Pagination::widget([
    'pagination' => $pagination,
    'view' => __DIR__ . '/../widgets/pagination_view.php',
]);
```

#### Inheritance

[](#inheritance-1)

You could build your application Pagination Widget with styles Inherited from `yidas\widgets\Pagination`. For example:

```
