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条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable