douzhu6149 2017-05-11 17:10
浏览 35
已采纳

PHP分页无法正常工作

So basically i created a webpage with url:

http://localhost/bbe/alumni/showthread.php?t=19
$limit = 8;
$offset = (isset($_GET["page"]) ? $_GET["page"] - 1 : 0) * $limit;

Then put a comment section (with paging) but my codes for paging is not working

<?php
        $total = $dbcon->query("SELECT count(*) FROM comment") or die(mysqli_error());
        $fetch = $total->fetch_assoc();


        for($x = 0; $x < $fetch["count(*)"] / $limit ; $x ++)
        {
          $page = $x + 1;
          if((isset($_GET["page"]) ? $_GET["page"] : 1) == $page) $page = "<b>".$page."</b>";
          echo '<a href="showthread.php?t='.$id.'?page='.($x + 1).'" class="label label-danger">'.$page.'</a>&nbsp;';
        }
      ?>

after 8 comments there is the page number but when i clicked the link for page 2 nothing happens except for the url is changed to:

http://localhost/bbe/alumni/showthread.php?t=19?page=2

and also if i clicked back to page 1 the url changed to

http://localhost/bbe/alumni/showthread.php?t=19?page=2?page=1

and so on.. why does the link in my paging doesn't work? sorry im new in php and still learning online

  • 写回答

2条回答 默认 最新

  • du656637962 2017-05-11 20:04
    关注

    As I already commented, additional request variables need to be separated by &, not ?. So the url should become http://localhost/bbe/alumni/showthread.php?t=19&page=2.

    The correct code for building the link should probably look like this:

    echo '<a href="showthread.php?t='.$id.'&page='.($x + 1).'" class="label label-danger">'.$page.'</a>&nbsp;';
    

    Or, while I'm at it, a bit cleaner imo would be something like this:

    $requestedPage = isset($_GET["page"]) ? $_GET["page"] : 1;
    
    for($page = 1; $page <= $fetch["count(*)"] / $limit; $page++) {
        // active page does not need to be a link
        if($requestedPage == $page) {
            echo "<strong>$page</strong>";
            continue;
        }
        echo "<a href='showthread.php?t=$id&page=$page' class='label label-danger'>$page</a>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装