0

I have a pagination script which works. The first page has a 'next' link, the last page should have a 'previous' link and the ones in between have both. The problem is that the last page doesnt show it 'previous' link.

Heres the code

include '../inc/connect.php';
//paganation settings
$per_page = 2;
$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `gallery`") 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 gallery 
                         ORDER BY id DESC 
             LIMIT $start, $per_page");
//find out the page we are on and display next and previous links accordingly
if ($page >= 1 && $page < $pages){ 
   echo "<span class='next'>";
   echo "<a href=\"?page={$next}\">Next page</a> ";
   echo "</span>";
   if ($page >=2){
      echo "<span class='prev'>";
  echo "<a href=\"?page={$prev}\">Previous page</a> ";
  echo "</span>";
   }
}       
else if ($page > $pages + 1){
   echo 'No more images in the database';
}
//display images
while($row=mysqli_fetch_array($q)){
  echo "<a href='{$row['filename']}' rel='thumbnail'>
        <img class='nailthumb-container' src='{$row['filename']}'  
        alt='{$row['description']}.Image' title='{$row['description']}' />
        </a>";
 }
4

2 回答 2

0

我认为将其更改为这将起作用,在您的最后一页上 $page 等于 $pages 而不是小于 $pages:

if ($page >= 1 && $page <= $pages){...}
于 2013-09-27T14:42:43.397 回答
0

The last page link is falling under the ($page < $pages) test, and it probably shouldn't.

if ($page >= 1){ 
    if ($page < $pages) {
   echo "<span class='next'>";
   echo "<a href=\"?page={$next}\">Next page</a> ";
   echo "</span>";
   }
   if ($page >=2){
      echo "<span class='prev'>";
  echo "<a href=\"?page={$prev}\">Previous page</a> ";
  echo "</span>";
   }
} 
于 2013-09-27T14:43:23.393 回答