Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pagination in cakephp 2.7

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 321
    Comment on it

    Pagination is basically dividing your data into multiple pages.

    When we fetch the data in bulk from the data base it comes on a single page but that is sometimes difficult to keep scrolling to the last if we want to go to the last data.

    It also takes more time to load the data.

    It is better to divide the data into multiple pages.

    There are multiple ways to apply pagination like using $paginate or paginator.

    Here i am explaining an easy way to paginate your data through controller.

    You can find the data like we usually do and then we can store it in variable.

    $page = $this->User->find("all", array("fields" => "User*"));

    and now you can paginate this variable.

     this->paginate = array(
                "limit" => 5,
                "order" => 'User.id DESC'
                );
    
    
    $page = $this->paginate();
    
    

    you can set this variable like you set it

    $this->set("page", $page);

    This will give 5 records on a page. You can set the limit accordingly. The data will come in the order of last inserted id first.

    Remember if you have associated the model of this controller with any other model it will also  give data of those tables.

    If you want data of only this then you can use recursive => -1 in the array.

     

    In ctp file you can show the data as per the requirement. Kindly see below code.

    <th><?php echo $this->Paginator->sort('id'); ?></th>
                            <th><?php echo $this->Paginator->sort('name'); ?></th>
                            <th><?php echo $this->Paginator->sort('address'); ?></th>
                            <th><?php echo $this->Paginator->sort('gender'); ?></th>
                            <th><?php echo $this->Paginator->sort('department'); ?></th>
                            <th><?php echo $this->Paginator->sort('Want to meet'); ?></th>
                            <th><?php echo $this->Paginator->sort('image'); ?></th>
                            <th><?php echo $this->Paginator->sort('status'); ?></th>                    
                            <th class="actions"><?php echo __('Actions'); ?></th>

    You can set the data using page variable below these headers.

    you also need to give the links of page and next and previous.

     
                           
                                

    Now as you have done all the things you can start work.

     

    Pagination in cakephp 2.7

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: