GlenR
GlenR

Reputation: 95

PHP pagination with page numbers

I have code that I have used on several sites to deal with pagination. The problem I'm facing is that the code only give 'next' and 'previous' links. For the site I am working on I need page numbers too, not them all, maybe 5 at a time, kin of like this < 1 2 3 4 5 > then when you get to page 5 < 6 7 8 9 10 >

This is my pagination code so farf

//paganation settings
$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `products` WHERE subcat = 1 AND 
                            status = 1") or die(mysqli_error($link));
$result = mysqli_fetch_array($pages_query, MYSQLI_NUM);
$pages = $result[0] / $per_page;
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$last = ($pages - 1) / $per_page;
$prev = $page - 1;                                                      
$next = $page + 1; 
//query the db
$q =mysqli_query($link, "SELECT * FROM products WHERE subcat = 1 AND status = 1
          ORDER BY id DESC LIMIT    $start, $per_page");
if(mysqli_num_rows($q) > 0){
//find out the page we are on and display next and previous links accordingly
   if ($page >= 1){ 
      if ($page < $pages) {
         echo "<span class='next'>";
          echo "<a href=\"?page={$next}\">Next</a> ";
          echo "</span>";
      }
      if ($page >=2){
         echo "<span class='prev'>";
         echo "<a href=\"?page={$prev}\">Back</a> ";
         echo "</span>";
      }
}   
else if ($page > $pages + 1){
   echo 'No more images in the database';
}

Can anyone help me add the page numbers in here

Thanks

Upvotes: 0

Views: 4862

Answers (1)

Vaibhav Malve
Vaibhav Malve

Reputation: 116

for ($i=1; $i<=$total_pages; $i++) {  
         $pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";  
};

This might hel you put it between if loops for next and prev page

Upvotes: 1

Related Questions