获取两个变量的方法

时间:2018-04-12 16:08:15

标签: php html mysql sql

出于某种原因,虽然在不使用get方法的页面中它正常工作,但分页不适用于我的搜索。如果我搜索一个术语,举个例子,我在数据库中有11个标题,标题中的单词示例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也说我在第一页,尽管在url中说搜索= example&amp; submit-search =?pn = 2 为什么我不能使用语法somwhere? 我该怎么做才能解决它?

0 个答案:

没有答案