Put page numbers in PHP page with MYSQL [duplicate]

0

How could I put the page numbers in this paging script with PHP and Mysql?

As it looks like this:

First Prev 1 2 3 4 5 6 7 8 9 Next Last

<?php

        if (isset($_GET['pageno'])) {
            $pageno = $_GET['pageno'];
        } else {
            $pageno = 1;
        }
        $no_of_records_per_page = 10;
        $offset = ($pageno-1) * $no_of_records_per_page;

        $conn=mysqli_connect("xx","buscaiuv_busca","xx@","xx");
        // Check connection
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            die();
        }

        $total_pages_sql = "SELECT COUNT(*) FROM empresas";
        $result = mysqli_query($conn,$total_pages_sql);
        $total_rows = mysqli_fetch_array($result)[0];
        $total_pages = ceil($total_rows / $no_of_records_per_page);

        $sql = "SELECT * FROM empresas LIMIT $offset, $no_of_records_per_page";
        $res_data = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($res_data)){
            echo $row['nome']."<br>";
        }
        mysqli_close($conn);
    ?>
    <ul class="pagination">
        <li><a href="?pageno=1">First</a></li>
        <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
            <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
        </li>
        <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
            <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
        </li>
        <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
    </ul>
    
asked by anonymous 29.05.2018 / 15:11

1 answer

1

You need to create a loop inside <ul> . See:

<ul class="pagination">
        <li><a href="?pageno=1">First</a></li>
        <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
            <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
        </li>


        <!-- AQUI COMEÇA O LOOP -->
        <!-- A PARTIR DAQUI COMEÇA A PAGINAÇÃO -->

        <?php for($x = 1; $x <= $total_pages; $x++){ ?>

         <li class="<?php if($pageno == $x){ echo 'disabled'; } ?>">
            <a href="<?php if($pageno == $x){ echo '#'; } else { echo "?pageno=".$x; } ?>"><?php echo $x; ?></a>
        </li>

        <?php } ?>

        <!-- AQUI TERMINA O LOOP -->


        <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
            <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + $x); } ?>">Next</a>
        </li>
        <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
 </ul>
    
29.05.2018 / 17:16