分页脚本不显示超过3个链接

时间:2013-02-12 03:12:19

标签: php pagination

我使用下面的php脚本从mysql生成注释,并同时对其进行分页。我在数据库中有19行,并且我在变量中每页设置了5条注释。所以我应该有4个链接,即[1] [2] [3] [4]。但我只得到3个链接 。我没有得到任何错误。即使我将每页设置为2,我仍然可以获得3个链接。

 <?php    
         $per_page = 5;
         $total_query = $query = mysql_query("SELECT COUNT(*) FROM comments ")  or die (mysql_error());

         $pages =  ceil(mysql_result($total_query, 0) / $total_query);

         $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;

         $start = ($page - 1) *  $per_page ;

         $query = mysql_query("SELECT * FROM comments LIMIT $start, $per_page")  or die (mysql_error());   

         while ($comment = mysql_fetch_assoc($query)) {

         <?php echo $comment['owner'] ; ?>

         <?php echo htmlspecialchars($comment['body']) ;?>

         <?php $date = date_create($comment['created']); 
         echo  date_format($date, 'F j, Y g:i a'); ?>  

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

     for ($x = 1; $x<=$pages; $x++) {

     echo ($x == $page)  ? '<a href="http://127.0.0.1/page.php?page='.$x.'">' . $x 

.'</a> </span>' : '<a href="http://127.0.0.1/page.php?page='.$x.'"> [' . $x .' ]</a> ';

  }
  }
  ?>  

1 个答案:

答案 0 :(得分:2)

你的专栏:

$pages =  ceil(mysql_result($total_query, 0) / $total_query);

得到的结果除以...... $total_query,应为$per_page