0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CakePHPでページネーションを実装する

Last updated at Posted at 2024-10-26

対象読者

CakePHPでページネーションを実装する方法を知りたい人

検証環境

ソフトウェア バージョン
CakePHP 4.7.5
PHP 8.1

完成イメージ

イメージはこんな感じです。
1ページあたり表示件数を10件表示するようなページングを実装してみました。

cakephp1.png

実装の手順

実装の手順は下記の通りとなります。

Controllerにページネーションの機能を追加

Controllerのinitializeクラス(初期化クラス)にページネーションを読み込ませます。

limitプロパティで、件数を指定してあげます↓
今回は、10件表示するように設定しました。

$this->paginate['limit'] = 10;

つづいて、今回は全ユーザー名を表示する画面にページネーションを付与します。
ページネーションを付与する基本的な処理の実装は下記です。

$this->paginate()

ユーザー全件表示する画面に対してページングをするので、記載はこうなります↓

$this->set('allUsers', $this->paginate($this->テーブル名->find('all')));

【処理の解説】
①取得対象のテーブルからfind('all')関数を使って全件取得する。
②paginate()関数でページング機能を付与する。

ここまでを踏まえて、Controllerのinitializeクラスの実装は、下記のようになります。

RegistersController.php
<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\ORM\TableRegistry;/*追加*/



/**
 * Registers Controller
 *
 * @method \App\Model\Entity\Register[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class RegistersController extends AppController
{       
    
    public function initialize(): void
    {
        parent::initialize();
        //companiesのDBを指定
        $this->usersTable = TableRegistry::get('users');
        //セッションをセットする
        $this->session = $this->getRequest()->getSession();
        //ページネーションで表示件数を指定
        $this->paginate['limit'] = 10;
    }

    ()
    /**
     * ユーザーを全権取得する
     */
    public function allusers()
    {
        //ページネーション機能を追加
        $this->set('allUsers', $this->paginate($this->usersTable->find('all')));
        //$this->set('allUsers', $this->paginate($this->usersTable->find('all')));
        $this->render(); 
    }
}

画面(ビュー)にページング機能を付与する

さいごに、画面にページング機能を作ってみましょう。

ページング機能は、paginateのオプション機能を使ってこのような感じで作ると良いです。

<div class="paginator">
    <ul class="pagenation" style="display:flex;list-style-type: none;"> 
        <?= $this->Paginator->first('<< ' . __('最初へ')) ?> | 
        <?= $this->Paginator->prev('< ' . __('一つ前')) ?> | 
        <?= $this->Paginator->numbers() ?> | 
        <?= $this->Paginator->next(__('次へ') . ' >') ?> | 
        <?= $this->Paginator->last(__('最後へ') . ' >>') ?> | 
    </ul>
</div>

これをふまえての画面のソースはこんな感じになります。

allusers.php
<div>
    <h3>全ユーザー一覧</h3>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>NAME</th>
                <th>Password</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($allUsers as $user): ?>
                <tr>
                    <td><?=$user->id ?></td>
                    <td><?=h($user->name) ?></td>
                    <td><?=h($user->password) ?></td>
                </tr>
            <?php endforeach; ?> 
        </tbody>
    </table>
</div>

<div class="paginator">
    <ul class="pagenation" style="display:flex;list-style-type: none;"> 
        <?= $this->Paginator->first('<< ' . __('最初へ')) ?> | 
        <?= $this->Paginator->prev('< ' . __('一つ前')) ?> | 
        <?= $this->Paginator->numbers() ?> | 
        <?= $this->Paginator->next(__('次へ') . ' >') ?> | 
        <?= $this->Paginator->last(__('最後へ') . ' >>') ?> | 
    </ul>
</div>

            

以上です。

スペシャルサンクス(参考にしたサイト)

CakePHP5 Paginate 表示件数(limit)を一括設定

CakePHP ページネーション機能(Paginator)

【CakePHP入門】Pagenation(ページネーション)の使い方

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?