- CakePHP - Discussion
- CakePHP - Useful Resources
- CakePHP - Quick Guide
- CakePHP - File upload
- CakePHP - Date and Time
- CakePHP - Pagination
- CakePHP - Creating Validators
- CakePHP - Validation
- CakePHP - Security
- CakePHP - Cookie Management
- CakePHP - Session Management
- CakePHP - Internationalization
- CakePHP - Form Handling
- CakePHP - Logging
- CakePHP - Errors & Exception Handling
- CakePHP - Services
- CakePHP - Delete a Record
- CakePHP - Update a Record
- CakePHP - View a Record
- CakePHP - Working with Database
- CakePHP - View Events
- CakePHP - View Elements
- CakePHP - Extending Views
- CakePHP - Views
- CakePHP - Controllers
- CakePHP - Routing
- CakePHP - Project Configuration
- CakePHP - Folder Structure
- CakePHP - Installation
- CakePHP - Overview
- CakePHP - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CakePHP - Pagination
If we want to show a set of data that is huge, we can use pagination and this feature is available with cake php 4 which is very easy to use.
We have a table titled “articles” with following data −
Let us use pagination to display the data in the form of pages, instead of showing them all together.
Example
Make Changes in the config/routes.php file as shown in the following program.
config/routes.php
<?php use CakeHttpMiddlewareCsrfProtectionMiddleware; use CakeRoutingRouteDashedRoute; use CakeRoutingRouteBuilder; $routes->setRouteClass(DashedRoute::class); $routes->scope( / , function (RouteBuilder $builder) { $builder->registerMiddleware( csrf , new CsrfProtectionMiddleware([ httpOnly => true, ])); $builder->applyMiddleware( csrf ); //$builder->connect( /pages ,[ controller => Pages , action => display , home ]); $builder->connect( posts ,[ controller => Posts , action => index ]); $builder->fallbacks(); });
Create a PostsController.php file at src/Controller/PostsController.php. Copy the following code in the controller file. Ignore, if already created.
src/Controller/PostsController.php
<?php namespace AppController; use AppControllerAppController; class PostsController extends AppController { pubpc function index(){ $this->loadModel( articles ); $articles = $this->articles->find( all )->order([ articles.id ASC ]); $this->set( articles , $this->paginate($articles, [ pmit => 3 ])); } } ?>
The data from articles table is fetched using −
$this->loadModel( articles ); $articles = $this->articles->find( all )->order([ articles.id ASC ]);
To apply pagination and we would show the data with 3 per records and the same is done as follows −
$this->set( articles , $this->paginate($articles, [ pmit => 3 ]));
This is enough to activate pagination on the articles tables.
Create a directory Posts at src/Template and under that directory create a Viewfile called index.php. Copy the following code in that file.
src/Template/Posts/index.php
<span> <?php foreach ($articles as $key=>$article) {?> <a href="#"> <span> <p><?= $article->title ?> </p> <p><?= $article->details ?></p> </span> </a> <br/> <?php } ?> <ul class="pagination"> <?= $this->Paginator->prev("<<") ?> <?= $this->Paginator->numbers() ?> <?= $this->Paginator->next(">>") ?> </ul> </span>
The pagination for the pst of pages is done as follows −
<ul class="pagination"> <?= $this->Paginator->prev("<<") ?> <?= $this->Paginator->numbers() ?> <?= $this->Paginator->next(">>") ?> </ul>
Execute the above example by visiting the following URL −
http://localhost/cakephp4/posts
Output
When you run the code, you will see the following output −
Cpck on the numbers below, to switch to next page, or use the next or previous button.
For example
You will see that page=2 is appended to the page url in the browser.
Advertisements