# 分頁(Pagination)
The process of pagination takes place when we need to present big groups of arbitrary data gradually.`Phalcon\Paginator`offers a fast and convenient way to split these sets of data into browsable pages.
## 數據適配器(Data Adapters)
This component makes use of adapters to encapsulate different sources of data:
| Adapter | Description |
| --- | --- |
| [Phalcon\\Paginator\\Adapter\\NativeArray](http://docs.iphalcon.cn/api/Phalcon_Paginator_Adapter_NativeArray.html) | Use a PHP array as source data |
| [Phalcon\\Paginator\\Adapter\\Model](http://docs.iphalcon.cn/api/Phalcon_Paginator_Adapter_Model.html) | Use a[Phalcon\\Mvc\\Model\\Resultset](http://docs.iphalcon.cn/api/Phalcon_Mvc_Model_Resultset.html)object as source data. Since PDO doesn’t support scrollable cursors this adapter shouldn’t be used to paginate a large number of records |
| [Phalcon\\Paginator\\Adapter\\QueryBuilder](http://docs.iphalcon.cn/api/Phalcon_Paginator_Adapter_QueryBuilder.html) | Use a[Phalcon\\Mvc\\Model\\Query\\Builder](http://docs.iphalcon.cn/api/Phalcon_Mvc_Model_Query_Builder.html)object as source data |
## 示例(Examples)
In the example below, the paginator will use the result of a query from a model as its source data, and limit the displayed data to 10 records per page:
~~~
<?php
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
// Current page to show
// In a controller/component this can be:
// $this->request->getQuery("page", "int"); // GET
// $this->request->getPost("page", "int"); // POST
$currentPage = (int) $_GET["page"];
// The data set to paginate
$robots = Robots::find();
// Create a Model paginator, show 10 rows by page starting from $currentPage
$paginator = new PaginatorModel(
[
"data" => $robots,
"limit" => 10,
"page" => $currentPage,
]
);
// Get the paginated results
$page = $paginator->getPaginate();
~~~
The`$currentPage`variable controls the page to be displayed. The`$paginator->getPaginate()`returns a`$page`object that contains the paginated data. It can be used for generating the pagination:
~~~
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Type</th>
</tr>
<?php foreach ($page->items as $item) { ?>
<tr>
<td><?php echo $item->id; ?></td>
<td><?php echo $item->name; ?></td>
<td><?php echo $item->type; ?></td>
</tr>
<?php } ?>
</table>
~~~
The`$page`object also contains navigation data:
~~~
<a href="/robots/search">First</a>
<a href="/robots/search?page=<?= $page->before; ?>">Previous</a>
<a href="/robots/search?page=<?= $page->next; ?>">Next</a>
<a href="/robots/search?page=<?= $page->last; ?>">Last</a>
<?php echo "You are in page ", $page->current, " of ", $page->total_pages; ?>
~~~
## 適配器使用(Adapters Usage)
An example of the source data that must be used for each adapter:
~~~
<?php
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
use Phalcon\Paginator\Adapter\NativeArray as PaginatorArray;
use Phalcon\Paginator\Adapter\QueryBuilder as PaginatorQueryBuilder;
// Passing a resultset as data
$paginator = new PaginatorModel(
[
"data" => Products::find(),
"limit" => 10,
"page" => $currentPage,
]
);
// Passing an array as data
$paginator = new PaginatorArray(
[
"data" => [
["id" => 1, "name" => "Artichoke"],
["id" => 2, "name" => "Carrots"],
["id" => 3, "name" => "Beet"],
["id" => 4, "name" => "Lettuce"],
["id" => 5, "name" => ""],
],
"limit" => 2,
"page" => $currentPage,
]
);
// Passing a QueryBuilder as data
$builder = $this->modelsManager->createBuilder()
->columns("id, name")
->from("Robots")
->orderBy("name");
$paginator = new PaginatorQueryBuilder(
[
"builder" => $builder,
"limit" => 20,
"page" => 1,
]
);
~~~
## 頁面屬性(Page Attributes)
The`$page`object has the following attributes:
| Attribute | Description |
| --- | --- |
| items | The set of records to be displayed at the current page |
| current | The current page |
| before | The previous page to the current one |
| next | The next page to the current one |
| last | The last page in the set of records |
| total\_pages | The number of pages |
| total\_items | The number of items in the source data |
## 自定義適配器(Implementing your own adapters)
The[Phalcon\\Paginator\\AdapterInterface](http://docs.iphalcon.cn/api/Phalcon_Paginator_AdapterInterface.html)interface must be implemented in order to create your own paginator adapters or extend the existing ones:
~~~
<?php
use Phalcon\Paginator\AdapterInterface as PaginatorInterface;
class MyPaginator implements PaginatorInterface
{
/**
* Adapter constructor
*
* @param array $config
*/
public function __construct($config);
/**
* Set the current page number
*
* @param int $page
*/
public function setCurrentPage($page);
/**
* Returns a slice of the resultset to show in the pagination
*
* @return stdClass
*/
public function getPaginate();
}
~~~
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl