Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: how to get total number of records retrieved with pagination

Tags:

cakephp

When you retrieve records using $this->paginate('Modelname') with some page limit, how do you get the total number of records retrieved?

I'd like to display this total count on the view, but count($recordsRetrieved) returns the number displayed only on the current page. So, if total number of records retrieved is 99 and limit is set to 10, it returns 10, not the 99.

like image 930
musicliftsme Avatar asked Jun 13 '13 17:06

musicliftsme


People also ask

How can I get total count in pagination?

To Get All Item Total$paginator->total() Determine the total number of matching items in the data store. (Not available when using simplePaginate).

What is CakePHP paginate?

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, to make building paginated queries easier. In the View PaginatorHelper is used to make the generation of pagination links & buttons simple.


Video Answer


4 Answers

You can debug($this->Paginator->params());

This will give you

/*
Array
(
    [page] => 2
    [current] => 2
    [count] => 43
    [prevPage] => 1
    [nextPage] => 3
    [pageCount] => 3
    [order] =>
    [limit] => 20
    [options] => Array
        (
            [page] => 2
            [conditions] => Array
                (
                )
        )
    [paramType] => named
)
*/

The final code for PHP >=5.4:

$this->Paginator->params()['count'];

For PHP versions less than 5.4:

$paginatorInformation = $this->Paginator->params();
$totalPageCount = $paginatorInformation['count'];
like image 128
Moyed Ansari Avatar answered Oct 19 '22 08:10

Moyed Ansari


To get your answer go to he following link http://book.cakephp.org/2.0/en/controllers/request-response.html

use pr($this->request->params) you will find all the stuff of pagination

like image 29
Arzon Barua Avatar answered Oct 19 '22 08:10

Arzon Barua


For cake 3.x

you can use method getPagingParams

example:

debug($this->Paginator->getPagingParams());

output:

[
    'users' => [
        'finder' => 'all',
        'page' => (int) 1,
        'current' => (int) 5,
        'count' => (int) 5,
        'perPage' => (int) 1,
        'prevPage' => false,
        'nextPage' => true,
        'pageCount' => (int) 5,
        'sort' => null,
        'direction' => false,
        'limit' => null,
        'sortDefault' => false,
        'directionDefault' => false,
        'scope' => null
    ]
]
like image 5
Вася Остапчук Avatar answered Oct 19 '22 06:10

Вася Остапчук


Here's how I got the count from my controller*

This is for CakePHP 2.3. It uses a view helper, which is typically a no-no in the controller as it violates MVC, but in this case I think makes sense to keep the code DRY.

// Top of file
App::uses('PaginatorHelper', 'View/Helper');

// Later in controller method
$paginatorHelper = new PaginatorHelper(new View(null));
$records = $this->paginate();
$count = $paginatorHelper->params()['count'];

* I know the OP asked about from the view, but I figure if Arzon Barua's answer is helping people (though I think it only tells you the requested count, not the actual count as the OP wants), then this might help too.

like image 2
Tyler Collier Avatar answered Oct 19 '22 07:10

Tyler Collier