doujiangao4229
doujiangao4229
2013-02-12 03:12

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

已采纳

I am using the php script below to generate comments from mysql, and also paginating it at the same time. I have 19 rows in the database, and I have set 5 comments per page in the variable. so I should have 4 links i.e. [1] [2] [3] [4] . but i am only getting 3 links . I do not get any error. even when I set per page to 2, I still get 3 links.

 <?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条回答

  • dtkjthe4025 dtkjthe4025 8年前

    Your line:

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

    is getting the result and dividing by... $total_query, which should be $per_page.

    点赞 评论 复制链接分享

相关推荐