Home » Php » pagination – Page view in php

pagination – Page view in php

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have to make a small adjustment on a website, the problem comes on the page.

For example I have a page with 11 button, these 1 I do not have to show them at once, but I have for example I have to show the first 5 and then the page has to be moving but I never have to stop showing 5 results on the page.

I leave reference images:

This is the way the page does NOT have to look:

This is the way the page does NOT have to look

This is the way the page should look:

This is the way the page should look

for example if I get to 7 that 3 is no longer shown and shows me 8

This is my code

    <nav>

    <ul class="pagination">
        <?php if ($pagina == 1) : ?>
            <li class="page-item disabled">
                <a class="page-link" href="">
                    <span class="ti-arrow-left"></span>
                </a>
            </li>
        <?php else : ?>
            <li class="page-item">
                <a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
                    <span class="ti-arrow-left"></span>
                </a>
            </li>
        <?php endif; ?>

        <!---------------------------------------------------------------------->

        <?php
        for ($i = 1; $i <= $numeroDePaginas; $i++) {
            if ($pagina == $i) {
                echo "<li class='page-item active'>
        <a class='page-link' href='?pagina=$i'>$i</a>
    </li>";
            } else {
                if (($i + 2) < $pagina || ($i - 2) > $pagina) {
                    echo "<li class='page-item'>
        <a class='page-link' href='?pagina=$i'>$i</a>
        </li>";
                }
            }
        }
        ?>

        <!---------------------------------------------------------------------->

        <?php if ($pagina == $numeroDePaginas) : ?>
            <li class='page-item disabled'>
                <a class='page-link' href=''>
                    <span class='ti-arrow-right'></span>
                </a>
            </li>
        <?php else : ?>
            <li class='page-item'>
                <a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
                    <span class='ti-arrow-right'></span>
                </a>
            </li>
        <?php endif; ?>

    </ul>
</nav>
How to&Answers:

Here you go. I have added next_pages variable and previous_pages variables, which will make sure to get the pagina – 2 and pagina + 2 if exists. Let me know if you need help understanding it.

    <nav>

    <?php
    $next_pages = ($pagina + 2) <= $numeroDePaginas ? $pagina + 2 : $numeroDePaginas;
    $previous_pages = ($pagina - 2) >= 1 ? $pagina - 2 : 1;
    ?>
    <ul class="pagination">
        <?php if ($pagina == 1) : ?>
            <li class="page-item disabled">
                <a class="page-link" href="">
                    <span class="ti-arrow-left"></span>
                </a>
            </li>
        <?php else : ?>
            <li class="page-item">
                <a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
                    <span class="ti-arrow-left"></span>
                </a>
            </li>
        <?php endif; ?>

        <!---------------------------------------------------------------------->

        <?php
        for ($i = $previous_pages; $i <= $next_pages; $i++) {
            if ($pagina == $i) {
                echo "<li class='page-item active'>
    <a class='page-link' href='?pagina=$i'>$i</a>
</li>";
            } else {
                     echo "<li class='page-item'>
    <a class='page-link' href='?pagina=$i'>$i</a>
    </li>";
             }
        }
        ?>

        <!---------------------------------------------------------------------->

        <?php if ($pagina == $numeroDePaginas) : ?>
            <li class='page-item disabled'>
                <a class='page-link' href=''>
                    <span class='ti-arrow-right'></span>
                </a>
            </li>
        <?php else : ?>
            <li class='page-item'>
                <a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
                    <span class='ti-arrow-right'></span>
                </a>
            </li>
        <?php endif; ?>

    </ul>
</nav>

Edit with more explaination:

I have added a variable called next_pages, this variable checks if the current page (ex: 9) + 2 is smaller than or equal to “numeroDePaginas” variable, I have done this to make sure that we never show any pages exceeding the number of pages we have, in case 9+2 is bigger than “numeroDePaginas” we set the variable equal to “numeroDePaginas”

For the previous_pages variable, we make sure that the current page(ex: 3) – 2 is never less than 1, (so it doesn’t show 0 or negative pages for example), in case

3-2 is smaller than 1, we set the variable to 1.

Now, I have edited the for loop, to make sure that it starts from the previous_pages variable, and ends with next_pages variable.

Here are a test case:

$pagina = 7;
$numeroDePaginas = 9;

//$next_pages will be = 9  (i.e 7+2)
//$previous_pages will be = 5 (i.e 7-2) 

for loop would print pages from 5 to 9.