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
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[
^]