【问题标题】:Limiting maximum page numbering and adding Previous and Next button限制最大页码并添加上一个和下一个按钮
【发布时间】:2021-08-31 12:48:42
【问题描述】:

我正在使用文本文件作为数据库而不是使用 mySQL 创建分页。已经完成了大部分,唯一的问题是页码本身。如何限制底部的最大页码?在我按最后一个页码后,还有什么要移动到下一组页面的吗?

是这样的PREV 1 2 3 4 5 NEXT

<?php
    
    // connect to txt database
    $file="database.txt";
    $con = file_get_contents($file);
    
    // define how many results you want per page
    $results_per_page = 5;
    
    // find out the number of results stored in database
    $linecount = 0;
    $handle = fopen($file, "r");
    while(!feof($handle)){
      $line = fgets($handle);
      $linecount++;
    }
    fclose($handle);
    $number_of_results = $linecount;
    
    // determine number of total pages available
    $number_of_pages = ceil($number_of_results/$results_per_page);
    
    // determine which page number visitor is currently on
    if (!isset($_GET['page'])) {
      $page = 1;
    } else {
      $page = $_GET['page'];
    }  
    
    // display the links to the pages
    for ($page=1;$page<=5;$page++) {
      echo '<a href="index.php?page=' . $page . '">' . $page . '</a> ';
    }
    
    ?>

【问题讨论】:

  • 是的,两者都可以。到目前为止,您尝试过什么?
  • 在我的代码中,所有页码都会出现。例如,我有 10 页,它们都显示在 1 行中。有没有办法限制它们?
  • 是的。这段代码是你写的吗? $page&lt;=$number_of_pages 控制限制,所以你会想要做一些不同的事情。
  • 该代码最初是基于从mySQL数据库中读取数据。我只是将其修改为读取文本文件而不是 mySQL。你能帮我解释一下逻辑吗?我想不通:/
  • 如果您了解for(a; b; c) 中每个部分的作用,答案应该会更加明显。 b 部分应确保 $page 永远不会超过您想要的最大值。如果您尝试理解这一点并用您尝试过的内容更新您的问题,我会在这里提供帮助和回复。向我们展示没有的工作。到目前为止,我还没有看到任何尝试。

标签: php html pagination txt


【解决方案1】:

我看到您正在使用数据库。如果您使用 CMS,这会更容易,但无论如何请检查您是否能理解此脚本:

try {

    // Find out how many items are in the table
    $total = $dbh->query('
        SELECT
            COUNT(*)
        FROM
            table
    ')->fetchColumn();

    // How many items to list per page
    $limit = 20;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

    // Display the paging information
    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

    // Prepare the paged query
    $stmt = $dbh->prepare('
        SELECT
            *
        FROM
            table
        ORDER BY
            name
        LIMIT
            :limit
        OFFSET
            :offset
    ');

    // Bind the query params
    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();

    // Do we have any results?
    if ($stmt->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);

        // Display the results
        foreach ($iterator as $row) {
            echo '<p>', $row['name'], '</p>';
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }

} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}

到处都有注释,所以应该很容易理解。

图片来源:Nev Stokes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2017-09-09
    • 2021-01-24
    • 2020-02-17
    • 2014-09-20
    • 2015-11-24
    相关资源
    最近更新 更多