Place page numbers in PHP paging with MYSQL

Asked

Viewed 101 times

0

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

The way it looks:

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>
  • What’s the problem with your code? Seems right to me.

  • does not display page numbers, only First Prev Next Last, when it should be First Prev 1 2 3 4 5 6 7 8 9 Next Last

1 answer

1


You need to create a loop within the <ul>. Behold:

<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>

Browser other questions tagged

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