CakePHP: Get pagination page count as variable, Hide page count if only one page

This isn’t mentioned in the CakePHP book, but you can get the number of pages in a pagination by using the following code:

$this->params['paging'][<MODEL NAME>]['pageCount']

I recently used this to only display a page count if there is more than one page. Below is the code, note that my model name is “Property” in this example.

$hasPages = ($this->params['paging']['Property']['pageCount'] > 1);
 
if ($hasPages)
{
    echo $this->Paginator->counter();
}

8 responses to “CakePHP: Get pagination page count as variable, Hide page count if only one page”

  1. Nick says:

    Exactly what I was looking for, and it works!
    Thanks!

  2. Ondiege says:

    Hey thanks, this just helped me catch a VERY obnoxious bug.

  3. Nag says:

    Thanks for this post. Its working for me!!

  4. Carlos says:

    Thanks for sharing!

    I found the same printing:
    $this->params[‘paging’]

    For example, here you could find also the actual page:
    $this->params[‘paging’][][‘page’];

  5. Brandon says:

    this works as well:

    $numPages = intval($this->Paginator->counter(‘{:pages}’));

  6. james says:

    Thanks,

    It is working.

  7. Diogo says:

    Thanks for this post. Its working for me 2!!

Leave a Reply to james Cancel reply

Your email address will not be published. Required fields are marked *