PHPackages                             changhorizon/paginator - 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. changhorizon/paginator

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

changhorizon/paginator
======================

Flexible pagination utility for PHP applications

v1.1.2(4w ago)01MITPHPPHP &gt;=8.2CI passing

Since Sep 3Pushed 4w agoCompare

[ Source](https://github.com/changhorizon/paginator)[ Packagist](https://packagist.org/packages/changhorizon/paginator)[ RSS](/packages/changhorizon-paginator/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

Paginator
=========

[](#paginator)

> PHP pagination made easy

[![License](https://camo.githubusercontent.com/9e507147866ed9d6d88d40eaac410d6f227a2da3381f1bb216447f2150fd5dbf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f706167696e61746f723f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/9e507147866ed9d6d88d40eaac410d6f227a2da3381f1bb216447f2150fd5dbf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f706167696e61746f723f7374796c653d666c61742d737175617265)[![Latest Version](https://camo.githubusercontent.com/0adf88c26d213f07d400b735cdddf1a999af435299def0e6ccdf4ce681b6cd68/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f706167696e61746f723f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0adf88c26d213f07d400b735cdddf1a999af435299def0e6ccdf4ce681b6cd68/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f706167696e61746f723f7374796c653d666c61742d737175617265)[![PHP Version](https://camo.githubusercontent.com/10b897c523f00fa3f8f7b54dfe73999190e622480655d5b5f011e31fc32a7111/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e322d2d382e342d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/10b897c523f00fa3f8f7b54dfe73999190e622480655d5b5f011e31fc32a7111/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e322d2d382e342d626c75653f7374796c653d666c61742d737175617265)[![Static Analysis](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)[![Tests](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)[![codecov](https://camo.githubusercontent.com/b463fced40d0bfcf24d92debd3f0a55942fe9fcfa29e3901c892c2716f694295/68747470733a2f2f636f6465636f762e696f2f67682f6368616e67686f72697a6f6e2f706167696e61746f722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/changhorizon/paginator)[![CI](https://github.com/changhorizon/paginator/actions/workflows/ci.yml/badge.svg?style=flat-square)](https://github.com/changhorizon/paginator/actions/workflows/ci.yml/badge.svg?style=flat-square)

Flexible pagination for PHP applications.

✨ 特性
----

[](#-特性)

- Calculate total pages, current page, previous and next pages.
- Generate page numbers for navigation with ellipsis support.
- Provides query limits and offsets for database queries.
- Handles edge cases like first page, last page, and single-page scenarios.
- Fully typed and compatible with PHP 8+.

📦 安装
----

[](#-安装)

```
composer require changhorizon/paginator
```

📂 目录结构
------

[](#-目录结构)

```
src
└── Paginator.php
```

🚀 用法示例
------

[](#-用法示例)

### 示例 1：基础分页导航

[](#示例-1基础分页导航)

```
use ChangHorizon\Paginator\Paginator;

// 假设总共有 120 条记录，每页 10 条，当前在第 5 页
$paginator = new Paginator(totalRows: 120, pageSize: 10, pageNumber: 5);

// 当前页码
echo $paginator->currentPage(); // 5

// 总页数
echo $paginator->totalPages(); // 12

// 上一页 / 下一页
echo $paginator->prevNumber(); // 4
echo $paginator->nextNumber(); // 6

// 页码数组（用于显示分页导航）
print_r($paginator->pageNumbers());
// 输出示例: [1, '…', 4, 5, 6, '…', 12]
```

### 示例 2：结合数据库查询分页

[](#示例-2结合数据库查询分页)

```
use ChangHorizon\Paginator\Paginator;
use PDO;

// 假设数据库有 120 条记录，每页 10 条，当前第 3 页
$totalRows  = 120;
$pageSize   = 10;
$pageNumber = 3;

$paginator = new Paginator($totalRows, $pageSize, $pageNumber);

// PDO 查询示例
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'user', 'pass');
$stmt = $pdo->prepare('SELECT * FROM posts LIMIT :limit OFFSET :offset');
$stmt->bindValue(':limit', $paginator->limitForQuery(), PDO::PARAM_INT);
$stmt->bindValue(':offset', $paginator->offsetForQuery(), PDO::PARAM_INT);
$stmt->execute();

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 打印结果
print_r($rows);
```

🔍 静态分析
------

[](#-静态分析)

使用 PHPStan 工具进行静态分析，确保代码的质量和一致性：

```
composer stan
```

🎯 代码风格
------

[](#-代码风格)

使用 PHP-CS-Fixer 工具检查代码风格：

```
composer cs:chk
```

使用 PHP-CS-Fixer 工具自动修复代码风格问题：

```
composer cs:fix
```

✅ 单元测试
------

[](#-单元测试)

执行 PHPUnit 单元测试：

```
composer test
```

执行 PHPUnit 单元测试并生成代码覆盖率报告：

```
composer test:coverage
```

🤝 贡献指南
------

[](#-贡献指南)

欢迎 Issue 与 PR，建议遵循以下流程：

1. Fork 仓库
2. 创建新分支进行开发
3. 提交 PR 前请确保测试通过、风格一致
4. 提交详细描述

📜 License
---------

[](#-license)

MIT License. See the [LICENSE](LICENSE) file for details.

📐 接口说明
------

[](#-接口说明)

### Paginator

[](#paginator-1)

```
namespace ChangHorizon\Paginator;

class Paginator
{
    public function __construct(int $totalRows, int $pageSize, int $pageNumber = 1);
    public function totalPages(): int;
    public function currentPage(): int;
    public function firstPage(): int;
    public function lastPage(): int;
    public function prevNumber(): ?int;
    public function nextNumber(): ?int;
    public function hasPrev(): bool;
    public function hasNext(): bool;
    public function limitForQuery(): int;
    public function offsetForQuery(): int;
    public function queryRange(): array;
    public function pageNumbers(int $showLength = 7): array;
}
```

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance94

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~83 days

Total

4

Last Release

29d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/191742660?v=4)[ChangHorizon](/maintainers/changhorizon)[@changhorizon](https://github.com/changhorizon)

---

Top Contributors

[![changhorizon](https://avatars.githubusercontent.com/u/191742660?v=4)](https://github.com/changhorizon "changhorizon (8 commits)")

---

Tags

helperutilitypaginatorpaginationnavigationpage numbers

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/changhorizon-paginator/health.svg)

```
[![Health](https://phpackages.com/badges/changhorizon-paginator/health.svg)](https://phpackages.com/packages/changhorizon-paginator)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.2k417.9M1.6k](/packages/nette-utils)[jasongrimes/paginator

A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.

4091.3M21](/packages/jasongrimes-paginator)[beberlei/porpaginas

Library that generically solves several pagination issues with DAO/repository abstractions.

164632.5k13](/packages/beberlei-porpaginas)[bocharsky-bw/arrayzy

A native PHP arrays easy manipulation library in OOP way.

38427.0k](/packages/bocharsky-bw-arrayzy)[coffeecode/paginator

Paginator is simple and is ready to generate results navigation in your application

12659.7k1](/packages/coffeecode-paginator)[stefangabos/zebra_pagination

A generic, Twitter Bootstrap compatible, PHP pagination library that automatically generates navigation links

11923.6k](/packages/stefangabos-zebra-pagination)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
