PHP code example of motokraft / pagination

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

    

motokraft / pagination example snippets




use \Motokraft\HtmlElement\HtmlElement;
use \Motokraft\Pagination\Renderer\BaseRenderer;
use \Motokraft\Pagination\Renderer\RendererInterface;
use \Motokraft\Uri\Uri;

class DemoPagination extends BaseRenderer implements RendererInterface
{
    function render(HtmlElement $element) : HtmlElement
    {
        $result = $element->appendCreateHtml('div', [
            'class' => 'category-pagination-inner'
        ]);

        // Вывод счетчика навигации
        $this->renderCounter($result);

        // Вывод контейнера навигация
        $this->renderPagination($result);

        return $result;
    }

    private function renderCounter(HtmlElement $element)
    {
        $padding = $this->getPagination();

        $div = $element->appendCreateHtml('div', [
            'class' => 'pagination-legend-inner'
        ]);

        $total = $padding->getTotal();
        $limitstart = $padding->getLimitStart();
        $limit = $padding->getLimit();
        $toResult = $total;

        if (($limitstart + $limit) < $total)
        {
            $toResult = $limitstart + $limit;
        }

        if ($total > 0)
        {
            $div->html(sprintf(
                'Showing %s to %s of %s entries',
                ($limitstart + 1), $toResult, $total
            ));
        }
        else
        {
            $div->html('No matching records found');
        }
    }

    private function renderPagination(HtmlElement $element)
    {
        $uri = clone Uri::getInstance();

        $nav = $element->appendCreateHtml('nav', [
            'class' => 'pagination-nav-inner'
        ]);

        // Кнопка 'В начало'
        $first = $this->_prepareFirstItem();
        $first = $first->render($nav, clone $uri);

        $first->addClass('nav-item-inner');
        $first->addAttrAria('label', 'First');

        // Кнопка 'Назад'
        $prev = $this->_preparePrevItem();
        $prev = $prev->render($nav, clone $uri);

        $prev->addClass('nav-item-inner');
        $prev->addAttrAria('label', 'Prev');

        foreach($this->_getPages() as $page)
        {
            // Нумерация страниц
            $page = $page->render($nav, clone $uri);
            $page->addClass('nav-item-inner');
        }

        // Кнопка 'Вперед'
        $next = $this->_prepareNextItem();
        $next = $next->render($nav, clone $uri);

        $next->addClass('nav-item-inner');
        $next->addAttrAria('label', 'Next');

        // Кнопка 'В конец'
        $last = $this->_prepareLasttItem();
        $last = $last->render($nav, clone $uri);

        $last->addClass('nav-item-inner');
        $last->addAttrAria('label', 'Last');
    }
}

use \Motokraft\Pagination\Pagination;
use \Motokraft\HtmlElement\HtmlElement;

// Добавляем класс из примера выще
Pagination::addRenderer('demo', DemoPagination::class);

$div = new HtmlElement('div');

$pagination = new Pagination(367, 0, 10, 9);
$pagination->render($div, 'demo');

echo $div;

$ php composer