duanhe1965
duanhe1965
2013-12-24 04:42

如何在搜索引擎中插入分页?

已采纳

How can I insert pagination in my search engine search.php page?

If there is 1 page, I do not want the Previous button and the Next button.

If there are 2 pages and I am on the first page, I hope that I can have the Next button.

If there are 2 pages and I am on the second page, I also hope that I can only have the Previous button.

This is my current code, hope that you can help me:

<?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'];
$page_number = $_GET['page'];

if (!$page_number);
$page_number = 0;

$results_per_page = 10;

$next = $page_number + $results_per_page;
$prev = $page_number - $results_per_page;

?>
<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: fixed;
                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" name="submit" style="height: auto; width: 60px; padding: 2px" />

                        <div id="suggestion" style="width: 548px">
                        </div>
                    </td>
                </tr>
            </table>
            <br>
            <hr>
            <table>
                <tr>
                    <td>
                        <?php
                        //SQL query
                        $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);
                        $x++;
                        if($x==1)
                        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
                            $index = 1;
                            while (($row = \mysqli_fetch_assoc($result)) && ($index < 10)) {//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 />";
                                $index++;
                            }
                        } elseif ($number_of_result = 1) {
                            echo "<b>$number_of_result result found!</b>";
                            echo "<p>";
                            //results found here and display them
                            $index = 1;
                            while (($row = \mysqli_fetch_assoc($result)) && ($index < 10)) {//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 />";
                                $index++;
                            }
                        }
                        ?>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="hidden" name="page" value="<?php echo 1; ?>" />
                        <div id="page-number">
                            Select Page Number:
                            <?php
                            //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
                            $max_page_number = $number_of_result / 10;
                            //echo $max_page_number;
                            echo "<ul>";
                            //both the condition are not the neccesary
                            if(!$page_number <= 0)
                            {
                                //print the link to previous page
                                echo "<li id='search-page-number'>";
                                echo "<a href=search.php?q=$query&page=$prev" . ">Previous</a>";
                                echo "</li>";
                            }
                            $i = 1;
                            for($index = 0; $index < $number_of_result; $index=$index+$results_per_page)
                            {
                                echo "<li id='search-page-number'>";
                                echo "<a href=search.php?q=$query&page=$i>";
                                echo $i++ . "</a>";
                                echo "</li>";
                            }
                            if($page_number < $number_of_result - $results_per_page)
                            {
                                //print the link to next page
                                echo "<li id='search-page-number'>";
                                echo "<a href=search.php?q=$query&page=$next" . ">Next</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>

Thanks in advance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dpgkg42484 dpgkg42484 8年前
    <?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'];
    $page_number = (int)$_GET['page'];
    
    if (!$page_number) 
    $page_number = 0;
    
    $results_per_page = 10;
    
    $next = $page_number + $results_per_page;
    $prev = $page_number - $results_per_page;
    
    ?>
    <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: fixed;
                    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" name="submit" 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
                            $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);
                            $x++;
                            if($x==1)
                            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
                                $index = 1;
                                while (($row = \mysqli_fetch_assoc($result)) && ($index < 10)) {//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 />";
                                    $index++;
                                }
                            } elseif ($number_of_result == 1) {
                                echo "<b>$number_of_result result found!</b>";
                                echo "<p>";
                                //results found here and display them
                                $index = 1;
                                while (($row = \mysqli_fetch_assoc($result)) && ($index < 10)) {//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 />";
                                    $index++;
                                }
                            }
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="hidden" name="page" value="0" />
    <div id="page-number">
                                Select Page Number:
                                <?php
    
                                //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
    
        $max_page_number = ceil($number_of_result / $results_per_page);
                                //echo $max_page_number;
                                echo "<ul>";
                                //both the condition are not the neccesary
    if ($max_page_number > 2) { // if more than 2 pages 
    
      if ($page_number > 0 ) { //Previous
        echo "<li id='search-page-number'>";
        echo "<a href=search.php?q=$query&page=".($page_number -  $results_per_page).">Previous</a>";
        echo "</li>";
      }
    
      for($index = 0 ; $index < $max_page_number ; $index++)
        {
    
    
    
    
          echo "<li id='search-page-number'>";
          echo "<a href=search.php?q=$query&page=".($index * $results_per_page).">";
          echo ($index + 1) . "</a>";
          echo "</li>";
    
        }
    
      if (($page_number + $results_per_page) < $number_of_result ) { //Next
        echo "<li id='search-page-number'>";
        echo "<a href=search.php?q=$query&page=".($page_number +  $results_per_page).">Next</a>";
        echo "</li>";
      }
    
    } elseif (($max_page_number == 2 ) ) {
    
      echo "<li id='search-page-number'>";
      echo "<a href=search.php?q=$query&page=".($page_number == 0 ? 10 : 0).">".($page_number == 0 ? "Next":"Previous" )."</a>";
      echo "</li>";
    } elseif (($max_page_number == 1 ) ) {
    
      echo "<li id='search-page-number'>";
      echo "<a href=search.php?q=$query&page=0>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>
    
    点赞 评论 复制链接分享

相关推荐