搜索词的第一个结果没有出来

时间:2013-12-27 05:44:45

标签: php pagination search-engine

为什么我的搜索字词的第一个结果总是不会出现?

我注意到只有在我导航到页码0时才会显示。

我该如何解决这个问题?

我希望当我导航到页码1时,第一个结果也会出来!

PHP代码:

<?php
    //php code goes here
    include 'connect.php'; // for database connection
    include 'script_suggestion.php';
    include 'script_close_suggestion_box.php';
    $query = $_GET['q']; // query
    $button = $_GET ['submit'];

    if (isset($_GET['page'])) {
        $page_number = (int)$_GET['page'];
        $page_number = mysqli_real_escape_string($page_number);
    } else {
        $page_number = 1;
    }

    $results_per_page = 10;

?>

HTML代码:

<html>
    <head>
        <title>
            Brandon's Search Engine
        </title>
        <style type="text/css">
            #title a {
                font-size: 17pt;
                margin: 5px;
                padding: 2px;
                border-color: black;
                text-decoration: underline;
                width: 544px;
            }
            #search-result {
                display: block;
                border: 1px solid grey;
                border-color: grey;
            }
            #search-result:hover {
                background-color: #dddddd;
                width: 544px;
            }
            #link {
                font-size: 17pt;
                margin: 5px;
                padding: 2px;
                width: 544px;
            }
            #description {
                font-size: 17pt;
                margin: 5px;
                padding: 2px;
                width: 544px;
            }
            #search-page-number {
                display: block;
                width: auto;
                height: auto;
                border: 1px solid gray;
                margin: 2px;
                padding-left: 5px;
                padding-right: 5px;
                padding-bottom: 2px;
                padding-top: 2px;
                list-style: none;
                float: left;
                text-align: center;
            }
            #search-page-number:hover {
                background-color: #dddddd;
            }
            #suggestion {
                border: 1px solid black;
                visibility: hidden;
                position: absolute;
                background-color: white;
                z-index: 10;
            }
            #suggestion a {
                font-size: 12pt;
                color: black;
                text-decoration: none;
                display: block;
                width: 548px;
                height: auto;
                text-align: left;
                padding: 2px;
            }
            #suggestion a:hover {
                background-color: #dddddd;
                width: 544px;
                padding: 2px;
            }
        </style>
    </head>
    <body>
        <form method="GET" action="search.php">
            <table>
                <tr>
                    <td>
                        <h2>
                            Brandon's Search Engine
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="text" value="<?php echo htmlspecialchars($_GET['q']); ?>" name="q" style="height: 27px; width: 550px; padding: 2px" name="q"
                               onkeyup="getSuggestion(this.value)" autocomplete="off" onblur="closeBox()" placeholder="Search Now"/>
                        <input type="submit" value="Search" style="height: auto; width: 60px; padding: 2px" />

                        <div id="suggestion" style="width: 548px">
                        </div>
                    </td>
                </tr>
            </table>
            <br>
            <hr>
            <table>
                <tr>
                    <td>
                        <?php
                        //count 
                        $count_sql = "SELECT count(*) as c FROM searchengine WHERE title LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR keywords LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR link LIKE '%" . mysqli_real_escape_string($con,$query) . "%' ";
                        $search_count = mysqli_fetch_array(mysqli_query($con,$count_sql));
                        $number_of_result = $search_count['c'];
                        //SQL query
                        $page_number = (int)$_GET['page'];
                        $stmt = "SELECT * FROM searchengine WHERE title LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR keywords LIKE '%" . mysqli_real_escape_string($con,$query) . "%' OR link LIKE '%" . mysqli_real_escape_string($con,$query) . "%' LIMIT " . $page_number . " , $results_per_page";
                        $result = mysqli_query($con,$stmt) or die(mysqli_error($con));
                        //$number_of_result = mysqli_num_rows($result);
                        if ($number_of_result < 1) {
                            echo "<b>No results found!</b>";
                            echo "<p>";
                            echo "Your search - <b>$query</b>" . " - did not match any documents. Please try different keywords.";
                        }

                        elseif ($number_of_result > 1) {
                            echo "<b>$number_of_result results found!</b>";
                            echo "<p>";
                            //results found here and display them
                            while (($row = \mysqli_fetch_array($result))) {//10 results per page
                                $title = $row["title"];
                                $description = $row["description"];
                                $link = $row["link"];
                                echo "<div id='search-result'>";
                                echo "<div id='title'><a href='$link'>" . $title . "</a></div>";
                                //echo "<br />";
                                echo "<div id='link'><small>" . $link . "</small></div>";
                                //echo "<p>";
                                echo "<div id='description'><small>" . $description . "</small></div>";
                                echo "</div>";
                                echo "<br />";
                            }
                        }

                        elseif ($number_of_result == 1) {
                            echo "<b>$number_of_result result found!</b>";
                            echo "<p>";
                            //results found here and display them
                            while (($row = \mysqli_fetch_array($result))) {//10 results per page
                                $title = $row["title"];
                                $description = $row["description"];
                                $link = $row["link"];
                                echo "<div id='search-result'>";
                                echo "<div id='title'><a href='$link'>" . $title . "</a></div>";
                                //echo "<br />";
                                echo "<div id='link'><small>" . $link . "</small></div>";
                                echo "<br />";
                                echo "<div id='description'><small>" . $description . "</small></div>";
                                echo "</div>";
                                echo "<br />";
                            }
                        }
                        ?>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="hidden" name="page" value="<?php echo 1; ?>" />
                        <div id="page-number">
                            <?php

                            $max_page_number = ceil($number_of_result / $results_per_page);

                            if ($number_of_result == 0) {
                                echo " ";
                            }

                            elseif ($number_of_result != 0) {
                                echo "You are on page $page_number of $max_page_number.";
                            }

                            //ie if 35 results are therer then we require 4 pages that are 0 to max_page_number
                            //current page number is equal to page_number
                            //echo $max_page_number;
                            echo "<ul>";
                            //both the condition are not the neccesary
                            if ($max_page_number >= 1) { // if more than 2 pages

                            if ($page_number != 1) { //First
                                echo "<li id='search-page-number'>";
                                echo "<a href=search.php?q=$query&page=1>First</a>";
                                echo "</li>";
                            }

                            if ($page_number != 1) { //Previous
                                echo "<li id='search-page-number'>";
                                $prev = $page_number - 1;
                                echo "<a href=search.php?q=$query&page=".($prev).">Previous</a>";
                                echo "</li>";
                            }

                            for($index = 1 ; $index <= $max_page_number ; $index++) {
                                echo "<li id='search-page-number'>";
                                echo "<a href=search.php?q=$query&page=".($index).">";
                                echo ($index) . "</a>";
                                echo "</li>";
                            }

                            if ($page_number != $max_page_number) { //Next
                                echo "<li id='search-page-number'>";
                                $next = $page_number + 1;
                                echo "<a href=search.php?q=$query&page=".($next).">Next</a>";
                                echo "</li>";
                            }

                            if ($page_number != $max_page_number) { //Last
                                echo "<li id='search-page-number'>";
                                echo "<a href=search.php?q=$query&page=".($max_page_number).">Last</a>";
                                echo "</li>";
                            }

                            } elseif (($max_page_number == 1 ) ) {
                              echo "<li id='search-page-number'>";
                              echo "<a href=search.php?q=$query&page=1>1</a>";
                              echo "</li>";
                            }

                            echo "</ul>";

                            ?>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        To insert your site in result fill in the form at <a href="insert.php">here</a>.
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

您的查询以:

结尾
"LIMIT " . $page_number . ", $results_per_page"

但这不是LIMIT子句在SQL中的工作方式。它被定义为:

LIMIT offset, row_count

offset是一个行号,从0开始。所以如果你这样做:

LIMIT 1, 10

您将获得记录2到11,而不是1到10.正确的方法是:

"LIMIT " . ($page_number-1)*$results_per_page . ", $results_per_page"

您应该发现有用的其他内容是SQL_CALC_FOUND_ROWS语句的SELECT选项。这告诉MySQL计算符合条件的总行数,即使您使用LIMIT仅返回一个子集。这样您就不必进行两次查询,一次查询COUNT(*),另一次查询获取一页数据。使用此选项进行查询后,您可以使用SELECT FOUND_ROWS()获取完整计数。

$safequery = mysqli_real_escape_string($con,$query);
$stmt = "SELECT SQL_CALC_FOUND_ROWS *
         FROM searchengine
         WHERE title LIKE '%" . $safequery . "%' 
         OR keywords LIKE '%" . $safequery . "%' 
         OR link LIKE '%" . $safequery . "%'
         LIMIT " . ($page_number-1)*results_per_page . " , $results_per_page";
$result = mysqli_query($con,$stmt) or die(mysqli_error($con));
$count_sql = "SELECT FOUND_ROWS() AS c";
$search_count = mysqli_fetch_array(mysqli_query($con,$count_sql));
$number_of_result = $search_count['c'];