1. Go to this page and download the library: Download esi/pagination library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
esi / pagination example snippets
use Esi\Pagination\Paginator;
use function array_slice;
use function filter_input;
use const FILTER_VALIDATE_INT;
use const INPUT_GET;
// Build a mock list of items we want to paginate through.
$items = [
'Banana',
'Apple',
'Cherry',
'Lemon',
'Pear',
'Watermelon',
'Orange',
'Grapefruit',
'Blackcurrant',
'Dingleberry',
'Snosberry',
'Tomato',
];
// Instantiate a new paginator service.
$paginator = new Paginator();
// Set some parameters (optional).
$paginator
->setItemsPerPage(10) // Give us a maximum of 10 items per page.
->setPagesInRange(5) // How many pages to display in navigation (e.g. if we have a lot of pages to get through).
;
// Pass our item total callback.
$paginator->setItemTotalCallback(static function () use ($items): int {
return \count($items);
});
// Pass our slice callback.
$paginator->setSliceCallback(static function (int $offset, int $length) use ($items): array {
return array_slice($items, $offset, $length);
});
// Paginate the collection, passing the current page number (e.g., from the current request).
$pagination = $paginator->paginate(filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT));
// Ok, from here on is where we would be inside a template or view (e.g., pass $pagination to your view).
// Iterate over the items on this page.
foreach ($pagination->getItems() as $item) {
echo \sprintf('%s<br />', $item);
}
// Build a basic page navigation structure.
foreach ($pagination->getPages() as $page) {
echo \sprintf('<a href="?page=%1$d">%1$d</a> ', $page);
}
use Esi\Pagination\Paginator;
use mysqli;
use function filter_input;
use const FILTER_VALIDATE_INT;
use const INPUT_GET;
// Instantiate a new paginator service.
$paginator = new Paginator();
// Set some parameters (optional).
$paginator
->setItemsPerPage(10) // Give us a maximum of 10 items per page.
->setPagesInRange(5) // How many pages to display in navigation (e.g., if we have a lot of pages to get through).
;
// Connect to a database.
$mysql = new mysqli('localhost', 'root', 'password', 'myDatabase');
// Pass our item total callback.
$paginator->setItemTotalCallback(static function () use($mysql): int {
// Run count query.
$result = $mysql->query("SELECT COUNT(*) AS `totalCount` FROM `TestData`");
$row = $result->fetch_array(MYSQLI_ASSOC);
// Return the count, cast as an integer.
return (int) $row['totalCount'];
});
// Pass our slice callback.
$paginator->setSliceCallback(static function (int $offset, int $length) use ($mysql): array {
// Run slice query.
$result = $mysql->query("SELECT `Name` FROM `TestData` LIMIT $offset, $length");
// Build a collection of items.
$collection = [];
while ($row = $result->fetch_assoc()) {
$collection[] = $row;
}
// Return the collection.
return $collection;
});
// Paginate the item collection, passing the current page number (e.g. from the current request).
$pagination = $paginator->paginate(filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT));
// Ok, from here on is where we'd be inside a template of view (e.g. pass $pagination to your view).
// Iterate over the items on this page.
foreach ($pagination->getItems() as $item) {
echo sprintf('%s<br />', $item['Name']);
}
// Let's build a basic page navigation structure.
foreach ($pagination->getPages() as $page) {
echo \sprintf('<a href="?page=%1$d">%1$d</a> ', $page);
}
$paginator = new Paginator([
'itemTotalCallback' => static function (): int {
// ...
},
'sliceCallback' => static function (int $offset, int $length): array {
// ...
},
'itemsPerPage' => 10,
'pagesInRange' => 5,
]);
if (\count($pagination) > 0) {
foreach ($pagination as $item) {
echo \sprintf('%s<br />', $item);
}
}
use Esi\Pagination\Pagination;
use function array_slice;
use function filter_input;
use function var_dump;
use const FILTER_VALIDATE_INT;
use const INPUT_GET;
// ...
$paginator->setItemTotalCallback(static function (Pagination $pagination) use ($items): int {
// Pass arbitrary metadata to pagination object.
$pagination->setMeta(['my', 'meta', 'data']);
return \count($items);
});
$paginator->setSliceCallback(static function (int $offset, int $length, Pagination $pagination) use ($items): array {
// Pass more arbitrary metadata to pagination object.
$pagination->setMeta(array_merge($pagination->getMeta(), ['more', 'stuff']));
return array_slice($items, $offset, $length);
});
// ...
// Perform the pagination
$pagination = $paginator->paginate(filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT));
// Get the metadata from the pagination object.
var_dump($pagination->getMeta());
// Render the first page link,
echo \sprintf('<a href="?page=%d">First Page</a> ', $pagination->getFirstPageNumber());
// Render the previous page link (note: the previous page number could be null),
echo \sprintf('<a href="?page=%d">Previous Page</a> ', $pagination->getPreviousPageNumber());
// Render page range links,
foreach ($pagination->getPages() as $page) {
echo \sprintf('<a href="?page=%1$d">%1$d</a> ', $page);
}
// Render the next page link (note: the next page number could be null),
echo \sprintf('<a href="?page=%d">Next Page</a> ', $pagination->getNextPageNumber());
// Render the last page link,
echo \sprintf('<a href="?page=%d">Last Page</a>', $pagination->getLastPageNumber());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.