Helps partition items across pages and generates sets of convenient controls for navigation
- PHP 5.6.0+
-
Include the library via Composer [?]:
$ composer require delight-im/pagination
-
Include the Composer autoloader:
require __DIR__ . '/vendor/autoload.php';
$numberOfItems = 1000;
$pagination = new Pagination($numberOfItems);
// or
$numberOfItems = 1000;
$itemsPerPage = 30;
$pagination = new Pagination($numberOfItems, $itemsPerPage);
// or
$numberOfItems = 1000;
$itemsPerPage = 30;
$currentPage = 10;
$pagination = new Pagination(
$numberOfItems,
$itemsPerPage,
$currentPage
);
If you don’t specify the current page explicitly in the third argument, it will be auto-detected from the page
or p
query parameters, if available.
$targetUrl = '/products/drinks/?page={page}';
echo $pagination->toHtml($targetUrl);
// or
$targetUrl = '/products/drinks/?page={page}';
$numberOfControls = 11;
$showFirstLast = true;
$showPreviousNext = true;
$previousLabel = '« Previous';
$nextLabel = 'Next »';
echo $pagination->toHtml(
$targetUrl,
$numberOfControls,
$showFirstLast,
$showPreviousNext,
$previousLabel,
$nextLabel
);
$targetUrl = '/products/drinks/?page={page}';
echo $pagination->toHtmlForBootstrap5($targetUrl);
// or
$targetUrl = '/products/drinks/?page={page}';
$displaySize = 0; // -1 = small, 0 = normal, +1 = large
$numberOfControls = 11;
$showFirstLast = true;
$showPreviousNext = true;
$previousLabel = '« Previous';
$nextLabel = 'Next »';
echo $pagination->toHtmlForBootstrap5(
$targetUrl,
$displaySize,
$numberOfControls,
$showFirstLast,
$showPreviousNext,
$previousLabel,
$nextLabel
);
Instead of Pagination#toHtmlForBootstrap5
, you can also call Pagination#toHtmlForBootstrap4
or Pagination#toHtmlForBootstrap3
with the same method signature.
First, generate an array of PaginationElement
objects:
$controls = $pagination->build();
// or
$numberOfControls = 11;
$showFirstLast = true;
$showPreviousNext = true;
$previousLabel = '« Previous';
$nextLabel = 'Next »';
$controls = $pagination->build(
$numberOfControls,
$showFirstLast,
$showPreviousNext,
$previousLabel,
$nextLabel
);
Then turn these PaginationElement
objects into custom HTML:
foreach ($controls as $control) {
// $control->getLabel();
// $control->getPage();
// $control->isSelected();
// $control->isDisabled();
}
$pagination->getOffset();
// int(1000)
$pagination->getLimit();
// int(25)
You can use these two values in your database queries, for example like this:
$sql = "SELECT * FROM products LIMIT " . $pagination->getOffset() . ", " . $pagination->getLimit();
// or
$sql = "SELECT * FROM products LIMIT " . $pagination->getLimit() . " OFFSET " . $pagination->getOffset();
All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.
This project is licensed under the terms of the MIT License.