get方法

tzdcorbm  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(194)

由于某些原因,分页不能与我的search get一起工作,尽管它在不使用get方法的页面中工作得很好。如果我搜索一个词,比如example,我在数据库中有11个标题,标题中有example 10这个词,前10个应该出现,然后在第二页我应该有第11个。但是,当我试图转到第二页时,从第一页得到的搜索结果丢失了,我得到了?pn=$i

<form class="sea" action="search.php" method="GET">
    <input class="searchbar" name="search" type="text" placeholder="Search...">
    <button class="searchbutton" name="submit-search" type="submit"><i class="fa fa-search"></i></button>
</form>

<?php
if(isset($_GET['submit-search'])) {
    $search = mysqli_real_escape_string($conn, $_GET['search']);
    $sql = "SELECT COUNT(*) FROM video_games WHERE video_games_title LIKE '%$search%' OR video_games_genre LIKE '$search'";
    $query = mysqli_query($conn, $sql);
    $row = mysqli_fetch_row($query);
    $rows = $row[0];
    $page_rows = 10;
    $last = ceil($rows/$page_rows);
    if($last < 1){
        $last = 1;
    }
    $pagenum = 1;
    if(isset($_GET['pn'])){
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
     }
    if ($pagenum < 1) {
        $pagenum = 1;
    }else if ($pagenum > $last) {
       $pagenum = $last;
   }
   $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
   $sql = "SELECT * FROM video_games WHERE video_games_title LIKE '%$search%' OR video_games_genre LIKE '$search' ORDER BY video_games_id $limit ";
   $query = mysqli_query($conn, $sql);
   $queryResult = mysqli_num_rows($query);
   $paginationCtrls = '';
   if($last != 1){
       if ($pagenum > 1) {
           $previous = $pagenum - 1;
           $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'"><i class="fas fa-angle-left"></i></a> &nbsp; &nbsp; ';
           for($i = $pagenum-4; $i < $pagenum; $i++){
               if($i > 0){
                   $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
               }
           }
       }
       $paginationCtrls .= ''.$pagenum.' &nbsp; ';
       for($i = $pagenum+1; $i <= $last; $i++){
           $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
           if($i >= $pagenum+4){
               break;
           }
       } 
       if ($pagenum != $last) {
           $next = $pagenum + 1;
           $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'"><i class="fas fa-angle-right"></i> ';
       }
   }
   if ($queryResult > 0) {
       while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
           $id = $row["video_games_id"];
           $title = $row["video_games_title"];
           $genre = $row["video_games_genre"];
           $image = $row["video_games_image"];
           $description= $row["video_games_description"];
           $platform= $row["video_games_platform"];
           $price= $row["video_games_price"];
?>
//Putting the data from the database to the html
<?php }//end of while ?>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>

如果我做些替换的话

$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?search='.$search.'&submit-search='.'?pn='.$i.'"">'.$i.'</a> &nbsp; ';

当我按第二页时,我得到第一页的结果,$paginationctrls也会说我在第一页eventhough in the url says search=example&submit search=?pn=2为什么它不起作用我把语法弄乱了?我能做些什么来修复它?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题