PHP Classes
elePHPant
Icontem

PHP Paginator: Show pagination links for listings split in pages

Recommend this page to a friend!
  Info   View files Documentation   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-08-12 (3 days ago) RSS 2.0 feedNot enough user ratingsTotal: 142 This week: 21All time: 8,442 This week: 34Up
Version License PHP version Categories
php-paginator 1.0.2MIT/X Consortium ...5HTML, PHP 5
Collaborate with this project Author

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.

  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 17 packages by
Country: Germany Germany
Age: 29
All time rank: 149898 in Germany Germany
Week rank: 7 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 8x

Details

Build Status Coverage Status codecov.io Scrutinizer Code Quality Codacy Badge SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version PHP 7 ready License

Paginator

Pagination, without a database dependency.

Install / Download

You can download it from here, or require it using composer.

{
    "require": {
      "voku/pagination": "2.*"
    }
}

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 dirname(__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 dirname(__DIR__) . '/vendor/composer/autoload_real.php';

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

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

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

$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 dirname(__DIR__) . '/vendor/autoload.php';

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

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

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

$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 dirname(__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  
File Role Description
Files folder imagedemo (1 file)
Files folder imagesrc (1 directory)
Files folder imagetests (3 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 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 phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

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

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

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

  Files folder image Files  /  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  /  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 PaginatorTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:142
This week:21
All time:8,442
This week:34Up