PHP Classes

PHP Paginator: Show pagination links for listings split in pages

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 291 All time: 7,493 This week: 49Up
Version License PHP version Categories
php-paginator 1.0.7MIT/X Consortium ...5HTML, PHP 5
Collaborate with this project 

Authors

David Carr
Lars Moelleken


Contributor

pagination - github.com

Description

This package can show pagination links for listings split in pages.

It can take as parameters the limit of entries to display per page and the name of a parameters that will be used to pass page numbers between pages.

It generates the URL of links to generate HTML navigation to browse between listing pages.

It can return the link URLs as arrays or as HTML for the links with the possibility to configure their presentation using CSS styles.

Picture of Lars Moelleken
  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 25 packages by
Country: Germany Germany
Age: 36
All time rank: 62140 in Germany Germany
Week rank: 22 Up2 in Germany Germany Up
Innovation award
Innovation award
Nominee: 11x

Winner: 1x

Documentation

Build Status codecov.io Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

? Paginator

Pagination, without (database) dependencies.

Install via "composer require"

composer require voku/pagination

Usage

  1. include the composer-autoloader
  2. instantiate a new object pass in the number of items per page and the instance identifier, this is used for the GET parameter such as ?p=2
  3. pass the set_total method the total number of records
  4. show the records
  5. call the page_links method to create the navigation links
use voku\helper\Paginator;

// include the composer-autoloader
require_once __DIR__ . '/vendor/autoload.php';

$pages = new Paginator(10, 'p');
$pages->set_total(100); // or a number of records

// display the records here

echo $pages->page_links();

if using a database you limit the records by placing $pages->get_limit() in your query, this will limit the number of records

SELECT * FROM table $pages->get_limit()

by default the page_links method created links starting with ? this can be changed by passing in a parameter to the method:

echo $pages->page_links('&');

The method also allows you to pass in extra data such as a series of GET's

echo $pages->page_links('?' . 'status=' . $_GET['status'] . '&active=' . $_GET['active'] . '&');

Database example

use voku\helper\Paginator;

// include the composer-autoloader
require_once __DIR__ . '/vendor/autoload.php';

// create new object pass in number of pages and identifier
$pages = new Paginator(10, 'p');

// get number of total records
$rowCount = $db->query('SELECT count(*) FROM table');

// pass number of records to
$pages->set_total($rowCount); 

$data = $db->query('SELECT * FROM table ' . $pages->get_limit());
foreach($data as $row) {
  // display the records here
}

// create the page links
echo $pages->page_links();

MVC example

using this class in an MVC environment its almost the same, only the database or dataset calls come from the model instead of the page directly.

in the controller:

use voku\helper\Paginator;

// create a new object
$pages = new Paginator(10, 'p');

// set the total records, calling a method to get the number of records from a model
$pages->set_total( $this->_model->get_all_count() );

// calling a method to get the records with the limit set
$data['records'] = $this->_model->get_all( $pages->get_limit() );

// create the nav menu
$data['page_links'] = $pages->page_links();

// then pass this to the view, may be different depending on the system
$this->_view->render('index', $data);

API example (with Database)

use voku\helper\Paginator;

// include the composer-autoloader
require_once __DIR__ . '/vendor/autoload.php';

// create new object pass in number of pages and identifier
$pages = new Paginator(10, 'p');

// get number of total records
$rowCount = $db->query('SELECT COUNT(*) FROM table');

// pass number of records to
$pages->set_total($rowCount); 

$data = $db->query('SELECT * FROM table ' . $pages->get_limit());
foreach($data as $row) {
  // display the records here
}

// create the api-call
header('Content-Type: application/json');
echo json_encode($pages->page_links_raw());

API example (with Array)

use voku\helper\Paginator;

// include the composer-autoloader
require_once __DIR__ . '/vendor/autoload.php';

$page = (int)$_GET['page'];
$perPage = (int)$_GET['per_page'];

$data = array('some', 'kind', 'of', 'data');

// use the helper-class to reduce the number of pages
$result = PaginatorHelper::reduceData($data, $perPage, $page);

// create the api-call
header('Content-Type: application/json');
echo json_encode($pages->page_links_raw());

  Files folder image Files (19)  
File Role Description
Files folder imagedemo (1 file)
Files folder imagesrc (1 directory)
Files folder imagetests (4 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file circle.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.php_cs Example Example script
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (19)  /  demo  
File Role Description
  Accessible without login Plain text file pagination.css Data Auxiliary data

  Files folder image Files (19)  /  src  
File Role Description
Files folder imagevoku (1 directory)

  Files folder image Files (19)  /  src  /  voku  
File Role Description
Files folder imagehelper (2 files)

  Files folder image Files (19)  /  src  /  voku  /  helper  
File Role Description
  Plain text file Paginator.php Class Class source
  Plain text file PaginatorHelper.php Class Class source

  Files folder image Files (19)  /  tests  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file PaginatorHelperTest.php Class Class source
  Plain text file PaginatorStrictTest.php Class Class source
  Plain text file PaginatorTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:291
This week:0
All time:7,493
This week:49Up
User Comments (1)