Federick Jons
Federick Jons

Reputation: 33

get total number of records retrieved with pagination in Cakephp

I wanna show the total number of records of the query. The problem is that I'm using paginator, so.. only show me the number of record of the page, and I need the number of all the records.

This is my code:

 public function index()
{
    $paisFK = $this -> Auth -> User()['paisFK'];

    $this->paginate['contain'] = ['Ciudades'];
    $this->paginate['conditions'] = ['Ciudades.paisFK' => $paisFK];

    $complejos = $this->paginate($this->Complejos);

    $this->set(compact('complejos'));
    $this->set('_serialize', ['complejos']);

    //Obtenemos la cantidad de complejos:

    $number = $complejos->count();
    $this->set('number',$number);
}

Someone can help me? Thanks!

Upvotes: 1

Views: 3711

Answers (2)

fool-dev
fool-dev

Reputation: 7777

there is a complete solution, if you follow this, I think solution here:

Controller:

public $paginate =[
    'limit' => 10,
    'order' => [
        'tble.id' => 'asc'
    ]
];

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

public function index()
{
    $product = TableRegistry::get('tble');
    $query = $product->find();
    $this->set(array('data'=>$query));
    $this->set('tble', $this->paginate($query));


    $this->render('index');
}

Index.ctp:

<?= $this->Paginator->prev('<< Previous') ?>
<?= $this->Paginator->next('Next >>') ?>
<?= $this->Paginator->counter() ?>

tble Is a example of your DB Table.

Upvotes: 0

ndm
ndm

Reputation: 60463

The paginator component adds pagination details to the request objects params, using the key paging, where the data is nested using the alias of the table object.

Check

debug($this->request->param('paging'));

This data is easily accessible in your view templates via the paginator helpers param() method.

$this->Paginator->param('count');

Also, depending on how you want to use the value, you could also use the counter() method with a custom format. It supports various tokens, including {{count}}.

$this->Paginator->counter('There is a total of {{count}} record(s)...');

See also

Upvotes: 3

Related Questions