Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Limit the paginate in cakephp

How to Limit the paginate in cakephp ?

Assume that i have 400 records.
I need to get only 25 records from 50th record to 75th record and need to display 5 records per page.
How i can do this in paginate ?

Sample Code:

        $this->paginate = array(
                'contain'=>array('User'),
                'recursive' => 2,
                'order' => array('Profile.winning' => 'DESC'),
                'limit' =>5

            );
like image 515
AnNaMaLaI Avatar asked May 27 '11 12:05

AnNaMaLaI


People also ask

How can I paginate in CakePHP?

CakePHP eases the burden on the developer by providing a quick, easy way to paginate data. Pagination in CakePHP is offered by a component in the controller. You then use View\Helper\PaginatorHelper in your view templates to generate pagination controls.

What does it mean to paginate a document?

Pagination is the process of separating print or digital content into discrete pages. For print documents and some online content, pagination also refers to the automated process of adding consecutive numbers to identify the sequential order of pages.


2 Answers

You can set conditions for the pagination.

function listRecords()
  {
  $this->paginate = array(
    'conditions' => array('Model.id >=' => 50, 'Model.id <=' => 75),
    'limit' => 5
    );
  $this->paginate('Model');
  );

EDIT:

A solution from here:

$this->paginate = array(
  'limit' => 20,
  'totallimit' => 1000
  );

And then in the Model:

public function paginateCount($conditions = null, $recursive = 0, $extra = array())
  {
  if( isset($extra['totallimit']) ) return $extra['totallimit'];
  }
like image 146
pawelmysior Avatar answered Sep 27 '22 20:09

pawelmysior


Improved version with reference of: http://www.mainelydesign.com/blog/view/best-paginatecount-cakephp-with-group-by-support

This return the correct total count base on whichever is less.

public function paginateCount($conditions = null, $recursive = 0, $extra = array()) 
    {
        $conditions = compact('conditions');
        if ($recursive != $this->recursive) {
            $conditions['recursive'] = $recursive;
        }
        unset( $extra['contain'] );
        $count = $this->find('count', array_merge($conditions, $extra));

        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }

        if (isset($extra['totallimit']) && $extra['totallimit'] < $count) {
            return $extra['totallimit'];
        }

        return $count;
    }
like image 25
neobie Avatar answered Sep 27 '22 19:09

neobie