PHP code example of bentools / pager

1. Go to this page and download the library: Download bentools/pager 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/ */

    

bentools / pager example snippets


use BenTools\Pager\Model\Pager;

foreach (new Pager($perPage, $currentPageNumber, $numFound) as $page) {
    $page->getPageNumber(); // Returns the page number
    $page->count(); // Returns the number of items the page contains
}

use BenTools\Pager\Model\Pager;

foreach (new Pager($perPage, $currentPageNumber, $numFound) as $page) {
    (string) $page; // $page->getPageNumber() shortcut
    count($page); // $page->count() shortcut
}

# http://localhost/?page_number=3

PageParameterUrlBuilder;

$perPage = 10;
// It will look for a page_number param in the request URI (and sets current page to #1 if not found)
$urlBuilder = PageParameterUrlBuilder::fromRequestUri($perPage, 'page_number'); 
$pager = $urlBuilder->createPager();
$pager->setNumFound(53);


printf('Total number of pages: %s' . PHP_EOL, count($pager));
printf('Current page number: %s' . PHP_EOL, $pager->getCurrentPage());

print PHP_EOL;

printf('First page number: %s' . PHP_EOL, $pager->getFirstPage());
printf('Previous page number: %s' . PHP_EOL, $pager->getPreviousPage());
printf('Next page number: %s' . PHP_EOL, $pager->getNextPage());
printf('Last page number: %s' . PHP_EOL, $pager->getLastPage());

print PHP_EOL;

foreach ($pager as $page) {
    printf(
            'Page %s contains %d items. - Url: %s' . PHP_EOL, 
            $page, 
            count($page), 
            $pager->getUrl($page)
        );
}

# http://localhost/?page=30

ltaPager;
use BenTools\Pager\Model\Factory\PageParameterUrlBuilder;

$perPage = 10;
$pager = PageParameterUrlBuilder::fromRequestUri($perPage)->createPager();
$pager->setNumFound(500);


printf('Total number of pages: %s' . PHP_EOL, count($pager));
printf('Current page number: %s' . PHP_EOL, $pager->getCurrentPage());

print PHP_EOL;

printf('First page number: %s' . PHP_EOL, $pager->getFirstPage());
printf('Previous page number: %s' . PHP_EOL, $pager->getPreviousPage());
printf('Next page number: %s' . PHP_EOL, $pager->getNextPage());
printf('Last page number: %s' . PHP_EOL, $pager->getLastPage());

print PHP_EOL;

$previous = null;
$delta = 2;
foreach (new DeltaPager($pager, $delta) as $page) {
    if (null !== $previous && $previous->getPageNumber() != $page->getPageNumber() - 1) {
        print '...' . PHP_EOL;
    }
    printf('Page %s' . PHP_EOL, $page);
    $previous = $page;
}