Just Simple Info

Pages

CakePhp Pagination Example

Cake php pagination example
CakePhp pagination
In web application, displaying all data from database is not a good practice. It can affect the performance especially if we have a million or even a thousand of data. It will load the entire page in certain amount of time which is not good for the user experience.
So, to overcome that kind of problem we have to divide the data to be display in our page. For example we have 100 data then we have only to display in first page the 1-10 data and next are the 11-20 and so on. That is called Pagination.

 And know we are going to apply pagination in our simple web application using the CakePhp framework.
Here is the file structure.
File structure
(click to enlarge image)


1.) We are going to create students table in our database.


CREATE TABLE students (
     id int NOT NULL AUTO_INCREMENT,
     code VARCHAR(100) NOT NULL,
     first_name VARCHAR(100) NOT NULL,
     last_name VARCHAR(100) NOT NULL,
     middle_name VARCHAR(100) NOT NULL,
     PRIMARY KEY (id)
)

2.) Create StudentsController.php in App/Controller folder.
      Here is our code.

<?php
class StudentsController extends AppController{
    var $name = 'Students';
    var $uses = array('Student');
    
    
    function index(){
        //*** LIMIT 5 AND ORDER BY STUDENT ID 
        $this->paginate = array('limit'=>5,'order'=>array('Student.id'=>'Desc'));
        
        //*** SET DATA
        $this->set('students',$this->paginate());
    }
    /**
     * Add some extra records
     */
    function addStudent(){
        
        //**** ADD DATA TO STUDENTS TABLE
        for($x = 0 ; $x < 10 ; $x++){
            
            $this->Student->create();
            $this->Student->set('code','code_'.$x);
            $this->Student->set('first_name','first_name_'.$x);
            $this->Student->set('last_name','last_name_'.$x);
            $this->Student->set('middle_name','middle_name_'.$x);
            $this->Student->save();
        }
        //*** SET FLASH MESSAGE
        $this->Session->setFlash('Added some record');
        //*** REDIRECT TO INDEX
        $this->redirect(array('controller' => 'students','action' => 'index'));
    }
    
    /**
     * Delete all record
     */
    function deleteStudent(){
        //*** DELETE ALL DATA IN STUDENT TABLE
        $this->Student->query('TRUNCATE students');
        //*** SET FLASH MESSAGE
        $this->Session->setFlash('All record deleted');
        //*** REDIRECT TO INDEX
        $this->redirect(array('controller' => 'students','action' => 'index'));
    }
}
?>

We provided some function which is addStudent() for adding some records and deleteStudent() for deleting record.
I use truncate command so than the auto increment id will start again to 1.

3.) Create Student.php in App/Model.
      Here is the code.

<?php
class Student extends AppModel{
    var $name = 'Student';
}
?>

Nothing more code in our model. We just want to use the model object.

4.) Create folder Students in App/View.

5.) Create index.ctp in App/View/Students.
     Here is the code for our index.ctp


<?php 
    if(count($students)>0)
        echo $this->Html->link('Delete All',array('controller' => 'students', 'action' => 'deleteStudent')); 
    else 
       echo $this->Html->link('Add Record',array('controller' => 'students', 'action' => 'addStudent')); 
?>

<table cellspacing="0" cellpadding="0">
    <?php
        //*** TABLE HEADER'S
        $tableHeaders = $this->Html->tableHeaders(array(
            $this->Paginator->sort('id'),
            $this->Paginator->sort('code'),
            $this->Paginator->sort('first_name'),
            $this->Paginator->sort('last_name'),
            $this->Paginator->sort('middle_name')
        ));
        
        //*** DISPLAY TABLE HEADER'S
        echo $tableHeaders;
        
        //*** TABLE ROW'S
        $rows =array();
        
        foreach ($students as $student){
            $rows[] = array(
                $student['Student']['id'],
                $student['Student']['code'],
                $student['Student']['first_name'],
                $student['Student']['last_name'],
                $student['Student']['middle_name']
            );
            
           
        }
        //*** DISPLAY TABLE ROW'S
        echo $this->Html->tableCells($rows);
    ?>
    
</table>
<div id="paging_div">
    <?php 
        //*** PAGINATION NUMBER'S
        echo $this->Paginator->numbers();
    
    ?>
</div>

Download Source Code.

That'a all!!!
Have a nice day and happy coding!!!

No comments:

Post a Comment