/    Sign up×
Community /Pin to ProfileBookmark

Why Matching Rows Count Changes After Clicking Next Page On SERP ?

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);
}
?>
[/code]

Any ideas ?

First Page Result
[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-08-20/1534809382-990632-2-found.png]
Second Page Result
[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-08-20/1534809414-676688-0-found.png]

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);

?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@rootAug 21.2018 — IMHO $page_number = $_GET["page_number"];
if ($page_number == "")
{
$page_number = 1;
}
needs to be done away with.

Use a session to record what page the person is on and recall that value with each new page. All it takes is for $_GET['page_number"] to have a similar value of zero length string as that is what it is testing for, a string that is zero length, it is not testing to see if it isset() which returns a boolean true or false for when the variable has been set, you use empty() to check if it is an empty string.

Then you have $total_pages = ceil($matching_rows_count/$links_per_page); which you are calculating pages based on the results that you have limited already... you do not need to do that, you can use MySQL to track that for you by way of the LIMIT OFFSET and the COUNT() of the results from a query to count the number of matching results.

On a side note... It is NO LONGER ... &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"&gt; but now &lt;!DOCTYPE html&gt; HTML4 was taken out and burried somewhere in those foot hills... Its now HTML5.
Copy linkTweet thisAlerts:
@rootAug 21.2018 — Also, this is a duplicate of a thread you have already opened.

DO NOT DUPLICATE THREADS... It makes getting an answer harder because of duplication.

Not helpful to you or those trying to help you.
Copy linkTweet thisAlerts:
@rootAug 21.2018 — {"locked":true}
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...