How to make serial number of paginated tabulated search result continous

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 "&nbsp";?><?php echo "|";?><?php echo $prev; ?><?php echo "&nbsp";?><?php echo "|";?><?php echo "Showing Page <b>$pageNum</b> of <b>$last_page</b>";?><?php echo "&nbsp";?><?php echo "|";?><?php echo $next;?><?php echo "&nbsp";?><?php echo "|";?><?php echo $last;							
											
	?>
type or paste code here

It will help me an image of your page.By the way I’ll suggest you to do this.Divide the total number of selected rows with the integer 3 (because your pagination page must have 3 rows/page).
So $x=ceil($num_rows/3);
Then if $x=1 // the page is working with the 1 or 2 or 3 rows.
If $x>1 then you must set $x=(the number of the 2nd or 3rd page etc.)
I hope that this helps you

I did just that as you can see in the code. The code works perfectly well without pagination. The issue here is that while page 1 displays the 3 rows with S/N 1-3 correctly, the 2nd page instead of showing rows S/N 4-6 starts counting 1-3 again, same with other pages. I suspect the issue is with my kanter ++ iteration of the S/N but cant just figure out how to correct that.
Thanks

It seems that you don’t set the starting value based on the page number, so it would always start again every time you call the code. You need to do something based on your $pageNum variable. There’s a chance I might have missed it as you have some very wide indenting going on above.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.