gpt4 book ai didi

php - 如何在 CakePHP 3 中获取通过分页检索的记录总数?

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:44 25 4
gpt4 key购买 nike

我在 CakePHP 3 中遇到正确分页的问题。

如何在 CakePHP 3 中获取分页检索的记录总数?

最佳答案

这可能对您有所帮助。在您的 Controller 中,即 UsersController.php 进行这样的分页:

public function index()
{

//set your pagination limit 10
$this->paginate = [
'limit' => 10
];

//query all users
$users = $this->paginate($this->Users->find('all'));

//pass to template
$this->set(compact('users'));

}

在您的模板 View 中,即 users/index.ctp 放置此代码以显示您的分页

<div class="users index large-9 medium-8 columns content">
<h3><?= __('Users') ?></h3>
<table cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('fullname') ?></th>
<th scope="col"><?= $this->Paginator->sort('email') ?></th>
<th scope="col"><?= $this->Paginator->sort('handphone') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $this->Number->format($user->uid) ?></td>
<td><?= h($user->fullname) ?></td>
<td><?= h($user->email) ?></td>
<td><?= h($user->handphone) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>

相应地更改上述变量以匹配您的表实体(列的名称)。

TIPS: If you use CakePHP bake command, you will get all this pagination by default

关于php - 如何在 CakePHP 3 中获取通过分页检索的记录总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com