doujiangao4229 2013-02-12 03:12
浏览 55
已采纳

分页脚本不显示超过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 2013-02-12 03:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况