Click here to Skip to main content
15,999,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i created a search filter and it works fine and i also created a pagination too and it also works fine. the problem is if i want to implement the pagination in to the search filter it does'nt work when i clicki next button but rather shows empty results even when i click on prev button but when i search it displays data and when i click on next, it destroys data..

What I have tried:

<form method="POST"  action="search_christian_songs.php">
        <div class="form-group input-group">
           <input class="form-control mr-sm-2" required="" name="search" type="text" placeholder="Search" aria-label="Search">
          <span class="input-group-btn">
            <button class="button"  name="submit-submit" type="submit">

            Search
            </button>
          </span>        
        </div>
      </form>
</div> 
                  <div class="col-sm-8">
                     <div class="blog_left_sidebar">

                           <aside class="single_sidebar_widget popular_post_widget">
                                   
                                                   <?php
  if (isset($_GET['page_no']) && $_GET['page_no']!="") {
  $page_no = $_GET['page_no'];
  } else {
    $page_no = 1;
        }

  $total_records_per_page = 3;
    $offset = ($page_no-1) * $total_records_per_page;
  $previous_page = $page_no - 1;
  $next_page = $page_no + 1;
  $adjacents = "2"; 

  $result_count = mysqli_query($conn,"SELECT COUNT(*) As total_records FROM african_songs ");
  $total_records = mysqli_fetch_array($result_count);
  $total_records = $total_records['total_records'];
    $total_no_of_pages = ceil($total_records / $total_records_per_page);
  $second_last = $total_no_of_pages - 1; // total page minus 1
                                            if (isset($_POST['search'])) {
                              
                                        $search = mysqli_real_escape_string($conn, $_POST['search'] );
                                        $query =  ("SELECT * FROM christian_songs WHERE Title LIKE '%$search%'  LIMIT $offset, $total_records_per_page ");
              
                                       $result = mysqli_query($conn, $query);
                                       $queryResult = mysqli_num_rows($result);
                       
                                echo "<h2 class='widget_title'> There are ".$queryResult." results found! </h2>";
                                        if ($queryResult > 0) {
                                            while ($row = mysqli_fetch_assoc($result)) {
                                               $id = $row['id']; 
                                            echo " <div class='media post_item'> 
                             <img src=".$row['image']." alt='post'> <div class='media-body'><ul class='list cat-list'><li>
                                     <a href='download_christian_songs.php?id=$id'> <h5>".($row['Title'])."</h5></a>
                             <p> ".($row['date'])."</p></li>
      
                           </div></ul>
                                          

               </div>";
                                               } 

                                         }
                                      } else{
              echo "<div class='alert alert-warning'>You Have No Record!</div>";
      }

                                      ?>
   
                                                                                          <br>
<h3>Page <?php echo $page_no." of ".$total_no_of_pages; ?></h3>

  <div class="page_nav">
    <ul class="d-flex flex-row align-items-start justify-content-start">
   <?php // if($page_no > 1){ echo "<li><a href='?page_no=1'>First Page</a></li>"; } ?>
    
  <li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>>
  <a <?php if($page_no > 1){ echo "href='?page_no=$previous_page'"; } ?>>Prev</a>
  </li>
       
    <?php 
  if ($total_no_of_pages <= 3){     
    for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
      if ($counter == $page_no) {
       echo "<li class='active'><a>$counter</a></li>";  
        }else{
           echo "<li><a href='?page_no=$counter'>$counter</a></li>";
        }
        }
  }
  elseif($total_no_of_pages > 10){  
  if($page_no <= 4) {     
   for ($counter = 1; $counter < 8; $counter++){     
      if ($counter == $page_no) {
       echo "<li class='active'><a>$counter</a></li>";  
        }else{
           echo "<li><a href='?page_no=$counter'>$counter</a></li>";
        }
        }
    echo "<li><a>...</a></li>";
    echo "<li><a href='?page_no=$second_last'>$second_last</a></li>";
    echo "<li><a href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
    }

   elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {     
    echo "<li><a href='?page_no=1'>1</a></li>";
    echo "<li><a href='?page_no=2'>2</a></li>";
        echo "<li><a>...</a></li>";
        for ($counter = $page_no - $adjacents; $counter <= $page_no + $adjacents; $counter++) {     
           if ($counter == $page_no) {
       echo "<li class='active'><a>$counter</a></li>";  
        }else{
           echo "<li><a href='?page_no=$counter'>$counter</a></li>";
        }                  
       }
       echo "<li><a>...</a></li>";
     echo "<li><a href='?page_no=$second_last'>$second_last</a></li>";
     echo "<li><a href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";      
            }
    
    else {
        echo "<li class='active'><a href='?page_no=1'>1</a></li>";
    echo "<li><a href='?page_no=2'>2</a></li>";
        echo "<li><a>...</a></li>";

        for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
          if ($counter == $page_no) {
       echo "<li class='active'><a>$counter</a></li>";  
        }else{
           echo "<li><a href='?page_no=$counter'>$counter</a></li>";
        }                   
                }
            }
  }
?>
 
  <li <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
  <a <?php if($page_no < $total_no_of_pages) { echo "href='?page_no=$next_page'"; } ?>>Next</a>
  </li>
    <?php if($page_no < $total_no_of_pages){
    echo "<li><a href='?page_no=$total_no_of_pages'>Last</a></li>";
    } ?>
</ul> 
Posted
Updated 29-Apr-20 4:02am
v2
Comments
Richard Deeming 29-Apr-20 9:10am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]

1 solution

Your page number links will make a GET request to the server. They will not include the search term you previously entered.

You need to change the page number links to buttons, and have them submit the search form. If the button needs to be outside of the form, you can add the form attribute to tell it which form to submit. And with Bootstrap, you can add class="btn btn-link" if you want it to look like a link instead of a button.

You also need to repopulate the search input with the value that was submitted to the server, making sure to HTML-encode it to avoid cross-site scripting vulnerabilities.

And you'll want to filter the Count query as well as the query which selects the records.

Try something like this:
PHP
<?php
    if (isset($_POST['search'])) {
        $search = $_POST['search'];
    } else {
        $search = "";
    }
    
    if (isset($_GET['page_no'])) {
        $page_no = intval($_GET['page_no']);
    } else if (isset($_POST['page_no'])) {
        $page_no = intval($_POST['page_no']);
    }
    else {
        $page_no = 1;
    }
    
    if (empty($search)) {
        $total_records = 0;
    }
    else {
        $stmt = $mysqli_prepare("SELECT Count(*) FROM christian_songs WHERE Title LIKE Concat('%', ?, '%')");
        mysqli_stmt_bind_param($stmt, "s", $search);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $total_records);
        mysqli_stmt_fetch($stmt);
    }
?>

<form id="search_form" method="POST"  action="search_christian_songs.php">
    <div class="form-group input-group">
        <input class="form-control mr-sm-2" required="" name="search" type="text" placeholder="Search" aria-label="Search" value="<?php echo htmlspecialchars($search) ?>">
        <span class="input-group-btn">
            <button class="button"  name="submit-submit" type="submit">Search</button>
        </span>
    </div>
</form>

<?php 
if (empty($search)) {
    echo "<div class='text-muted'>Please enter your search term</div>";
} else if ($total_records == 0) {
    echo "<div class='alert alert-warning'>No matching records found</div>";
} else {
    $total_records_per_page = 3;
    $offset = ($page_no - 1) * $total_records_per_page;
    $total_no_of_pages = ceil($total_records / $total_records_per_page);
    
    echo "<h2 class='widget_title'> There are ".$total_records." results found! </h2>";
    
    $stmt = $mysqli_prepare("SELECT Id, Title, Image, Date FROM christian_songs WHERE Title LIKE Concat('%', ?, '%') LIMIT ?, ?");
    mysqli_stmt_bind_param($stmt, "sii", $search, $offset, $total_records_per_page);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $Id, $Title, $Image, $Date);
    
    while (mysqli_stmt_fetch($stmt)) {
        echo "<div class='media post_item'>
            <img src='$Image' alt='post'>
            <div class='media-body'>
                <ul class='list cat-list'>
                    <li>
                        <h5><a href='download_christian_songs.php?id=$Id'>$Title</a></h5>
                        <p>$Date</p>
                    </li>
                </ul>
            </div>
        </div>";
    }

    echo "<h3>Page $page_no of $total_no_of_pages</h3>";
    echo "<div class='page_nav'>";
    echo "<ul class='d-flex flex-row align-items-start justify-content-start'>";
    
    if ($page_no <= 1) {
        echo "<li class='disabled'>First</li>";
        echo "<li class='disabled'>Prev</li>";
    }
    else {
        $previous_page = $page_no - 1;
        echo "<li><button class='btn btn-link' form='search_form' type='submit' name='page_no' value='1'>First</button></li>";
        echo "<li><button class='btn btn-link' form='search_form' type='submit' name='page_no' value='$previous_page'>Prev</button></li>";
    }
    
    for ($counter = 1; $counter <= $total_no_of_pages; $counter++) {
        if ($counter == $page_no) {
            echo "<li class='active'>$counter</li>";
        }
        else {
        echo "<li><button class='btn btn-link' form='search_form' type='submit' name='page_no' value='$counter'>$counter</button></li>";
        }
    }
    
    if ($page_no >= $total_no_of_pages) {
        echo "<li class='disabled'>Next</li>";
        echo "<li class='disabled'>Last</li>";
    }
    else {
        $next_page = $page_no + 1;
        echo "<li><button class='btn btn-link' form='search_form' type='submit' name='page_no' value='$next_page'>Next</button></li>";
        echo "<li><button class='btn btn-link' form='search_form' type='submit' name='page_no' value='$total_no_of_pages'>Last</button></li>";
    }
    
    echo "</ul>";
    echo "</div>";
}
?>
(I've ignored the code for trimming the list of page numbers, but you should get the general idea.)

PHP: htmlspecialchars - Manual[^]
PHP: intval - Manual[^]
 
Share this answer
 
v5
Comments
peter sababa 29-Apr-20 10:41am    
Am getting an error sir, or can i send you the program to help me out....?
Parse error: syntax error, unexpected ')' in C:\xampp\htdocs\music\search_christian_songs.php on line 36
Richard Deeming 29-Apr-20 10:43am    
Which line is line 36?
peter sababa 29-Apr-20 10:48am    
if (isset($_GET['page_no'])) {
//this one // $page_no = intval($_GET['page_no']));
} else if (isset($_POST['page_no'])) {
$page_no = intval($_POST['page_no']));
}
Richard Deeming 29-Apr-20 10:49am    
Ah, sorry - there's an extra closing ) on those lines.
if (isset($_GET['page_no'])) {
    $page_no = intval($_GET['page_no']);
} else if (isset($_POST['page_no'])) {
    $page_no = intval($_POST['page_no']);
}
peter sababa 29-Apr-20 10:53am    
mysqli_stmt_bind_param($stmt, "s", $search)
// still get another error on this line//mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $total_records);
mysqli_stmt_fetch($stmt);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900