We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Add Range to Paginator

GitHub link

Add Range to Paginator like this

use Phalcon\Paginator\Adapter\Model as Paginate;
$page = new Paginate(
    array(
        "data"        => Class::find(),
        "limit"       => 40,
        "page"        => 1,
        "rangeLength" => 5                //Add range length
    )
);

And $page->getPaginate() returned

Array[11]
    items       => Array[40]
                    ...
    first       => "1"
    before      => "1"
    current     => "1"
    last        => "5"
    next        => "2"
    limit       => "40"
    rangeLength => "5"
    totalPages  => "5"
    totalItems  => "200"
    range => Array[5]
        0 => "1"
        1 => "2"
        2 => "3"
        3 => "4"
        4 => "5"

Probably necessary combine all of the variables in the array pagination

Array[11]
    items       => Array[40]
                    ...
    pagination = Array (10)
        first       => "1"
        before      => "1"
        current     => "1"
        last        => "5"
        next        => "2"
        limit       => "40"
        rangeLength => "5"
        totalPages  => "5"
        totalItems  => "200"
        range => Array[5]
            0 => "1"
            1 => "2"
            2 => "3"
            3 => "4"
            4 => "5"

I'm not clear on exactly what "range" is.

lol you can write simple only use if

<?php
    $url = $this->router->getControllerName().'/'.$this->router->getActionName();
    $number = ($pagin->total_pages > 4) ? 5 : $pagin->total_pages;
    if ($pagin->total_pages > 1):
?>
<div class="text-right">
    <nav>
        <ul class="pagination">
            <li<?= ($pagin->before == $pagin->current) ? ' class="disabled"' : '' ?>>
                <a href="<?= $this->url->get($url."/".$pagin->before); ?>" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            <?php
                if ($pagin->current > 2 && $pagin->total_pages > 4 && $pagin->last > ($pagin->current + 2)) {
                    for($i = $pagin->current; $i < ($pagin->current + 5); $i++) {
                        $j = $i - 2;
                        $active = ($pagin->current == $j) ? ' class="active"' : '';
                        echo '<li' . $active . '><a href="' . $this->url->get($url . "/" . $j) . '">' . $j . '</a></li>';
                    }
                }
                elseif ($pagin->current > 2 && $pagin->total_pages > 4 && $pagin->last < ($pagin->total_pages + 2)) {
                    for($i = ($pagin->total_pages - 4); $i < ($pagin->total_pages + 1); $i++) {
                        $j = $i - 2;
                        $active = ($pagin->current == $i) ? ' class="active"' : '';
                        echo '<li' . $active . '><a href="' . $this->url->get($url . "/" . $i) . '">' . $i . '</a></li>';
                    }
                }
                else {
                    for($i = 1; $i < ($number + 1); $i++) {
                        $active = ($pagin->current == $i) ? ' class="active"' : '';
                        echo '<li' . $active . '><a href="' . $this->url->get($url . "/" . $i) . '">' . $i . '</a></li>';
                    }
                }
            ?>
            <li<?= ($pagin->last == $pagin->current) ? ' class="disabled"' : '' ?>>
                <a href="<?= $this->url->get($url."/".$pagin->next); ?>" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        </ul>
    </nav>
</div>
<?php endif ?>

but i can't rember what varibels $number do