stephweb/dww-php-pagination

Simple PHP Pagination Library


Keywords
php, pagination
License
MIT

Documentation

DWW PHP Pagination

Latest Stable Version License

DWW PHP Pagination is a library of simple Pagination in PHP. This open source pagination contains of a PHP class, and a CSS style sheet. You can of course edit the CSS in fontion your desires.

Installation

Installation via Composer :

php composer.phar require stephweb/dww-php-pagination

Examples

Simple example :

<?php

use DwwPhpPagination\Pagination;

$pagination = new Pagination();

$pagination->paginate($countElements);

$limit = $pagination->getLimit();
$offset = $pagination->getOffset();

// Here your listing of elements with a loop

echo $pagination->render();
echo $pagination->perPage();

Example with SQL queries :

<?php

// If you do not use Composer to download this package,
// you must do it "require" manually
require_once 'your-path/dww-php-pagination/DwwPhpPagination/bootstrap/autoload.php';

use DwwPhpPagination\Pagination;


// Count Elements of a table
function countElements() {
    $sql = "SELECT COUNT(*) AS nb FROM table";
    $query = db()->query($sql);
    $result = $query->fetch();

    return $result->nb;
}

// Collect the Elements
function findElements($limit, $offset) {
    $sql = "SELECT * FROM table LIMIT ? OFFSET ?";
    $query = db()->prepare($sql);
    $query->bindValue(1, $limit, PDO::PARAM_INT);
    $query->bindValue(2, $offset, PDO::PARAM_INT);
    $query->execute();

    return $query;
}


// Creating an object Pagination
$pagination = new Pagination();

// Paginate
$pagination->paginate(countElements());

$limit = $pagination->getLimit();
$offset = $pagination->getOffset();

// Show elements one by one that are retrieved from the database
foreach (findElements($limit, $offset) as $article) {
    echo htmlspecialchars($article->field);
}

// Show the Pagination
echo $pagination->render();

Fontion the "db()" should return the result of the connection to a database (the result of an instance of PDO for example). But you are not obliged to create one function "db()", you can use this library with an ORM example.

Example with a list of files of a directory :

<?php

use DwwPhpPagination\Pagination;

$scandir = scandir('your_path_upload');

$listFilesFromPath = [];
$count = 0;
foreach ($scandir as $f) {
    if ($f != '.' && $f != '..') {
        $listFilesFromPath[] = $f;
        $count++;
    }
}

// Creating an object Pagination
$pagination = new Pagination();

// Paginate
$pagination->paginate($count);

// Listing
$files = array_slice($listFilesFromPath, $pagination->getOffset(), $pagination->getLimit());

// Show files one by one
foreach ($files as $file) {
    echo $file;
}

// Show the Pagination
echo $pagination->render();
// Show a "per page" so that the visitor can have the choice of the number of elements to display per page
echo $pagination->perPage();

Additional Options :

_When creating the Pagination object, you can spend an options array at the instance constructor :

<?php

// Number of Elements per page
$pagination = new Pagination(['pp'=>20]);
// Is 10 by default

// Number of links alongside the current page
$pagination = new Pagination(['limits_start_end'=>5]);
// Is 4 by default

// The choice to select potentially generate with perPage()
$pagination = new Pagination(['options_select'=>[5, 10, 50, 100, 500, 'all']]);
// The value of 'options_select' must be a array.
// Only integers and 'all' is permitted.
// Value is [5, 10, 25, 50, 100] by default

// To change the CSS style pagination (to another CSS class as default)
$pagination = new Pagination(['ccs_class_p'=>'name-css-class-of-pagintion']);
// The CSS class name is by default "bloc-pagination"

// To change the CSS style of a page (select) (to another CSS id as default)
$pagination = new Pagination(['ccs_id_pp'=>'name-css-id-of-per-page']);
// The CSS ID name is by default  "per-page"

_To view a select so that the visitor can have the choice of the number of elements to display per page, this must be added below the method "render ()": :

<?php

echo $pagination->perPage();

In parameter of this method, you can specify the form action. If you do not do it, take action as default value $_SERVER['REQUEST_URI'].

_To view the total number of items on which pagine :

<?php

echo $pagination->getCount();

_To view number of elements to current page :

<?php

echo $pagination->getCountOnCurrentPage();

_To return indexing of the first element and indexing of the last element on the current page (Useful for example display: element "nb start" to "nb end" on this page) :

<?php

// Return int - indexing of the first element
echo $pagination->getFrom();

// Return int - indexing of the last element
echo $pagination->getTo();

_To view the current page :

<?php

echo $pagination->getCurrentPage();

_To view number of pages :

<?php

echo $pagination->getNbPages();

_Pour afficher le nombre d'éléments qui sont affichés par page :

<?php

echo $pagination->getPerPage();

_Return bool - True if there are still pages after the current one :

<?php

$pagination->hasMorePages();

_If there are already parameters in request GET, we sometimes want "to accumulate" them with the links of the pagination. Here is the solution :

<?php

// For to accumulate one GET to Pagination link
echo $pagination->render('get');

// For to accumulate multi GET to Pagination links
echo $pagination->render(['get1', 'get2']);

Bugs and security Vulnerabilities

If you discover a bug or a security vulnerability within DWW PHP Pagination, please send an message to Steph. Thank you. All beg and all security vulnerabilities will be promptly addressed.

License

The DWW PHP Pagination is open-sourced software licensed under the MIT license.