简单的PHP分页脚本不起作用

时间:2017-07-10 12:13:28

标签: php search pagination

我有一个php搜索脚本,可以搜索并回显数据库中的结果。问题是我正在尝试对搜索结果进行分页,但是当我点击其他页面时,它会显示相同的结果。也就是说,搜索结果不会在页面之间共享。请问我该如何解决?

以下是我的代码

if (isset($_GET["mainSearch"])) 
{
  $condition = '';
  $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
  $keyword = $_GET['mainSearch'];
  $query = explode(" ", $keyword);
  $perpageview = 10;
  $page = $_GET["pageno"];
  $frompage = $page*$perpageview+1-$perpageview;

  foreach ($query as $text) 
  {
      $condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
  }
  $condition = substr($condition, 0, -4);


  $order = " ORDER BY quiz_id DESC ";
  $sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order;
  $sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
  $result = $db->query($sql_query);
  $resultCount = $db->querySingle($sql_query_count);
  $pagecount = ceil($resultCount/$perpageview);

  if ($resultCount > 0)
  {
  if ($result)
  {
      while ($row = $result->fetchArray(SQLITE3_ASSOC))
      {

          $wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);

           echo '<div class="quesbox_3">
            <div class="questitle">
                <h2>'.$row["question"].'</h2>
            </div>
            <div class="quesanswer">'.$wording.'</div>
        </div>';
      }
      for ($i=1; $i <= $pagecount; $i++)
      {
          echo '<a href="searchresult.php?mainSearch='.$mainSearch.'&pageno='.$i.'">'.$i.'</a>';
      }
  }
  }
  else
  {
      echo "No results found";
  }
} 

非常感谢。

1 个答案:

答案 0 :(得分:0)

我在您之前的请求检查中提到了

 if (isset($_GET["mainSearch"])) 
    {
      $condition = '';
      // $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
      $keyword = $_GET['mainSearch'];
      $query = explode(" ", $keyword);
      $perpageview=10;
      if($_GET["pageno"]){
           $page=$_GET["pageno"];
     }else{
      $page=1;
     }
  $frompage = $page*$perpageview-$perpageview;
      foreach ($query as $text) 
      {
          $condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR ";
      }
      $condition = substr($condition, 0, -4);


      $order = " ORDER BY quiz_id DESC ";
      $sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order.' LIMIT '.$frompage.','.$perpageview;
      $sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
      $result = $db->query($sql_query);
      $resultCount = $db->querySingle($sql_query_count);
      $pagecount = ceil($resultCount/$perpageview);
      if ($resultCount > 0)
      {
      if ($result)
      {
          while ($row = $result->fetchArray(SQLITE3_ASSOC))
          {

              $wording = str_replace($text, "<span style='font-weight: bold;'>".$text."</span>", $row['answer']);

               echo '<div class="quesbox_3">
                <div class="questitle">
                    <h2>'.$row["question"].'</h2>
                </div>
                <div class="quesanswer">'.$wording.'</div>
            </div>';
          }
          for ($i=1; $i <= $pagecount; $i++) { 
             echo '<a href="url?mainSearch='.$mainSearch.'&pageno='.$i.'">'.$i.'</a>';
          }
      }
      }
      else
      {
          echo "No results found";
      }
    }