Search code examples
phppaginationsearch-engine

Insert pagination features in my search engine for navigation?


How can I:

  1. Insert page number 1, 2 & ... and the Next button if I have 2 or more pages of results and if I am on every page except the last page.

  2. Insert page number 1, 2 & ... and the Previous button if I have 2 or more pages and if I am on every page except the first page.

  3. Insert the First button on every page except the first page if I have 2 or more pages.

  4. Insert the Last button on every page except the last page if I have 2 or more pages.

I really a newbie so I need your guidance and help.

This is my current code:

The PHP Code:

    if (!$page_number) 
    $page_number = 0;

    $results_per_page = 4;

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

?>

The HTML Code:

<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
                        $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 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>

Thanks in advance.


Solution

  • This code can help you:

    You have to define the number of elements per page, the number of links and set the current page.

    <?php
    class page {
    
    public $element_per_page = 10,
        $nb_links = 2,
        $nb_page = 0,
        $page = 1;
    
    public function display_page() {
        $data = array(); // with many elements
        $this->nb_element = count($data);
        $this->nb_page = ceil($this->nb_element / $this->element_per_page);
    
        if (!empty($_GET['p']) && $_GET['p'] > 0 && $_GET['p'] <= $this->nb_page) {
            $this->page = $_GET['p'];
        }
    
        $this->getPagination();
      }
    
      public function getPagination() {                                             
    
        $pages = array();                                                         
    
        if ($this->page == $this->nb_links+2) {                                   
            $pages[]['value'] = 1;                                                
        } else if($this->page > $this->nb_links+2) {                              
            $pages[]['value'] = 1;                                                
            $pages[]['value'] = '…';                                              
        }                                                                         
    
        $start = $this->page-$this->nb_links;                                     
    
        if ($start < 1) {                                                         
            $start = 1;                                                           
        }                                                                         
    
        for ($i=$start; $i<=$this->page+$this->nb_links; $i++) {                  
            if ($i>0 && $i<=$this->nb_page) {                                     
                $pages[$i]['value'] = $i;                                         
                if ($this->page == $i) {                                          
                    $pages[$i]['selected'] = 'selected';                          
                }                                                                 
            } else {                                                              
                break;                                                            
            }                                                                     
        }                                                                         
    
        if ($this->page < $this->nb_page-$this->nb_links-1) {                     
            $pages[]['value'] = '…';                                              
        }                                                                         
    
        if ($this->page <= $this->nb_page-($this->nb_links+1)) {                  
            $pages[]['value'] = $this->nb_page;                                   
        }                                                                         
    
        return $pages;                                                            
    
      }
    
    }