I have been stuck for hours on a project I’m working on in localhost and would appreciate any assistance. The application incorporates a search form which returns a tabulated result that is also paginated when the total number of records exceeds 3 rows, as was set in the application. My challenge is that the output on search result pages other than the first page start counting from S/N 1 again instead of, for instance on the 2nd page to show S/N 4,5, 6 and 3rd page to show 7,8,9 rows (i.e. all the search result pages show S/N 1,2,3 rows). Please find the PHP part of the code for the search-result page.
Thanks for your anticipated assistance.
$conn = mysqli_connect('localhost', 'root', '', 'hms1');
if (!$conn){
echo "Failed to connect".' '.mysqli_connect_error();
exit();
}else{
//part-coding for the pagination
if (isset($_GET['page'])){ $pageNum=$_GET['page'];
}else{
$pageNum=1;
}
//Code for processing the search starts here
if (isset($_GET['searchq']) && (!empty($_GET['searchq']))){
$search_item = mysqli_real_escape_string($conn, $_GET['searchq']);
$rows_per_page=3;//Number of Rows per page
$previous_rows=($pageNum-1)*$rows_per_page;
$result = mysqli_query($conn, "SELECT * FROM users WHERE role LIKE '%$search_item%' OR last_name LIKE '%$search_item%' OR first_name LIKE '%$search_item%' OR username LIKE '%$search_item%' Limit $previous_rows,$rows_per_page");
$num_rows = mysqli_num_rows($result);
if ($num_rows == 0){
echo "No records exist for $search_item";
exit();
}else{
echo '<table class="table table-striped table-bordered table-condensed">
<thead class="t-head">
<tr>
<th>S/N</th> <th>ID</th>
<th>ROLE</th>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>USERNAME</th>
<th>DATE CREATED</th>
</tr>
</thead>';
for($kanter=1; $kanter <= $num_rows; $kanter++){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$id = $row['id'];
$role = $row['role'];
$fname = $row['first_name'];
$lname = $row['last_name'];
$uname = $row['username'];
$dateJoined = $row['date_joined'];
echo '<tbody>
<tr>
<td>' .$kanter++.'</td>
<td>' .$id. '</td>
<td>' .$role. '</td>
<td>' .$fname. '</td>
<td>' .$lname. '</td>
<td>' .$uname. '</td>
<td>' .$dateJoined. '</td>
</tr>
</tbody>';
}
} echo '</table>';
}
}
}
$sql2 = mysqli_query($conn, "SELECT * FROM users WHERE role LIKE '%$search_item%' OR last_name LIKE '%$search_item%' OR first_name LIKE '%$search_item%'");
$num_of_pages=ceil($num_rows/3);
$num_of_rows=mysqli_num_rows($sql2);
$num_of_pages=ceil($num_of_rows/3);
$last_page=$num_of_pages;
echo"<br>";
if (isset($_GET['page'])){
$pageNum=$_GET['page'];
}else{
$pageNum=1;
}
$rows_per_page=3;//Number of Rows per page
$previous_rows=($pageNum-1)*$rows_per_page;
if ($pageNum > 1){
$page=$pageNum-1;
$prev="<a href=search-result.php?searchq=$search_item&page=$page>Back</a>";
$first="<a href=search-result.php?searchq=$search_item&page=1>First Page </a>";
}else{
$prev='Back';
$first='First Page';
}
if ($pageNum < $last_page){
$page=$pageNum + 1;
$next="<a href=search-result.php?searchq=$search_item&page=$page>Next</a>";
$last="<a href=search-result.php?searchq=$search_item&page=$last_page>Last Page</a>";
}else{
$next='Next';
$last='Last Page';
}
echo $first;?><?php echo " ";?><?php echo "|";?><?php echo $prev; ?><?php echo " ";?><?php echo "|";?><?php echo "Showing Page <b>$pageNum</b> of <b>$last_page</b>";?><?php echo " ";?><?php echo "|";?><?php echo $next;?><?php echo " ";?><?php echo "|";?><?php echo $last;
?>
type or paste code here