Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和PDO搜索分页_Php_Search_Pdo_Pagination - Fatal编程技术网

使用PHP和PDO搜索分页

使用PHP和PDO搜索分页,php,search,pdo,pagination,Php,Search,Pdo,Pagination,我想对搜索条目的结果进行分页 我已经尝试了一些代码,分页数据,但它不工作的搜索 在第一页,没问题。它显示前20个结果,并且具有正确的分页和总结果。但每当我单击“下一步”或“前进”时,它都会显示“无法显示结果”,这意味着输入的条目丢失 请帮忙。谢谢大家! 这是我的密码: <?php require("config.inc.php");//databaseconnection $entry = $_POST['entry']; // Find out how many items ar

我想对搜索条目的结果进行分页

我已经尝试了一些代码,分页数据,但它不工作的搜索

在第一页,没问题。它显示前20个结果,并且具有正确的分页和总结果。但每当我单击“下一步”或“前进”时,它都会显示“无法显示结果”,这意味着输入的条目丢失

请帮忙。谢谢大家!

这是我的密码:

<?php
require("config.inc.php");//databaseconnection
$entry = $_POST['entry'];
    // Find out how many items are in the table
    $total = $dbconn->query('
        SELECT
            COUNT(*)
        FROM
            'mytable'
        WHERE
            'column' = $entry
    ')->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 = $dbconn->prepare('
        SELECT
            *
        FROM
            'mytable'
        WHERE
            'column' = $entry
        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 $row['ID'] . ", " . $row['name'] . "<br>";
        }

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

?>


当您点击链接时,
$entry
将为空,因为没有发布请求,因此
$entry=$\u POST['entry']为空(链接单击使用GET方法)。您需要将其更改为使用GET或使用
method=“post”
form进行分页。

您必须在单击另一个页面时将搜索文本添加到
$\u GET
参数,例如
index.php?page=2&search=something