PaginationHelper

Introduction

The PaginationHelper class in JiFramework provides an efficient and flexible way to implement pagination in your application. It simplifies the process of dividing large datasets into manageable pages and generating pagination links for navigation. By using this class, developers can easily integrate pagination functionality without reinventing the wheel.

Key Features:

  • Dynamic Pagination Data: Calculates necessary pagination parameters such as current page, total pages, offsets, and more.
  • HTML Pagination Links: Generates customizable HTML pagination links, compatible with popular CSS frameworks like Bootstrap.
  • Flexible Configuration: Supports custom query parameters, base URLs, and the maximum number of page links to display.

Usage:

// Option 1: Initialize the PaginationHelper through the App
use JIFramework\Core\App\App;
$app = new App();
$pagination = $app->pagination;

OR 

// Option 2: Directly instantiate the PaginationHelper
use JIFramework\Core\Utilities\Pagination\PaginationHelper;
$pagination = new PaginationHelper();

Explanation:

  • Option 1: Initializes the App class and retrieves the PaginationHelper instance through it, using the framework's main entry point.
  • Option 2: Directly creates a PaginationHelper instance.

paginate()

Method: paginate(int $itemsPerPage, int $totalItems, array $options = []): object

Description: Calculates and returns pagination data based on the total number of items and items per page.

Parameters:

  • $itemsPerPage (int): The number of items to display per page.
  • $totalItems (int): The total number of items in the dataset.
  • $options (array, optional): An associative array of optional parameters:
    • 'currentPage' (int): The current page number. Defaults to $_GET['page'] or 1 if not set.
    • 'queryParams' (array): Additional query parameters to include in pagination links.

Returns: An object containing pagination data:

  • currentPage (int): The current page number.
  • nextPage (int): The next page number.
  • previousPage (int): The previous page number.
  • offset (int): The offset to use in database queries.
  • itemsPerPage (int): The number of items per page.
  • totalPages (int): The total number of pages.
  • queryParams (string): Query parameters to include in pagination links.

Usage:

$itemsPerPage = 10;
$totalItems = 150; // For example, total items retrieved from a database count query

$paginationData = $pagination->paginate($itemsPerPage, $totalItems);

/*
Sample $paginationData:

object(stdClass)#1 (7) {
  ["currentPage"]=> int(1)
  ["nextPage"]=> int(2)
  ["previousPage"]=> int(1)
  ["offset"]=> int(0)
  ["itemsPerPage"]=> int(10)
  ["totalPages"]=> int(15)
  ["queryParams"]=> string(0) ""
}

*/

// Use $paginationData->offset and $paginationData->itemsPerPage in your database query

Explanation:

  • Determines the current page from the $options array or $_GET['page'].
  • Calculates the total number of pages based on $totalItems and $itemsPerPage.
  • Computes the nextPage and previousPage numbers with boundary checks.
  • Calculates the offset for use in database queries (e.g., SQL LIMIT and OFFSET).
  • Preserves existing query parameters, excluding 'page', and constructs a query string.
  • Returns an object with all relevant pagination data for use in your application.