2017-08-14 66 views
0

如何將分頁添加到搜索引擎結果頁面?將分頁添加到我的搜索引擎

我建立了一個搜索引擎,但每個搜索都有成千上萬的搜索結果,所以我想向它添加頁面。

搜索引擎的結果輸出在表中。 我最近開始學習php和sql ...

如何添加這些頁面?

到目前爲止,但沒有成功,我已經試過這樣:

<?php 
$con = mysqli_connect(xxxx); 
mysqli_select_db($con, 'Data') or die("could not find the database!"); 
$output = ''; 

$results_per_page = 1000; 

//positioning 

if(isset($_GET['search'])) 
    { 
    $starttime = microtime(true);  //TIME 
    $searchkey = $_GET['search']; 
    $query = mysqli_query($con, "SELECT * FROM table1 WHERE email LIKE '%$searchkey%'") or die("Could not search") ; 
    $count = mysqli_num_rows($query); 

    // count number of pages for the search 
    $number_of_pages = ceil($count/$results_per_page); 
    // determine which page number visitor is currently on 
    if (!isset($_GET['page'])) 
     { 
     $page = 1; 
     } 
    else 
     { 
     $page = $_GET['page']; 
     } 
    // LIMIT 
    $this_page_first_result = ($page-1)*$results_per_page; 




    if ($count == 0) 
     { 
     echo "</br>"; 
     $output = 'There are no search results !' ; 

     } 
    else 
     { 

     echo '<table class="myTable">'; 
     echo "<tr><th>aaa</th><th>bbb</th></tr>"; 
     $query = mysqli_query($con, "SELECT * FROM table1 WHERE email LIKE '%$searchkey%' LIMIT " . $this_page_first_result . ',' . $results_per_page" ") or die("Could not search") ; 

     while ($row = mysqli_fetch_array($query)) 
      { 
      $email = preg_replace('/(' . $searchkey . ')/i', '<mark>\1</mark>', $row["aaa"]); 
      $password = $row['bbb']; 
      echo "<tr><td>"; 
      echo $aaa; 
      echo "</td><td>"; 
      echo $bbb; 
      echo "</td></tr>"; 
      $output = '</table>'; 
      } 
     //echo "</table>"; 
     $endtime = microtime(true); 
     $duration = $endtime - $starttime; 
     echo "</br>"; 
     if ($count == 1) 
      { 
      echo "<div class=resinfo>"; 
      echo '<div>'."1 result was found for '$searchkey' in $duration seconds.".'</div>'.'</br>'; 
      echo "</div>"; 
      } 
     else 
      { 
      echo "<div class=resinfo>"; 
      echo '<div>'."$count results were found for '$searchkey' in $duration seconds.".'</div>'.'</br>'; 
      echo "</div>"; 
      } 
     } 
    echo $output; 
    } 
//LINKS to other pages 
for ($page = 1; $page<=$number_of_pages;$page++){ 
echo '<a href="search.php?search=' . $searchkey . ' &page=' . $page . ' ">' . $page . '</a>'; 
} 


?> 

我做了什麼錯,我可以改進,使其工作? 非常感謝您的幫助!

+0

你的標題好像是問關於分頁,但你的實際問題更像是「爲什麼這個代碼不加工。」什麼是真正的問題? –

+0

我想知道如何將頁面添加到我的搜索引擎,並且代碼是我迄今爲止嘗試做的,但沒有成功。我想知道是否有更好的添加頁面的方式,或者我選擇的路徑是好還是有一些錯誤。 感謝您的幫助! – alb

+0

你以爲你可以幫我@TimBiegeleisen? – alb

回答

相關問題