voku / pagination
Pagination, without a database dependency
Installs: 10 592
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 45
Open Issues: 1
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ~6.0|~7.0
README
📖 Paginator
Pagination, without (database) dependencies.
Install via "composer require"
composer require voku/pagination
Usage
- include the composer-autoloader
- 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
- pass the set_total method the total number of records
- show the records
- 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());