简体   繁体   English

向我的搜索引擎添加分页

[英]Add pagination to my search engine

How can I add pagination to a search engine results page? 如何向搜索引擎结果页面添加分页?

I have build a search engine but there are hundreds of thousands of results for every search so I want to add pages to it. 我已经建立了一个搜索引擎,但是每次搜索都有成千上万个结果,因此我想向其中添加页面。

The results of the search engine are outputted in a table. 搜索引擎的结果输出到表格中。 I have started to learn php and sql recently... 我最近开始学习php和sql ...

How can I add those pages? 如何添加这些页面?

I have tried this so far but with no success: 到目前为止,我已经尝试过但没有成功:

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


?>

What have I done wrong, what can I improve to make it work? 我做错了什么,我可以做些什么改进才能使它起作用? Thanks a lot for your help! 非常感谢你的帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM