Php搜索和分页无法正常工作

时间:2018-04-15 13:31:34

标签: php pagination

我有一个搜索表单,搜索mysql数据库并返回结果。我还有一个searchmodel.php文件,它应该对结果进行分页。但是,分页不会返回第2页和其他页面的结果。搜索表单如下所示:

<?php

?>
<html>
<form action = "searchmodel.php" method = "GET">
            <select name = "choice">
                <!--<option value = "<?php echo $_GET['title']; ?>" name = "title">title</option>-->
                <option value = "title" name = "title">title</option>
                <option value = "author" name = "author"> author</option>
                <option value = "scc" name = "scc"> SCC_NO</option>
            </select>
                <input name= "term" type ="text" size="65" maxlength = "88" style = "display:inline">
                <input name = "mysearch" type="submit"  style = "display:inline">

        </form>

</html>

searchmodel.php文件如下所示:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
error_reporting(E_ERROR | E_PARSE);
include "data.php";
$con = dbConnect();
$perpage = 20;
echo ($pagenumber = isset($_GET['page']) ? $_GET['page'] : 1) . '<br />';

$offset = ($pagenumber-1) * $perpage;
$term = isset($_GET['term']) ? $_GET['term'] : null;
$choice = isset($_GET['choice']) ? $_GET['choice'] : '';
            if(empty($term) && ($choice == 'title' || $choice == 'author')){
                header ('Location: ' . 'search.php');   
                }

                if($choice == 'title') {
            $sth = $con->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM manuscript WHERE title 
            LIKE '%$term%' LIMIT {$offset},{$perpage}");
            $sth->execute();
            //$results = $sth->rowCount();
            //echo '<b>' . $results . '</b>'. '<br />';
            $results = $sth->fetchAll();
            $x=1;


            foreach($results as $key => $value){
                echo $x++ . ':' .$value['title'] . '<br />';


            }   
        }

            $total = $con->query("SELECT FOUND_ROWS() as total")->fetch()['total'];
            var_dump($total);
            $pages = ceil($total/$perpage);
            echo $pages . '<br />';



        for ($x=1; $x<=$pages; $x++){
        //echo $x;

        echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
    //echo "<a href='?pagenumber=$x'>$x</a>";

    }

我在这个网站(stackoverflow)上搜索了一个类似于我的问题并找到了这两个问题。 This onethis one。但是,提供的答案似乎不适合我的情况。我的问题是,我可能做错了什么?是导致问题的$ _GET [&#39;选择&#39;]变量?提前感谢您

1 个答案:

答案 0 :(得分:0)

更改此行

echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";

<a href = "?choice=<?php echo $_GET['choice'].'&term='.$_GET['term'].'&mysearch=Submit+Query&page=' . $x; ?>"><?php echo $x;?></a>

问题在于查询链接。