A Simple PHP Pagination library (with Bootstrap 3 example), (*1)
Installation
require_once '/path/to/t2-simple-pagination/T2SimplePagination.php';
Usage
Initialize T2SimplePagination providing the 2 required parameters, total and page as well as additional settings., (*2)
// get the current page
$page = isset($_GET['p']) ? $_GET['p'] : 1;
// count total data from your record source. ie: DB or a file
$total = DB::count($data);
// set the number of items to display per page
$per_page = 10;
// set the number of page links to display
$range = 7; // IMPORTANT: please use odd numbers ONLY!!!
$pagination = new T2SimplePagination($total, $page, $per_page, $range);
Then extract the records by using the offset and per_page properties., (*3)
$query = "SELECT * FROM my_table LIMIT {$pagination->offset}, {$pagination->per_page}"
See the class Properties below for more details., (*4)
Page Links
<?php for ($i = $pagination->min_page; $i <= $pagination->max_page; $i++): ?>
<a href="?p=<?php echo $i ?>">
<?php echo $i ?>
</a>
<?php endfor; ?>
Pager (Next and Prev Links)
prev_page): ?>
<a href="?p=<?php echo $pagination->prev_page ?>">Prev</a>
Prev
next_page): ?>
<a href="?p=<?php echo $pagination->next_page ?>">Next</a>
Next
You may want to hide the pagination links when there is only one page, so it is recommended to enclose the above codes inside this if block:, (*5)
num_page): ?>
// pager or page links
For Bootstrap 3 example, please check the index.php file included in this repository., (*6)
Properties
| Variables |
Description |
| total |
the total number of records to be paginated |
| page |
the current page, default: 1 |
| per_page |
the number of items to display per page, default: 10 |
| num_page |
the total number of pages |
| offset |
the current record pointer |
| next_page |
the next page if page is not equals to num_page
|
| prev_page |
the previous page if page is not 1 |
| range |
the number of page links to display, default: 5 (Note: Use ODD numbers only!) |
| min_page |
the start page in the page links |
| max_page |
the end page in the page links |
License
This library is distributed under MIT License. © 2017 Fatima Aurelia, (*7)