Yii – Pagination

  • Post author:
  • Post category:Yii
  • Post comments:3 Comments

When you have too much data to display on a single page, you should display it on multiple pages. This is also known as pagination.

To show pagination in action, we need data.

Preparing the DB

Step 1 āˆ’ Create a new database. Database can be prepared in the following two ways.

  • In the terminal run mysql -u root -p
  • Create a new database via CREATE DATABASE helloworld CHARACTER SET utf8 COLLATE utf8_general_ci;

Step 2 āˆ’ Configure the database connection in the config/db.php file. The following configuration is for the system used currently.

<?php
   return [
      'class' => 'yii\db\Connection',
      'dsn' => 'mysql:host = localhost;dbname = helloworld',
      'username' => 'vladimir',
      'password' => '12345',
      'charset' => 'utf8',
   ];
?>

Step 3 āˆ’ Inside the root folder run ./yii migrate/create test_table. This command will create a database migration for managing our DB. The migration file should appear in the migrations folder of the project root.

Step 4 āˆ’ Modify the migration file (m160106_163154_test_table.php in this case) this way.

<?php
   use yii\db\Schema;
   use yii\db\Migration;
   class m160106_163154_test_table extends Migration {
      public function safeUp() {
         $this->createTable("user", [
            "id" => Schema::TYPE_PK,
            "name" => Schema::TYPE_STRING,
            "email" => Schema::TYPE_STRING,
         ]);
         $this->batchInsert("user", ["name", "email"], [
            ["User1", "user1@gmail.com"],
            ["User2", "user2@gmail.com"],
            ["User3", "user3@gmail.com"],
            ["User4", "user4@gmail.com"],
            ["User5", "user5@gmail.com"],
            ["User6", "user6@gmail.com"],
            ["User7", "user7@gmail.com"],
            ["User8", "user8@gmail.com"],
            ["User9", "user9@gmail.com"],
            ["User10", "user10@gmail.com"],
            ["User11", "user11@gmail.com"],
         ]);
      }
      public function safeDown() {
         $this->dropTable('user');
      }
   }
?>

The above migration creates a user table with these fields: id, name, and email. It also adds a few demo users.

Step 5 āˆ’ Inside the project root run ./yii migrate to apply the migration to the database.

Step 6Ā āˆ’ Now, we need to create a model for ourĀ userĀ table. For the sake of simplicity, we are going to use theĀ GiiĀ code generation tool. Open up thisĀ url: http://localhost:8080/index.php?r=gii. Then, click the ā€œStartā€ button under the ā€œModel generatorā€ header. Fill in the Table Name (ā€œuserā€) and the Model Class (ā€œMyUserā€), click the ā€œPreviewā€ button and finally, click the ā€œGenerateā€ button.

The MyUser model appears in the models directory.

Pagination in Action

Step 1 āˆ’ Add an actionPagination method to the SiteController.

public function actionPagination() {
   //preparing the query
   $query = MyUser::find();
   // get the total number of users
   $count = $query->count();
   //creating the pagination object
   $pagination = new Pagination(['totalCount' => $count, 'defaultPageSize' => 10]);
   //limit the query using the pagination and retrieve the users
   $models = $query->offset($pagination->offset)
      ->limit($pagination->limit)
      ->all();
   return $this->render('pagination', [
      'models' => $models,
      'pagination' => $pagination,
   ]);
}

Step 2 āˆ’ Create a view file called pagination.php inside the views/site folder.

<?php
   use yii\widgets\LinkPager;
?>
<?php foreach ($models as $model): ?>
   <?= $model->id; ?>
   <?= $model->name; ?>
   <?= $model->email; ?>
   <br/>
<?php endforeach; ?>
<?php
   // display pagination
   echo LinkPager::widget([
      'pagination' => $pagination,
   ]);
?>

Now, go to the local hostĀ http://localhost:8080/index.php?r=site/paginationĀ through the web browser, you will see a pagination widget āˆ’

This Post Has 3 Comments

Leave a Reply