我有来自数据库的数据行,我想要一个具有简单分页的表,最简单的方法是什么?
如果有人能提供的话,我会很高兴的.

推荐答案

这是HTML和代码的混合,但我认为它非常基本,易于理解,解耦应该相当简单,以满足您的需要.

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

Php相关问答推荐

为什么这个方法调用用引号和花括号包裹?

PHP:从数组中获取唯一的数字组合

将产品类别添加到WooCommerce产品附加信息

在特定订单状态更改时自定义WooCommerce订单发货项目

如何修复警告:当使用WordPress get_the_Terms()时,Foreach()参数必须是ARRAY|OBJECT类型?

Sylius php -如何将购物车传递到小枝模板(Sylius模板事件)

添加并显示单价字段到WooCommerce产品变化

使用MySQLi使用连接字符串设置字符集

从字符串转换日期和/或时间时,Laravel转换失败

如何用Ubuntu 22.04在VSCode中启用XDebug

在WooCommerce管理中为订单项目添加自定义字段价格值

打折时更改 Woocommerce 产品名称

使用帖子 ID 更新 wp 帖子

如何正确判断 PHP 是否已正确配置为使用 DOMDocument?

根据所选付款方式启用/禁用 WooCommerce 所需的 checkout 字段

WooCommerce - 调用功能ID不正确.不应直接访问订单属性

PDO 和 SQL 查询结果中日期格式不同的原因可能是什么?

Laravel 测试 assertJsonMissing 不适用于唯一的键.为什么?

为什么 ECDSA 384 签名验证在 GO 中失败,但在 PHP 中却没有?

路由未定义的 laravel 导出