Pagination

Table of contents

  1. Overview
  2. Setup
  3. Final View


1. Overview Table of Contents

Pagination is supported with the use of a Pagination class and built in Bootstrap 5 support. An example of this is shown below in Figure 1.

Pagination example

Figure 1 - Pagination example


2. Setup Table of Contents

The following are instructions for setting up Pagination within your controller. We will use the indexAction of the ContactsController.

A. Import Pagination Class

use Core\Lib\Pagination\Pagination;


B. Get Current Page

$page = Pagination::currentPage($this->request);


C. Get Total Records

We want to obtain the list of all of the records. In this case the number of contacts for this user when creating instance of Pagination class. The condition is the user_id and we bind with the current user’s id since we want only contacts associated with this user.

$pagination = new Pagination($page, 10, Contacts::findTotal([
    'conditions' => 'user_id = ?',
    'bind'       => [$this->currentUser->id]
]));


D. Retrieve Paginated Records

We will now proceed to get paginated records using base model’s find method. In this step we use the paginationParams function from the Pagination class to build our query.

$contacts = Contacts::find($pagination->paginationParams(
    'user_id = ?',                  // Conditions
    [$this->currentUser->id],       // Bind
    'lname, fname')                 // Order
);


E. Configure The View

Configure the view by setting the results of the pagination function to the $this->view->pagination variable. This variable contains the data for rendering the page links.

$this->view->pagination = Pagination::pagination($page, $pagination->totalPages());

Putting everything together here is the complete indexAction function:

public function indexAction(): void {
    // Determine current page
    $page = Pagination::currentPage($this->request);

    // Get the total number of contacts for the user
    $pagination = new Pagination($page, 10, Contacts::findTotal([
        'conditions' => 'user_id = ?',
        'bind'       => [$this->currentUser->id]
    ]));
    
    // Retrieve paginated contacts using the base model’s find method
    $contacts = Contacts::find($pagination->paginationParams(
        'user_id = ?', 
        [$this->currentUser->id], 
        'lname, fname')
    );

    // Configure the view
    $this->view->contacts = $contacts;
    $this->view->pagination = Pagination::pagination($page, $pagination->totalPages());
    $this->view->render('contacts/index');
}


F. Adding Pagination To View

Within your view add the following line, in this cases, right after the closing tag for the table element:

<?= $this->pagination ?>


3. Final View Table of Contents

Putting everything together we can demonstrate what the final view looks like. By placing the $this->pagination variable below the closing tag for the table the links are located where they need to be.

<?php $this->setSiteTitle("My Contacts"); ?>

<?php $this->start('body'); ?>
<h2 class="text-center">My Contacts</h2>
<table class="table table-striped  table-bordered table-hover">
    <thead>
        <th>Name</th>
        <th>Email</th>
        <th>Cell Phone</th>
        <th>Home Phone</th>
        <th>Work Phone</th>
        <th></th>
    </thead>
    <tbody>
        <?php foreach($this->contacts as $contact): ?>
            <tr>
                <td>
                    <a href="<?=Env::get('APP_DOMAIN', '/')?>contacts/details/<?=$contact->id?>">
                        <?= $contact->displayName(); ?>
                    </a>
                </td>
                <td><?= $contact->email ?></td>
                <td><?= $contact->cell_phone ?></td>
                <td><?= $contact->home_phone ?></td>
                <td><?= $contact->work_phone ?></td>
                <td class="text-center">
                    <a href="<?=Env::get('APP_DOMAIN', '/')?>contacts/edit/<?=$contact->id?>" class="btn btn-info btn-sm">
                        <i class="fa fa-edit"></i> Edit
                    </a>
                    <a href="<?=Env::get('APP_DOMAIN', '/')?>contacts/delete/<?=$contact->id?>" class="btn btn-danger btn-sm" onclick="if(!confirm('Are you sure?')){return false;}">
                        <i class="fa fa-trash"></i> Delete
                    </a>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<?= $this->pagination ?>
<?php $this->end(); ?>