0

I want to make a pagination for search results. I can see the results in first page when I use the search box but I can't see the other results in other pages. What am I doing wrong?

I would be really glad if you could help me

Here' my search page codes.

Codes for pagination

<?php
    
                if(isset($_GET["page"])) {
                    $page = $_GET["page"];
                }else {
                    
                    $page = "";
                }

            if($page == "" || $page == 1) {
                
                $starter_post = 0;
            } else {
                
                $starter_post = ($page * 6) - 6;
            }
    
                    $sql_query2 = "SELECT * FROM posts ";
                    $look_all_post = mysqli_query($conn, $sql_query2);
                    $all_post_count = mysqli_num_rows($look_all_post);
                    $page_number = ceil ($all_post_count / 6);

Codes for search results

 
    if(isset($_POST["searchbtn"])) {
        
        $search = $_POST["search"];
        
        
        $query = "SELECT * FROM posts WHERE post_tags LIKE '%$search%' ORDER BY post_id DESC LIMIT $starter_post, 6";
        $search_query = mysqli_query($conn, $query);
        
        if(!$search_query) {
            die("QUERY FAILED:". mysqli_error($conn));
        }
        
        $search_count = mysqli_num_rows($search_query);
        
        if($search_count == 0) {
            echo "<h3> No Result </h3>";
        } else {
            
                 
                            
                            while ($row = mysqli_fetch_assoc($search_query)){
                                $post_id = $row["post_id"];
                                $post_date = $row["post_date"];
                                $date = strtotime($post_date);
                                $newdate = date("d/m/Y", $date);
                                $post_title = $row["post_title"];
                                $post_text = $row["post_text"];
                                $post_image = $row["post_image"];
                                
                                       
                        ?>
                                
              <div class="col-md-6">
                  <div class="blog">
                     <div class="blog-img ">
                        <img src="images/<?php echo $post_image; ?>" class="img-fluid" >
                        </div>
                            <div class="blog-content">
                                <ul class="blog-meta">
                                   <li><i class="far fa-calendar-alt"></i><span class="writer"><?php 
                                   echo $newdate; ?></span></li>
                                </ul>
                                    <h3><?php echo $post_title; ?></h3>
                                    <p><?php echo $post_text; ?></p>
                                    <div class="blog-content2 text-center">
                                    </div>
                                </div>   
                            </div>
                        </div>
                                
                                
                          <?php } 
                                     
            
            
        }
        
    }
                        ?>
      

Codes for pagination

        
            </div>
                    
   <div class="row">
 <nav aria-label="Page navigation example">
  <ul class="pagination justify-content-center">
 <li <?php if ($page == 1 or $page == "") {echo "class='page-item disabled'";} ?>>
   <a class="page-link" href="search.php?page=<?php if ($page > 1) {echo $page - 1;} ?>">Previous</a>
 </li>
  <?php //Pagination Continue
   for($i=1; $i<=$page_number; $i++) {
    echo "<li class='page-item'><a class='page-link' href='search.php?page=$i'>{$i}</a></li>";
   }    
  ?>
 <li <?php if ($page == $page_number) {echo "class='page-item disabled'";} ?>>
   <a class="page-link" href="search.php?page=<?php if ($page == "") {echo $page = 2;} else if($page_number!=$page){echo $page+1;} else if($page_number==$page){echo $page;}?>">Next</a>
 </li>
  </ul>
</nav>
</div> 

  • 1
    **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 09 '20 at 20:41
  • Can you show an example on my codes? – laylaylom Sep 09 '20 at 21:15
  • You can see here how to do it properly: https://stackoverflow.com/questions/28385145/correct-way-to-use-like-var-with-prepared-statements-mysqli – Dharman Sep 09 '20 at 21:19

0 Answers0