1

我正在尝试构建一个基本搜索,但在这里遇到了一个小代码问题,我想对我的搜索结果集进行分页,这就是我所拥有的 1) 一个名为“input”的输入字段,a 并且在 post 方法 2 的形式中) 当用户提交表单时,它会使用下面的代码调用 search.php。

问题

分页返回正确分页的结果集,但是当我移至下一页时,将返回搜索表中的所有记录,包括那些不符合 where 子句中条件的记录。

这是代码:让我知道这里的代码是否太多。我可以粘贴到其他地方并提供链接;多谢你们

<?php

    $input = $_POST['input'];
    $categories = $_POST['category'];
    $state = $_POST['state'];

    $targetpage = "search.php";
    $limit = 3;

    //This query checks for data
    $qq = " SELECT * FROM classified where confirm='0' ";
    $qq = $db->prepare($qq);
    $qq->execute();

    $total_pages =$qq->rowCount();

    $stages = 3;
    $page = ($_GET['page']);
    if ($page){
        $start = ($page - 1) * $limit;
    } else {
        $start = 0;
    }

    $rows = $qq->fetchAll();
    if ($rows > 0){
        $q = " SELECT * FROM classified where confirm='0' ";
        if (!empty( $input)) {
            $q .= "AND title LIKE '%".$input."%' ";
        }
        if (!empty($_POST['search_category']) ) {
            $q .= "AND id_cat = ".$categories." ";
        }

        if (!empty($_POST['state']) ) {
            $q .= "AND id_state = ".$state." ";
        }
        $q .= "ORDER BY date DESC LIMIT $start, $limit ";
    }
    $q = $db->prepare($q);
    $q->execute();
    //// Just for testing purposes to see what's coming out
    print_r($q);

    /*** echo number of columns ***/
    $resultt = $q->fetchAll();
    // Initial page num setup
    if ($page == 0){
        $page = 1;
    }
    $prev = $page - 1;
    $next = $page + 1;

    $lastpage = ceil($total_pages/$limit);
    $LastPagem1 = $lastpage - 1;

    $paginate = '';
    if ($lastpage > 1) {
        $paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
        } else {
            $paginate.= "<span class='disabled'>Previous</span>";
        }
        // Pages
        if ($lastpage < 7 + ($stages * 2)) { // Not enough pages to breaking it up
            for ($counter = 1; $counter <= $lastpage; $counter++) {
                if ($counter == $page) {
                $paginate.= "<span class='current'>$counter</span>";
                } else {
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                }
            }
        } else if ($lastpage > 5 + ($stages * 2)) {// Enough pages to hide a few?
            // Beginning only hide later pages
            if ($page < 1 + ($stages * 2)) {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) {
                    if ($counter == $page) {
                        $paginate.= "<span class='current'>$counter</span>";
                    } else {
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                    }
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
            } else if ($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) { // Middle hide some front and some back
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) {
                    if ($counter == $page) {
                        $paginate.= "<span class='current'>$counter</span>";
                    } else {
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                    }
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
            } else { // End only hide early pages
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) {
                    if ($counter == $page) {
                        $paginate.= "<span class='current'>$counter</span>";
                    } else {
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                    }
                }
            }
        }
        // Next
        if ($page < $counter - 1) {
            $paginate.= "<a href='$targetpage?page=$next'>Next</a>";
        } else {
            $paginate.= "<span class='disabled'>Next</span>";
        }
        $paginate.= "</div>";
    }
    // pagination
    echo $total_pages.' Results';
    echo $paginate;
    ////////
    if (count($resultt) !== 0) {
        foreach ($resultt as $row) {
            echo $row['title'];
            echo $row['categories'];
            echo $row['state'];
        }
    } else {
        echo "No data available";
    }
4

1 回答 1

1

这当然是因为当您转到下一页时,您不会再次发送这些值:

$input = $_POST['input'];
$categories = $_POST['category'];
$state = $_POST['state'];

对应于您的过滤器值。他们肯定会变得空虚。

希望这可以帮助。

于 2013-01-02T03:31:48.967 回答