Php分页链接未在最后一页显示

时间:2013-09-27 14:38:03

标签: php pagination

我有一个有效的分页脚本。第一页有一个“下一个”链接,最后一页应该有一个“上一个”链接,其中两个链接都有。问题是最后一页没有显示它是“上一个”链接。

继承代码

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>";
 }

2 个答案:

答案 0 :(得分:0)

将其更改为此功能我认为,在您的最后一页$ page等于$ pages而不是小于$ pages:

if ($page >= 1 && $page <= $pages){...}

答案 1 :(得分:0)

最后一页链接属于($ page&lt; $ pages)测试,可能不应该。

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>";
   }
}