The simpliest PHP pagination class

v1.0.1 2016-02-21 22:13 UTC

This package is not auto-updated.

Last update: 2024-05-22 12:58:33 UTC


License Packagist


  • PHP 5.3 or higher

Installation with Composer

  • from the command line
composer require leoshtika/pagination
  • or updating your composer.json file
    "require": {
        "leoshtika/pagination": "~1.0"


Connect to an SQLite database


require_once 'vendor/autoload.php';

use leoshtika\libs\Pagination;
use leoshtika\libs\Sqlite;
use leoshtika\libs\UserFaker;

$sqliteFile = 'demo.sqlite';

// Create a new sqlite db if not exists and load some dummy data. 
// After the database is created, you don't need this line of code anymore
UserFaker::create($sqliteFile, 120);

$dbh = Sqlite::connect($sqliteFile);

// Get the total number of records
$totalRecords = $dbh->query('SELECT count(*) FROM user')->fetch(PDO::FETCH_COLUMN);

// Instantiate the Pagination
$pagination = new Pagination($_GET['page'], $totalRecords, 10);

// Get records using the pagination
$sth = $dbh->prepare('SELECT * FROM user LIMIT :offset, :records');
$sth->bindValue(':offset', $pagination->offset(), PDO::PARAM_INT);
$sth->bindValue(':records', $pagination->getRecordsPerPage(), PDO::PARAM_INT);
$users = $sth->fetchAll(PDO::FETCH_OBJ);

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<body class="container-fluid">
    <table class="table table-bordered table-hover">
        <?php foreach ($users as $user) : ?>
                <td><?php echo $user->id; ?></td>
                <td><?php echo $user->name; ?></td>
                <td><?php echo $user->email; ?></td>
                <td><?php echo $user->address; ?></td>
                <td><?php echo $user->phone; ?></td>
        <?php endforeach; ?>
    <?php echo $pagination->nav(); ?>