【问题标题】:PHP paging not workingPHP分页不起作用
【发布时间】:2017-10-10 19:57:45
【问题描述】:

所以基本上我用 url 创建了一个网页:

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

然后放一个评论部分(带分页),但我的分页代码不起作用

<?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;';
        }
      ?>

8 cmets 后有页码,但是当我点击第 2 页的链接时,除了 url 更改为:

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

如果我点击返回第 1 页,则 url 更改为

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

等等..为什么我的分页中的链接不起作用?对不起,我是 php 新手,还在网上学习

【问题讨论】:

  • 额外的请求变量需要用&amp;分隔,而不是?,所以url应该变成http://localhost/bbe/alumni/showthread.php?t=19&amp;page=2。另外,考虑限制你的查询结果而不是在 PHP 中分页,随着数据量的增加,这将变得非常慢
  • @Pevara 分页现在可以工作,因为我用 & 符号分隔请求变量。但是我将如何限制我的查询结果?我仍然对它的真正含义感到困惑。 comment() 中的代码如下:"SELECT * FROM comment ORDER BY timestamp DESC LIMIT $offset,$limit"
  • 我的错误,我没有仔细阅读您的代码并假设您在哪里获取实际的 cmets,而您似乎只是在使用总数来构建寻呼机链接。如果这对您有用,则代码很好,无需限制查询结果。
  • @ronstoppable 你的问题是链接对吗?您能帮我解释一下您要解决的问题是什么
  • @MehdiBounya 是的,我的问题是链接。但它现在解决了。感谢佩瓦拉。我不知道不同的请求变量需要用&amp; 分隔,而不是?

标签: php


【解决方案1】:

正如我已经评论过的,额外的请求变量需要用&amp; 分隔,而不是?。所以 url 应该变成http://localhost/bbe/alumni/showthread.php?t=19&amp;page=2

构建链接的正确代码应该如下所示:

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

或者,当我这样做时,更清洁的 imo 会是这样的:

$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>";
}

【讨论】:

    【解决方案2】:

    使用LIMIT,您可以通过设置limitoffset 来限制每页显示的行数。

    $total = $dbcon->query("SELECT count(*) FROM comment") or die(mysqli_error());
    $num_rows = $total->fetch_assoc()["count(*)"]; // get row count
    
    $limit = 8;
    $lastPage = round(($num_rows / $limit) - 1); // get last page
    $current_page = isset($_GET['t']) ? $_GET['t'] : 0; // if not set the page is index.php
    $offset = $current_page * $limit; // get offset by multiplying the current page number by limit
    
    $sql = "SELECT * FROM comment LIMIT $offset, $limit";
    $result = $dbcon->query($sql); // get results using LIMIT
    
    // display results here
    while ($row = $result->fetch_assoc()) {
        // echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
    
    echo createNav($current_page, $lastPage); // creating pagination
    
    function createNav($pageNumber, $lastPage)
    {
        $listItems = '';
        while ($lastPage >= 1) {
            $pageNumber += 1;
            if ($pageNumber >= 1) {
                $listItems = createListItem($pageNumber, $lastPage) . $listItems;
            }
            $lastPage -= 1;
        }
        return $listItems;
    }
    
    function createListItem($pageNumber, $lastPage)
    {
        if ($pageNumber <= $lastPage && $lastPage > 0) {
            return '<a href="showthread.php?t=' . $pageNumber . '?&page=' . ($pageNumber + 1) . '">' . $pageNumber . '</a><br>';
        }
    }
    

    check this simple pagination

    【讨论】:

    • 不,您不能使用 $pageNumber 作为 t 的值。在return '&lt;a href="showthread.php?t=' . $pageNumber . '?&amp;page=' . ($pageNumber + 1) . '"&gt;' . $pageNumber . '&lt;/a&gt;&lt;br&gt;'; 中,因为 t 的值是当前线程的 id。无论如何,Pevara 在上面的评论中已经回答了这个问题。
    猜你喜欢
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多