代码之家  ›  专栏  ›  技术社区  ›  Alex Maina

Php搜索和分页不工作

  •  1
  • Alex Maina  · 技术社区  · 6 年前

    我有一个搜索表单,可以搜索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 one this one .然而,提供的答案似乎不适用于我的情况。我的问题是,我可能做错了什么??是否是$\u GET['choice]变量导致了问题?提前感谢您

    1 回复  |  直到 6 年前
        1
  •  1
  •   tom sawyer    6 年前

    更改此行

    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>
    

    问题在于查询链接。