【问题标题】:How to get query to transfer to subsquent pages when paginating results分页结果时如何获取查询以转移到后续页面
【发布时间】:2011-03-19 02:01:44
【问题描述】:

我浏览了网站上所有的分页问题和答案,在所有冗长的代码和OO解决方案中,这段代码是最短和最简单的:

<?php  
// insert your mysql connection code here  
$perPage = 10; 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
$startAt = $perPage * ($page - 1);  
$query = "SELECT COUNT(*) as total FROM table"; 
$r = mysql_fetch_assoc(mysql_query($query));  
$totalPages = ceil($r['total'] / $perPage);  
$links = ""; 
for ($i = 1; $i <= $totalPages; $i++) { $links .= ($i != $page )  ? "<a href='index.php?page=$i'>Page $i</a> " : "$page "; }   
$query = "SELECT * FROM table ORDER BY title LIMIT $startAt, $perPage";  
$r = mysql_query($query);  
// display results here the way you want  
echo $links; // show links to other pages 

但我仍然看不到如何在后续页面上使用更新的LIMIT 重新生成查询。这些消息都没有说明这一点,无论我尝试哪种代码,我都会继续得到空白的第二页。如果有人能解释如何将查询结果带到下一页,我将不胜感激。

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    我认为您必须在查询中使用 OFFSET 令牌。像这样:

    $query = "SELECT * FROM table ORDER BY title LIMIT $perPage OFFSET $perPage * $page"; 
    

    我希望这会有所帮助。

    【讨论】:

    • 范围被提供给查询,它有开始的记录号和要显示的记录数。问题是如何获取所有查询信息和表结构以显示结果以跟进到后续页面。
    • 您确定问题出在您发布的代码中吗?我试过了,对我来说效果很好。
    • 我的查询搜索词最初是使用 POST 提取的。我假设由于在访问链接时使用 GET 来获取页码,因此不会重新运行查询,因为它没有所需的搜索词,这听起来对吗?只是将搜索词添加到链接页面的 href 中吗?如果是这样,我应该添加什么?尝试将 &?search=$searchterm 添加到 但这显然是不对的。
    • @Chelle:为了避免数据传递的问题,我总是使用$_REQUEST 而不是$_GET$_POST。如果您为显示的每个页面运行查询,则必须以某种方式发送搜索词。如果您使用包含空格的字符串作为搜索词,则必须对其进行编码。这可能是问题所在。哟应该试试这样的链接:&lt;a href='index.php?page=&lt;?php echo $i; ?&gt;&amp;search=&lt;?php echo url_encode($searchterm); ?&gt;'&gt;
    【解决方案2】:

    不知道为什么每次都需要选择 count(*)。我建议这样做:

    <?php
    /* establish the connection */
    $mysqli = new mysqli (
                'localhost',  // The host to connect to
                'username',       // The user to connect as
                'password',   // The password to use
                'dbname');     // The default database to query
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s<br />\n", mysqli_connect_error());
        exit();
    }
    
    $perPage = 10;
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $startAt = $perPage * ($page - 1);
    
    /* Send a query to the server */
    if ($result = $mysqli->query(
         "SELECT * FROM table ORDER BY title limit $startAt, $perPage")) {
        $rowCnt = $result->num_rows;
        echo "<h3>Page: $page</h3>\n";
    
        /* Fetch the results of the query and show results*/
        while( $row = $result->fetch_assoc() ) {
            printf("%s - %s<br/>\n", $row['orderDate'], $row['orderDescription']);
        }
    
        /* Show next page, previous page links dynamically */
        echo "<p>\n";
        // generate previous page link if current page no is after 1st page
        if ($page > 1)
            printf("<a href='%s?page=%d'>Previous Page</a>&nbsp;&nbsp;&nbsp;\n", 
               $_SERVER["SCRIPT_NAME"], ($page-1));
    
        // generate next page link if we were able to fetch $perPage rows    
        if ($rowCnt == $perPage) 
            printf("<a href='%s?page=%d'>Next Page</a>\n", 
               $_SERVER["SCRIPT_NAME"], ($page+1));
        echo "</p>\n";
    
        /* Destroy the result set and free the memory used for it */
        $result->close();
    }
    
    /* close the connection */
    $mysqli->close();
    ?>
    

    我正在使用php的新mysqli扩展,这里是php mysqli reference documentation

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多