1

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.

Below is my code to display 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";
  }
}

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

}

How do i add pagination to my searchresults.php?

Thanks.

Community
  • 1
  • 1
codebarz
  • 327
  • 1
  • 5
  • 28

1 Answers1

2

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

?>

sekaraja
  • 177
  • 1
  • 1
  • 8
  • I tried this but when i search all search results are shown on the same page along side the page numbers. No matter the page number i click, it shows the same result. I think the results are not sharing into different pages. Please how do i fix this. @sekaraja – codebarz Jul 10 '17 at 10:06
  • hi change this line small correction $frompage = $page*$perpageview-$perpageview; instead of this line $frompage = $page*$perpageview+1-$perpageview; because first data will be missing. I have changed in above code. Thanks – sekaraja Jul 10 '17 at 13:43
  • @sekeraja Thanks very much. You saved my day. – codebarz Jul 10 '17 at 13:49