Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.