PHP code example of ashleydawson / simple-pagination
1. Go to this page and download the library: Download ashleydawson/simple-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/ */
ashleydawson / simple-pagination example snippets
use AshleyDawson\SimplePagination\Paginator;
// Build a mock list of items we want to paginate through
$items = array(
'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(function () use ($items) {
return count($items);
});
// Pass our slice callback
$paginator->setSliceCallback(function ($offset, $length) use ($items) {
return array_slice($items, $offset, $length);
});
// Paginate the item collection, passing the current page number (e.g. from the current request)
$pagination = $paginator->paginate((int) $_GET['page']);
// 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 $item . '<br />';
}
// Let's build a basic page navigation structure
foreach ($pagination->getPages() as $page) {
echo '<a href="?page=' . $page . '">' . $page . '</a> ';
}
use AshleyDawson\SimplePagination\Paginator;
// 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(function () {
// Run count query
$result = mysql_query("SELECT COUNT(*) FROM `TestData`");
// Return the count (the value of the first result column), cast as an integer
return (int) mysql_result($result, 0);
});
// Pass our slice callback
$paginator->setSliceCallback(function ($offset, $length) {
// Run slice query
$result = mysql_query("SELECT `Name` FROM `TestData` LIMIT {$offset}, {$length}");
// Build a collection of items
$collection = array();
while ($row = mysql_fetch_assoc($result)) {
$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((int) $_GET['page']);
// 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 $item['Name'] . '<br />';
}
// Let's build a basic page navigation structure
foreach ($pagination->getPages() as $page) {
echo '<a href="?page=' . $page . '">' . $page . '</a> ';
}
$paginator = new Paginator(array(
'itemTotalCallback' => function () {
// ...
},
'sliceCallback' => function ($offset, $length) {
// ...
},
'itemsPerPage' => 10,
'pagesInRange' => 5
));
if (count($pagination) > 0) {
foreach ($pagination as $item) {
echo $item . '<br />';
}
}
use AshleyDawson\SimplePagination\Pagination;
// ...
$paginator->setItemTotalCallback(function (Pagination $pagination) use ($items) {
// Pass arbitrary metadata to pagination object
$pagination->setMeta(['my', 'meta', 'data']);
return count($items);
});
$paginator->setSliceCallback(function ($offset, $length, Pagination $pagination) use ($items) {
// 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((int) $_GET['page']);
// Get the metadata from the pagination object
var_dump($pagination->getMeta());
// Render the first page link
echo '<a href="?page=' . $pagination->getFirstPageNumber() . '">First Page</a> ';
// Render the previous page link (note: the previous page number could be null)
echo '<a href="?page=' . $pagination->getPreviousPageNumber() . '">Previous Page</a> ';
// Render page range links
foreach ($pagination->getPages() as $page) {
echo '<a href="?page=' . $page . '">' . $page . '</a> ';
}
// Render the next page link (note: the next page number could be null)
echo '<a href="?page=' . $pagination->getNextPageNumber() . '">Next Page</a> ';
// Render the last page link
echo '<a href="?page=' . $pagination->getLastPageNumber() . '">Last Page</a>';
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.