PHP code example of delight-im / pagination

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

    

delight-im / pagination example snippets


     

$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
);

$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
);

$controls = $pagination->build();

// or

$numberOfControls = 11;
$showFirstLast = true;
$showPreviousNext = true;
$previousLabel = '« Previous';
$nextLabel = 'Next »';
$controls = $pagination->build(
    $numberOfControls,
    $showFirstLast,
    $showPreviousNext,
    $previousLabel,
    $nextLabel
);

foreach ($controls as $control) {
    // $control->getLabel();
    // $control->getPage();
    // $control->isSelected();
    // $control->isDisabled();
}

$pagination->getOffset();
// int(1000)

$pagination->getLimit();
// int(25)

$sql = "SELECT * FROM products LIMIT " . $pagination->getOffset() . ", " . $pagination->getLimit();

// or

$sql = "SELECT * FROM products LIMIT " . $pagination->getLimit() . " OFFSET " . $pagination->getOffset();