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

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.6k
    Comment on it

    Hello Reader's,
    Today we will discuss the Pagination in CakePHP 3. Pagination is a very useful part of any application where you can manage multiple record in a single page with the help of Pagination. Pagination in CakePHP is provided by a component in the controller which makes building paginated queries easier.

    Here you can see how to use Pagination in CakePHP 3. Open your controller then put below code.

    public $paginate = [		
            'limit' => 10,
            'order' => [
                'Articles.title' => 'asc'
            ]
        ];

    After this now load Paginator in initialize () function.

    public function initialize()
        {
            parent::initialize();        
            $this->loadComponent('Paginator');
        }

    Set Paginate in Index function.

    
    public function index()
        {    	
    	$this->layout=false;			
    	$details=$this->Articles->find('all');	
    	$this->set('articles', $this->paginate($details));
        }

    in your index.ctp page.

    <?php
                    echo "<div class='center'><ul class='pagination' style='margin:20px auto;'>";
                    echo $this->Paginator->prev('< ' . __('previous'), array('tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'disabled'), null, array('class' => 'prev disabled'));
                    echo $this->Paginator->numbers(array('separator' => '','tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'active')); 
                    echo $this->Paginator->next(__('next').' >', array('tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'disabled'), null, array('class' => 'next disabled'));
                    echo "</div></ul>";
                ?>

     

 1 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: