将分页添加到我的搜索引擎



如何向搜索引擎结果页面添加分页?

我已经建立了一个搜索引擎,但每次搜索都有数十万个结果,所以我想向它添加页面。

搜索引擎的结果输出在表格中。 我最近开始学习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>';
}

?>

我做错了什么,我可以改进什么来使其发挥作用? 非常感谢您的帮助!

从头开始构建分页不是一个好主意,而是使用这样的库: https://github.com/KnpLabs/knp-components/blob/master/doc/pager/intro.md

最新更新