Limit the number of pages in the pagination

Asked

Viewed 42 times

1

I have a pagination that displays many pages.inserir a descrição da imagem aqui

I would like to know how I can limit this outcome. For example if the user is on the page [5] , I’d like you to show up [3][4] 5 [6][7] always two links before and after the page that he is

    <?php
    session_start();
    require '../conectaBanco.php';

    //Verificar se esta sendo passado na URL a pagina atual, senao  atribuida a pagina 
    $pagina = (isset($_GET['pagina'])) ? $_GET['pagina'] : 1;

    //Selecionar todos os produtos da tabela
    $sql = "SELECT * FROM produtos";
    $query = mysqli_query($conecta, $sql);

    //Contar o total de produtos
    $total_linhas = mysqli_num_rows($query);

    //Seta a quantidade de produtos por pagina
    $quantidade_pg = 1;

    //calcular o numero de pagina necessarias para apresentar os produtos
    $num_pagina = ceil($total_linhas / $quantidade_pg);

    //Calcular o inicio da visualizacao
    $incio = ($quantidade_pg * $pagina) - $quantidade_pg;

    //Selecionar os produtos a serem apresentado na pagina
    $sql = "SELECT * FROM produtos limit $incio, $quantidade_pg";
    $query = mysqli_query($conecta, $sql);
    $total_linhas = mysqli_num_rows($query);
    ?>

Displays the pagination

            <?php
            //Verificar a pagina anterior e posterior
            $pagina_anterior = $pagina - 1;
            $pagina_posterior = $pagina + 1;
            ?>
            <div id="bottom" class="row">
                <div class="col-md-12">
                    <nav class="text-center">
                        <ul class="pagination">
                            <li>
                                <?php if ($pagina_anterior != 0) { ?>
                                    <a href="produtos.php?pagina=<?php echo $pagina_anterior; ?>" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                <?php } else { ?>
                                    <span aria-hidden="true">&laquo;</span>
                                <?php } ?>
                            </li>
                            <?php
                            //Apresentar a paginacao
                            for ($i = 1; $i < $num_pagina + 1; $i++) {
                                ?>
                                <li><a href="produtos.php?pagina=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                            <?php } ?>
                            <li>
                                <?php if ($pagina_posterior <= $num_pagina) { ?>
                                    <a href="produtos.php?pagina=<?php echo $pagina_posterior; ?>" aria-label="Previous">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                <?php } else { ?>
                                    <span aria-hidden="true">&raquo;</span>
                                <?php } ?>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div> 
  • 1

    I’m not answering your question, I’m just giving a hint based on my own experience. You are using select * from products in the count query. If the table is small and with few columns it is not a problem but in large tables prefer to use a select Count(id) from products where id is the name of the column q is primary key. This returns a resultset which you take using $line=mysql_fetch_assoc( $rs) and then $line['Count']. Or to use it the way you’re using it now, select id from products works the same way with mysqli_num_rows, only without taking all the columns.

  • Thanks for the tip Antonioalexandre.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.