在搜索结果中添加PHP分页 [英] Adding PHP pagination to search results

查看:85
本文介绍了在搜索结果中添加PHP分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将PHP分页添加到来自搜索查询的结果中.我已经在此处

How do i add PHP pagination to results coming from search query. I have tried here andhere but didn't seem to get how these works or should work with my code. I have written the search query before i thought of pagination cause i was using the load on scroll before. How do i make the results show paginated.

下面是我显示 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来在滚动条上加载更多结果

I was using this along side javascript to load more result on scroll

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 中添加分页?

谢谢.

推荐答案

不使用jquery尝试此代码

try this code without using 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";
      }
    }

?>

这篇关于在搜索结果中添加PHP分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆