将PHP分页添加到搜索结果中

时间:2017-07-10 05:36:03

标签: php jquery mysql pagination

如何将PHP分页添加到来自搜索查询的结果中。我尝试了herehere,但似乎无法了解这些是如何工作的,或者应该使用我的代码。在我想到分页之前,我已经编写了搜索查询,因为之前我正在使用滚动加载。如何使结果显示分页。

以下是我显示 searchresults.php 的代码。

if (isset($_GET["mainSearch"])) 
{
  $condition = '';
  // $mainSearch = SQLite3::escapeString($_GET['mainSearch']);
  $keyword = $_GET['mainSearch'];
  $query = explode(" ", $keyword);

  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);

  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>';
      }
  }
  }
  else
  {
      echo "No results found";
  }
}

我在旁边的javascript中使用它来在滚动中加载更多结果

if (isset($_POST['limit']) && isset($_POST['start'])) {

$start = $_POST["start"];
$limit = $_POST["limit"];

$query =<<<EOF
SELECT * FROM questions ORDER BY quiz_id DESC LIMIT '$start', '$limit';
EOF;

// echo $query;

$result = $db->query($query);

while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo '<div class="quesbox_2">
  <div class="questitle">
      <h2>'.$row["question"].'</h2>
  </div>
  <div class="quesanswer">'.$row["answer"].'</div>
    <div class="quesdatetime"><img src="images/questime.png" alt="export question">'.$row["date"].'</div>
</div>';
}

}

如何为 searchresults.php 添加分页?

感谢。

1 个答案:

答案 0 :(得分:2)

不使用jquery

尝试此代码
<?php 


 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";
      }
    }

&GT;