Data 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 browsable pages. Data Adapters ------------- This component makes use of adapters to encapsulate different sources of data: +--------------+------------------------------------------------------------+ | Adapter | Description | +==============+============================================================+ | NativeArray | Use a PHP array as source data | +--------------+------------------------------------------------------------+ | Model | Use a Phalcon\\Mvc\\Model\\Resultset object as source data | +--------------+------------------------------------------------------------+ Using Paginators ---------------- In the example below, the paginator will use as its source data the result of a query from a model, and limit the displayed data to 10 records per page: .. code-block:: php 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 \Phalcon\Paginator\Adapter\Model( array( "data" => $robots, "limit"=> 10, "page" => $currentPage ) ); // Get the paginated results $page = $paginator->getPaginate(); Variable $currentPage 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: .. code-block:: html+php items as $item) { ?>
Id Name Type
id; ?> name; ?> type; ?>
The $page object also contains navigation data: .. code-block:: html+php First Previous Next Last current, " of ", $page->total_pages; ?> Page Attributes --------------- The $page object has the following attributes: +---------+--------------------------------------------------------+ | Adapter | Description | +=========+========================================================+ | items | The set of records to be displayed at 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 | +---------+--------------------------------------------------------+ Implementing your own adapters ------------------------------ The :doc:`Phalcon\\Paginator\\AdapterInterface <../api/Phalcon_Paginator_AdapterInterface>` interface must be implemented in order to create your own paginator adapters or extend the existing ones: .. code-block:: php