CakePHP:如何获取使用分页检索的记录总数

时间:2013-06-13 17:41:57

标签: cakephp

使用$this->paginate('Modelname')使用某个页面limit检索记录时,如何获取检索到的记录总数?

我想在视图上显示此总计数,但count($recordsRetrieved)返回仅在当前页面上显示的数字。因此,如果检索的记录总数为99且limit设置为10,则返回10,而不是99.

5 个答案:

答案 0 :(得分:19)

你可以debug($this->Paginator->params());

这会给你

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

PHP的最终代码> = 5.4:

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

对于小于5.4的PHP版本:

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

答案 1 :(得分:7)

要获得答案,请转到以下链接 http://book.cakephp.org/2.0/en/controllers/request-response.html

使用pr($this->request->params)你会发现所有的分页内容

答案 2 :(得分:1)

对于蛋糕3.x

你可以使用方法getPagingParams

示例:

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

输出:

[
    '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
    ]
]

答案 3 :(得分:0)

以下是我从控制器获取计数的方法*

这适用于CakePHP 2.3。它使用了一个视图助手,它在控制器中通常是禁止的,因为它违反了MVC,但在这种情况下,我认为保持代码DRY是有意义的。

{{1}}

*我知道OP从视图中询问,但我想如果Arzon Barua的回答是帮助人们(虽然我认为它只告诉你所要求的数量,而不是OP想要的实际数量),那么这可能会有所帮助太

答案 4 :(得分:0)

这种方式是通过控制器获取分页信息。

  printf("Give a 5-digit number: ");
  char buf[80];
  if (fgets(buf, sizeof buf, stdin) == NULL) Handle_EOF();
  afmi = atoi(buf);  // or strtol() for better error handling.
相关问题