2017 © Pedro Peláez
 

library pagination

Pagination, without a database dependency

image

voku/pagination

Pagination, without a database dependency

  • Sunday, June 10, 2018
  • by voku
  • Repository
  • 1 Watchers
  • 4 Stars
  • 6,394 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 36 Forks
  • 0 Open issues
  • 14 Versions
  • 1 % Grown

The README.md

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

📖 Paginator

Pagination, without (database) dependencies., (*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 __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, (*3)

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:, (*4)

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

The method also allows you to pass in extra data such as a series of GET's, (*5)

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., (*6)

in the controller:, (*7)

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());

The Versions

10/06 2018

dev-master

9999999-dev

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

pagination paginator pager

10/06 2018

3.0.0

3.0.0.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

pagination paginator pager

05/11 2016

2.1.7

2.1.7.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

03/11 2016

2.1.6

2.1.6.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

06/04 2016

2.1.5

2.1.5.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

08/03 2016

2.1.4

2.1.4.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

02/03 2016

2.1.3

2.1.3.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

02/03 2016

2.1.2

2.1.2.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

02/03 2016

2.1.1

2.1.1.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

02/03 2016

2.1.0

2.1.0.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

02/03 2016

2.0.0

2.0.0.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

14/01 2016

1.0.2

1.0.2.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

02/12 2015

1.0.1

1.0.1.0

Pagination, without a database dependency

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager

02/12 2015

1.0.0

1.0.0.0

Pagination, without a database dependency

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

pagination paginator pager