Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implement Pagination Using Cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 179
    Comment on it

    This tutorial will help you to learn how to implement pagination using cakephp framework


    Requisite:

    1. Create a database
    2. Insert some data in the table
    3. Controller say UsersController to write the logic.
    4. Model for the Users table
    5. View to display the pagination

    We will start with creating a UsersController and writing an action in the controller.

    path: app/Controller/UsersController.php

    <?php
    
    class UsersController extends AppController {
    
    var $uses  = array('User');
    public $components = array( 'Paginator');
    public $helpers = array('Html','Paginator');
    
    public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('paginateuser');
    }
    
    public function paginateuser() {     
    
       $paginate = array(
            'fields' => array('User.user&#95;name', 'User.email','User.gender','User.state','User.city'),
            'order' => array(
            'User.user&#95;name' => 'asc')
        );
    
        $data = $this->Paginator->paginate();
        $this->set('models', $data);
    }
    }
    ?>
    

    Create a model for the users table:

    path: app/Model/User.php

    <?php
    
    class User extends AppModel {
    
    }
    ?>
    

    Create a view for the action, paginateuser() in the UsersController.php

    path: app/View/Users/paginateuser.ctp

    <html>
    <head><title>simple page</title></head>
    <body>
    <div>
    <table>
    <thead>
        <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Gender</th>
        <th>Sate</th>
        <th>City</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach($models as $val){?>    
    <tr>
        <td><?php echo $val['User']['user&#95;name'];?></td>
        <td><?php echo $val['User']['email'];?></td>
        <td><?php echo $val['User']['gender'];?></td>
        <td><?php echo $val['User']['state'];?></td>
        <td><?php echo $val['User']['city'];?></td>
    </tr>
    <?php }?>
    </tbody>
    </table>
    <tr>
      <td>
       <?php 
       echo $this->paginator->prev('<<Previous', null, null, null);
       echo "&nbsp;&nbsp;";
       echo $this->paginator->numbers();
       echo "&nbsp;&nbsp;";
       echo $this->paginator->next('Next>>', null, null, null);
       ?>
      </td>
    </tr>
    </div>
    </body>
    </html>
    

 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: