How to use Phalcon's built-in pagination functionality?

by raven_corwin , in category: PHP Frameworks , a year ago

How to use Phalcon's built-in pagination functionality?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , a year ago


To use Phalcon's built-in pagination functionality:

  1. Initialize the Paginator object by creating an instance of the PhalconPaginatorAdapterModel class. use PhalconPaginatorAdapterModel as PaginatorModel; $paginator = new PaginatorModel( [ "data" => YourModel::find(), "limit" => 10, "page" => $currentPage, ] );
  2. Set the data to be paginated by using the data key. In the above example, the YourModel::find() method is used to fetch the data to be paginated.
  3. Set the limit of data to be displayed per page using the limit key.
  4. Set the current page using the page key.
  5. Fetch the paginated data using the getPaginate() method of the Paginator object. $page = $paginator->getPaginate();
  6. Loop through the paginated data to display it on your view. tag->linkTo(['page=' . $page->before, 'Previous']) ?> tag->linkTo(['page=' . $page->next, 'Next']) ?> items as $item) { ?> Field 1 Field 2 Field 3 field1 ?> field2 ?> field3 ?>
  7. Display the pagination links using the before and next properties of the getPaginate() method.

Note: getPaginate() returns an object that contains the following properties:

  • before: The previous page number, or NULL if there is no previous page.
  • next: The next page number, or NULL if there is no next page.
  • last: The last page number.
  • current: The current page number.
  • total_pages: The total number of pages.
  • total_items: The total number of items.