How do I limit pages in the EX pagination: 1,2,3,4.... , do not leave all numbers in view from 1 to 50

Asked

Viewed 369 times

1

<?php include_once("conexao.php");

$pagina = (isset($_GET['pagina']))? $_GET['pagina'] : 1;


$result_curso = "SELECT * FROM cursos";
$resultado_curso = mysqli_query($conn, $result_curso);


$total_cursos = mysqli_num_rows($resultado_curso);


$quantidade_pg = 6;


$num_pagina = ceil($total_cursos/$quantidade_pg);


$incio = ($quantidade_pg*$pagina)-$quantidade_pg;


$result_cursos = "SELECT * FROM cursos limit $incio, $quantidade_pg";
$resultado_cursos = mysqli_query($conn, $result_cursos);
$total_cursos = mysqli_num_rows($resultado_cursos);
?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Criar pagina com abas</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container theme-showcase" role="main">
            <div class="page-header">
                <h1>Cursos</h1>
            </div>
            <div class="row">
                <?php while($rows_cursos = mysqli_fetch_assoc($resultado_cursos)){ ?>
                    <div class="col-sm-6 col-md-4">
                        <div class="thumbnail">
                            <img src="imagens/produto.jpg" alt="...">
                            <div class="caption text-center">
                                <a href="detalhes.php?id_curso=<?php echo $rows_cursos['id']; ?>"><h3><?php echo $rows_cursos['nome']; ?></h3></a>
                                <p><a href="#" class="btn btn-primary" role="button">Comprar</a> </p>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
            <?php
                $pagina_anterior = $pagina - 1;
                $pagina_posterior = $pagina + 1;
            ?>
            <nav class="text-center">
                <ul class="pagination">
                    <li>
                        <?php
                        if($pagina_anterior != 0){ ?>
                            <a href="index.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="index.php?pagina=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                    <?php } ?>
                    <li>
                        <?php
                        if($pagina_posterior <= $num_pagina){ ?>
                            <a href="index.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>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
    </body>
</html>
No answers

Browser other questions tagged

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