Php Buddies,
Look at this Pagination Page. It works fine in terms of grabbing data from mysql tbl and displaying on html table.
It works fine in terms of displaying on the first page the number of result found. But, when you go to the second page the result found is shown as zero. Why is that ?
I have set it to show 1 row per page. Since there is a 2 rows match, then on the first paginating page, it mentions 2 results found and shows 1 result as expected (1 row or result per page). So far so good.
And, heading over to the second page ,it shows the next or in our case the final row in the search. Again, so far, so good.
But wait! On this second page, it shows result found (matches: 0), even though the match (the second matching row) is being displayed on this second page. Why does the $matching_rows_count change it’s value (from ‘2’ to ‘0’) when clicking over to the second page from the first page ?
Here’s my code:
[code]
<?php
//Required PHP Files.
include ‘config.php’;
include ‘header.php’;
include ‘account_header.php’;
if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print “$errno: $errorn”;
exit();
}
else
{
//Get the Page Number. Default is 1 (First Page).
$page_number = $_GET[“page_number”];
if ($page_number == “”)
{
$page_number = 1;
}
$sender_username = $social_network_admin_username;
$recipient_username = $user;
$links_per_page = 1;
$max_result = 100;
//$offset = ($page_number*$links_per_page)-$links_per_page;
$offset = ($page_number-1)*$links_per_page;
//$query_1 = “SELECT COUNT(*) FROM notices WHERE recipient_username = ? AND sender_username = ?”;
$query_1 = “SELECT COUNT(*) FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id LIMIT ? OFFSET ?”;
$stmt_1 = mysqli_prepare($conn,$query_1);
//mysqli_stmt_bind_param($stmt_1,’ss’,$recipient_username,$sender_username);
mysqli_stmt_bind_param($stmt_1,’ssii’,$recipient_username,$sender_username,$links_per_page,$offset);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$matching_rows_count);
mysqli_stmt_fetch($stmt_1);
mysqli_stmt_free_result($stmt_1);
$total_pages = ceil($matching_rows_count/$links_per_page);
$query_2 = “SELECT id,date_and_time,recipient_username,sender_username,notice FROM notices WHERE recipient_username = ? AND sender_username = ? ORDER BY id LIMIT ? OFFSET ?”;
$stmt_2 = mysqli_prepare($conn,$query_2);
mysqli_stmt_bind_param($stmt_2,’ssii’,$recipient_username,$sender_username,$links_per_page,$offset);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_bind_result($stmt_2,$id,$date_and_time,$recipient_username,$sender_username,$notice);
mysqli_stmt_fetch($stmt_2);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional/EN”>
<html>
<head>
<meta content=”text/html; charset=ISO-8859-1″ http-equiv=” content-type”>
<title><?php echo “$site_name User $user Notices in $server_time time.”; ?></title>
</head>
<body>
<br>
<p align=”center”><span style=”font-weight:bold;”><?php echo “$site_name User $user Notices in $server_time time.”; ?></span></align>
<br>
<br>
<table width=”1500″ border=”0″ cellpadding=”5″ cellspacing=”2″ bgcolor=”#666666″>
<?php if(!$stmt_2)
{
?>
<tr>
<td bgcolor=”#FFFFFF”>No record found! Try another time.</td>
</tr>
<?php
}
else
{
if(($offset+1)<=$max_result)
{
printf(“<b> %d Result Found …</b>n”,$matching_rows_count); ?><br>
<br>
<tr name=”headings”>
<td bgcolor=”#FFFFFF” name=”column-heading_submission-number”>Submission Number</td>
<td bgcolor=”#FFFFFF” name=”column-heading_logging-server-date-and-time”>Date & Time in <?php echo “$server_time” ?></td>
<td bgcolor=”#FFFFFF” name=”column-heading_recipient-username”>To</td>
<td bgcolor=”#FFFFFF” name=”column-heading_sender-username”>From</td>
<td bgcolor=”#FFFFFF” name=”column-heading_notice”>Notice</td>
</tr>
<tr name=”user-details”>
<td bgcolor=”#FFFFFF” name=”submission-number”><?php printf(“%s”,$id); ?></td>
<td bgcolor=”#FFFFFF” name=”logging-server-date-and-time”><?php printf(“%s”,$date_and_time); ?></td>
<td bgcolor=”FFFFFF” name=”column-heading_recipient-username”><?php printf(“%s”,$recipient_username); ?></td>
<td bgcolor=”#FFFFFF” name=”column-heading_sender-username”><?php printf(“%s”,$sender_username); ?></td>
<td bgcolor=”#FFFFFF” name=”notice”><?php printf(“%s”,$notice); ?></td>
</tr>
<?php
//Use this technique: http://php.net/manual/en/mysqli-stmt.fetch.php
while(mysqli_stmt_fetch($stmt_2))
{
?>
<tr name=”user-details”>
<td bgcolor=”#FFFFFF” name=”submission-number”><?php printf(“%s”,$id); ?></td>
<td bgcolor=”#FFFFFF” name=”logging-server-date-and-time”><?php printf(“%s”,$date_and_time); ?></td>
<td bgcolor=”#FFFFFF” name=”recipient-username”><?php printf(“%s”,$recipient_username); ?></td>
<td bgcolor=”#FFFFFF” name=”sender-username”><?php printf(“%s”,$sender_username); ?></td>
<td bgcolor=”#FFFFFF” name=”notice”><?php printf(“%s”,$notice); ?></td>
</tr>
<?php
}
?>
<tr name=”pagination”>
<td colspan=”10″ bgcolor=”#FFFFFF”> Result Pages:
<?php
if($page_number<$total_pages)
{
for($i=1;$i<=$total_pages;$i++) //Show Page Numbers in Serial Order. Eg. 1,2,3.
echo “<a href=”{$_SERVER[‘PHP_SELF’]}?user=$user&page_number={$i}”>{$i}</a> “;
?><br>
<?php echo “$total_pages”; //DELETE
}
else
{
for($i=$total_pages;$i>=1;$i–) //Show Page Numbers in Reverse Order. Eg. 3,2,1.
echo “<a href=”{$_SERVER[‘PHP_SELF’]}?user=$user&page_number={$i}”>{$i}</a> “;
?><br>
<?php echo “$total_pages”; //DELETE
}
?>
</td>
</tr>
<?php
}
}
?>
</table>
<br>
<br>
<p align=”center”><span style=”font-weight:bold;”><?php echo “$site_name User $user Notices in $server_time time.”; ?></span></align>
<br>
</div>
<br>
</body>
</html>
<?php
//Free Result Set.
mysqli_stmt_free_result($stmt_2);
//Close Statement Connection.
mysqli_stmt_close($stmt_2);
//Close Database Connection.
mysqli_close($conn);
}
?>
Any ideas ?
First Page Result
Second Page Result
Error reporting on in one of the included files:
[code]
<?php
//ERROR REPORTING CODES.
declare(strict_types=1);
ini_set(‘display_errors’, ‘1’);
ini_set(‘display_startup_errors’, ‘1’);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>