How to use Pagination in CakePHP ?

Member

trycia.jones

by trycia.jones , in category: Technology , a month ago

How to use Pagination in CakePHP ?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dion.waelchi , a month ago

In CakePHP controller Pagination component is used to building paginated queries. In order to generate pagination links & buttons in view PaginatorHelper is used

Below are sample pagination usage code in CakePHP


Pagination in Controller


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class PostsController extends AppController
{

    public $paginate = [
        'limit' => 25,
        'order' => [
            'Posts.id' => 'desc'
        ]
    ];

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Paginator');
        $this->loadHelper('Paginator', ['templates' => 'MyPlugin.paginator-templates']);
    }

    public function display(){
      $this->set('posts', $this->paginate());
    }
}


Pagination in CakePHP Views


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

$paginator = $this->Paginator;
 
if($posts){
 
    //creating our table
    echo "<table>";
 
        // our table header, we can sort the data user the paginator sort() method!
        echo "<tr>";
         
            // in the sort method, there first parameter is the same as the column name in our table
            // the second parameter is the header label we want to display in the view
            echo "<th>" . $paginator->sort('id', 'ID') . "</th>";
            echo "<th>" . $paginator->sort('title', 'Title') . "</th>";
            echo "<th>" . $paginator->sort('published_on', 'Published on') . "</th>";
          
        echo "</tr>";
         
        // loop through the user's records
        foreach( $posts as $post ){
            echo "<tr>";
                echo "<td>{$post['Post']['id']} </td>";
                echo "<td>{$post['Post']['title']} </td>";
                echo "<td>{$post['Post']['published_on']} </td>";
               
            echo "</tr>";
        }
         
    echo "</table>";
 
    // pagination section
    echo "<div class='paging'>";
 
        // the 'first' page button
        echo $paginator->first("First");
         
        // 'prev' page button, 
        // we can check using the paginator hasPrev() method if there's a previous page
        // save with the 'next' page button
        if($paginator->hasPrev()){
            echo $paginator->prev("Prev");
        }
         
        // the 'number' page buttons
        echo $paginator->numbers(array('modulus' => 2));
         
        // for the 'next' button
        if($paginator->hasNext()){
            echo $paginator->next("Next");
        }
         
        // the 'last' page button
        echo $paginator->last("Last");
     
    echo "</div>";
     
}
 
// tell the user there's no records found
else{
    echo "No posts found.";
}
?>