Zeroone Zeroone - 2 months ago 5
PHP Question

How to release pagination in "GET" search?

I use this code For search in database:

<?php

if(isset($_GET['keywords'])) {

$keywords = $CONNECT->escape_string($_GET['keywords']);
if(!$keywords){
echo("<script>location.href = '/news';</script>");

//if javascript disabled
$url='/news';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';
}
$Query = mysqli_query($CONNECT, "SELECT `id`, `title`, `himg`, `readed` FROM `news` WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%'");
?>
<div class="found">founded<?php echo $Query->num_rows; ?> </div>
</div>
</div>
<div class="ncontent">
<div class="keyword">phrase: "<p><?php echo htmlspecialchars($keywords); ?></p>"</div>
<?php
if($Query->num_rows){
while ($r = $Query->fetch_object()){
?>
<div class="nline"><a href="/news/content/id/<?php echo $r->id; ?>"><div class="prewnews">
<div class="imgh1"><img src="<?php echo $r->himg; ?>"/></div>
<span><i class="fa fa-heart-o" aria-hidden="true"> <p>124</p></i><hr style="background:#cd4436; border:0; height:3px" /><i class="fa fa-eye" aria-hidden="true"> <p><?php echo $r->readed; ?></p></i> </span>
<h3><?php echo $r->title; ?></h3>
</div>
</a>
</div>
<?php
}
}else echo '<div class="notfound"><img src="/resource/img/notfound.png" alt=""></div>';
}else {echo("<script>location.href = '/news';</script>");
$url='/news';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';}
?>





Search form:




<form action="/search" method="GET">
<input class="search red" placeholder="search..." type="search" name="keywords" autocomplete="off">
</form>





So, after search i get link like this:
/search?keywords=test+search+phrase

As I think i can do like this:
/search?keywords=test+search+phrase&page2


or like this:
/page2/search?keywords=test+search+phrase





What you advise, which way pagination?

And can you provide links that will help to realize them?

Or anyone of you can help me here?

Answer

The following pagination solution will show at most 10 search results per page, and for navigating rest of the search results, use pagination links. The solution will show at most 5 links per page, kind of like this:

// user is on 1st page
1 2 3 4 5 ...
-

// user is on 7th page
... 5 6 7 8 9 ...
        -

// user is on 10th page(lets say, 10th page is the last page)
... 6 7 8 9 10
             -

So, to implement this pagination functionality, you need to make few changes in your code. Keep your HTML search form as it is, and change your PHP code in the following way,

  • First check if there's any ...&page=123 exists or not in the URL, and based on that, process your $_GET superglobal like this way,

    if(isset($_GET['page']) && !empty($_GET['page']) && is_numeric($_GET['page'])){
        $current_page = $_GET['page'];
    }else{
        $current_page = 1;
    }
    
  • Define rows per page and calculate offset based on that, like this:

    $per_page = 10;  // rows per page
    $offset = ($current_page - 1) * $per_page;  // offset
    
  • And fetch table results based on these $per_page and $offset values, like this:

    $Query = mysqli_query($CONNECT, "SELECT ...  WHERE `content` LIKE ... LIMIT ". $per_page . " OFFSET " . $offset);
    

Now to display pagination links below the search results, follow the below procedure:

  • Get total number of rows from the queried result set and calculate total number of page/pagination links to display, like this:

    // display pagination links
    $Query = mysqli_query($CONNECT, "SELECT ... FROM `news`  WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%'");
    if($Query->num_rows){
        $total_records = $Query->num_rows;
        $total_pages = ceil($total_records / $per_page);
    
        ...
    
  • Find the superset range of pages, like 1-10, or 1-20 etc. For example, if $total_pages = 20 then this superset range would be 1-20. The code for this step is this:

    // superset range of pages
    $superset_range = range(1, $total_pages);
    
  • Find the subset range of pages to display, like 1-5, or 3-7 etc. For example, if $total_pages = 20 then this subset range would be 1-5, or 3-7, or 6-10 etc., it can be any consecutive five pages between 1 and 20. Also, adjust this range whenever necessary. The code for this step is this:

    // subset range of pages to display
    $subset_range = range($current_page - 2, $current_page + 2);
    
    // adjust the range(if required)
    foreach($subset_range as $p){
        if($p < 1){
            array_shift($subset_range);
            if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){
                $subset_range[] = $subset_range[count($subset_range) - 1] + 1;
            }
        }elseif($p > $total_pages){
            array_pop($subset_range);
            if(in_array($subset_range[0] - 1, $superset_range)){
                array_unshift($subset_range, $subset_range[0] - 1);
            }
        }
    }
    
  • Finally, display the pagination links and dots accordingly. The code for this step is this:

    // display intermediate pagination links
    if($subset_range[0] > $superset_range[0]){
        echo "...&nbsp;";
    }
    foreach($subset_range as $p){
        if($p == $current_page){
            echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "' style='text-decoration: underline;'>$p</a>";
        }else{
            echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "'>$p</a>";
        }
    
    }
    if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
        echo "&nbsp;... ";
    }
    

Here's the complete code:

<?php

    if(isset($_GET['keywords'])) {

        $keywords = $CONNECT->escape_string($_GET['keywords']);
        if(!$keywords){
            echo("<script>location.href = '/news';</script>"); 

            //if javascript disabled
            $url='/news';
            echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';
        }
        if(isset($_GET['page']) && !empty($_GET['page']) && is_numeric($_GET['page'])){
            $current_page = $_GET['page'];
        }else{
            $current_page = 1;
        }
        $per_page = 10;
        $offset = ($current_page - 1) * $per_page;
        $Query = mysqli_query($CONNECT, "SELECT `id`, `title`, `himg`, `readed` FROM `news`  WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%' LIMIT ". $per_page . " OFFSET " . $offset);
        ?>         
                <div class="found">founded<?php echo $Query->num_rows; ?> </div>
                </div>
            </div>
        <div class="ncontent"> 
        <div class="keyword">phrase: "<p><?php echo htmlspecialchars($keywords); ?></p>"</div>
        <?php
        if($Query->num_rows){
            while ($r =  $Query->fetch_object()){
        ?>
            <div class="nline">
                <a href="/news/content/id/<?php echo $r->id; ?>">
                    <div class="prewnews">
                        <div class="imgh1">
                            <img src="<?php echo $r->himg; ?>"/>
                        </div>
                        <span>
                            <i class="fa fa-heart-o" aria-hidden="true"> 
                                <p>124</p>
                            </i>
                            <hr style="background:#cd4436; border:0; height:3px" />
                            <i class="fa fa-eye" aria-hidden="true"> 
                                <p><?php echo $r->readed; ?></p>
                            </i> 
                        </span>
                        <h3><?php echo $r->title; ?></h3>
                    </div>
                </a>
            </div>   
        <?php   
            }

            // display pagination links
            $Query = mysqli_query($CONNECT, "SELECT `id`, `title`, `himg`, `readed` FROM `news`  WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%'");
            if($Query->num_rows){
                $total_records = $Query->num_rows;
                $total_pages = ceil($total_records / $per_page);

                if($total_records > $per_page){
                    echo "<center>";

                    // Superset range of pages
                    $superset_range = range(1, $total_pages);

                    // subset range of pages to display
                    $subset_range = range($current_page - 2, $current_page + 2);

                    // adjust the range(if required)
                    foreach($subset_range as $p){
                        if($p < 1){
                            array_shift($subset_range);
                            if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){
                                $subset_range[] = $subset_range[count($subset_range) - 1] + 1;
                            }
                        }elseif($p > $total_pages){
                            array_pop($subset_range);
                            if(in_array($subset_range[0] - 1, $superset_range)){
                                array_unshift($subset_range, $subset_range[0] - 1);
                            }
                        }
                    }

                    // display intermediate pagination links
                    if($subset_range[0] > $superset_range[0]){
                        echo "...&nbsp;";
                    }
                    foreach($subset_range as $p){
                        if($p == $current_page){
                            echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "' style='text-decoration: underline;'>$p</a>";
                        }else{
                            echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "'>$p</a>";
                        }

                    }
                    if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
                        echo "&nbsp;... ";
                    }

                    echo "</center> ";  
                }    
            }

        }else{
            echo '<div class="notfound"><img src="/resource/img/notfound.png" alt=""></div>';
        }
    }else {
        echo("<script>location.href = '/news';</script>"); 
        $url='/news';
        echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';
    }
?>
Comments