Jump to content

Page pagination problem


Conf1

Recommended Posts

So this is my code for pagination

 

<?php 

          $num_rec_per_page=8;

          if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 

          $start_from = ($page-1) * $num_rec_per_page; 

          $sql="SELECT * FROM `images` ORDER BY `id` DESC LIMIT $start_from,$num_rec_per_page";
          $rdata=mysqli_query($mysqli, $sql);
          while($res=mysqli_fetch_array($rdata))
          {
          $imgname=$res['image'];
          $path = 'folder/'.$imgname;
          echo "
          <div class='col-xs-6 col-md-3'>
          <a href='#'' class='thumbnail'>
          <img src='$path' height='500' width='500'>
          </a>
          </div>";

          }

          $total_records = mysqli_num_rows($rdata);  //count number of records
          $total_pages = ceil($total_records / $num_rec_per_page); 

  

          for ($i=1; $i<=$total_pages; $i++) { 
                      
          }; 

          

          ?>


      

    <br><br>
    <footer>
    <br><br>
      <div class="well text-center">
          a href="<?php echo "index.php?page=1"; ?>"><button type="button" class="btn btn-fresh text-uppercase">Previous page</button></a>
          <a href="<?php echo "index.php?page=".$i." "; ?>"><button type="button" class="btn btn-sky text-uppercase">Next page</button></a>
      </div>

Now when I press "Next page" button, I can go to only 2 page, not more, but there must be more pages because there is more posts..

Link to comment
https://forums.phpfreaks.com/topic/297102-page-pagination-problem/
Share on other sites

In addition to what fastsol said, the following line is only going to give you the number of records retrieved from your query:

$total_records = mysqli_num_rows($rdata);  //count number of records

Since the query uses the LIMIT clause, $total_records is never going to be more than 8. To set $total_records, you could create a separate query that counts all the records in the "images" table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.