添加分页到我的搜索引擎

时间:2017-08-14 08:46:37

标签: php sql pagination search-engine

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

我已经建立了一个搜索引擎,但每次搜索都有成千上万的结果,所以我想为它添加页面。

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


?>

我做错了什么,我能做些什么才能让它发挥作用? 非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

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