Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry and forum users.


I have a problem that I can't resolve.


I have set up my pagination script to display 5 blog posts per page. (I'm working on a local server at present so unfortunately I can't provide a link to the page). Currently, there are 15 blog posts in my db. Obviously, the pagination numbers at the bottom of my page should show three digits, '1', '2', and '3', with each page showing 5 posts. The thing is, I see four digits: '1', '2', '3'... and '4'.


When I click '1', I see the first 5 records and "index.php?s=0&p=4" is displayed in my url. Clicking '2' shows me records 6 to 10 ("index.php?s=5&p=4" displays in the url), '3' shows 11 to 15 ("index.php?s=10&p=4" displays in the url)... and clicking '4' takes me to a page displaying no posts at all.

When I add a sixteenth post, no problem. Clicking '4' shows a page with 1 post, as should be the case, and my URL shows "index.php?s=15&p=4").


So, in summary, the problem seems to arise when the number of records is evenly divisible by the number of records held in the $display variable. When the total number of records is not evenly divisible, the problem resolves itself.


Can anyone figure out what might be wrong? I'm sure it's something small... I'd really appreciate your help. My code is below (most of which is directly from your book, Larry, on pp 318, 319):


$display = 5;
// determine the number of pages
if (isset($_GET['p']) &&
is_numeric ($_GET['p'])) { // already determined
 $pages = $_GET['p'];
 } else { // must be determined
  $q = "
  SELECT COUNT(post_id)
  FROM lfbp_blog
  $r = @mysqli_query ($dbc, $q); // Run the query.
  $row = mysqli_fetch_array ($r, MYSQLI_NUM);
  $total_records = $row[0];
  // Calculate the number of pages that will be required to display all records
  if ($total_records > $display) { // means that more than 1 page will be required
  $pages = ceil($total_records/$display); // ceil function returns division result rounded upwards to nearest integer
  } else {
  $pages = 1;
} // end if
 // Determine where in the db to start returning results
 if (isset($_GET['s']) &&
  is_numeric($_GET['s'])) {
$start = $_GET['s'];
  } else {
$start = 0;
// start of news
// find blog posts to display
// end of news-->
// show links to other pages, if necessary
  if ($pages > 1) {
echo '<p>';
$current_page = ($start/$display) + 1;
// if it's not the first page, make a Previous button
if ($current_page !=1) {
 echo '<a href="index.php?s=' .
 ($start - $display) . '&p=' . $pages . '">Newer posts</a> ';
// Make all the numbered pages
for ($i = 1; $i <= $pages; $i++) {
 if ($i != $current_page) {
  echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; }
  else {
   echo $i . ' ';
} // end of FOR loop
// If it's not the last page, make a Next button
if ($current_page != $pages) {
 echo '<a href="index.php?s=' . ($start + $display) . '&p=' . $pages . '">Older posts</a>';
echo '</p>'; // Close the paragraph on the pagination line of content
  } // End of the links section

Link to comment
Share on other sites

You may have found a bug in my code. Try replacing this line:

$pages = ceil($total_records/$display);


$pages = ($total % $display == 0) ? ($total/$display) : ceil($total_records/$display);

and see if it still works.

Link to comment
Share on other sites



I do not know where is the error. According to the PHP manual, ceil() will "return the next highest integer by rounding up the provided value if necessary".

According to the code, if $display = 5, and $total_records = 15, then $pages should be equal to 3, as there will be no remainder from the division and the ceil() function should not have an effect (resulting an integer). I do not see what is the use of testing for the remainder of the division using the ternary operator as there should be no effect whatsoever.

I have even tried out the code myself and it seems to be working fine, I have 15 records in my database, $display is set to 5 and the page displays three links to paginate them.

Is there something I am not taking into account?

Help much appreciated.

Link to comment
Share on other sites

Hmmm...you know, I think you're right. I just did

echo ceil(15/5);

and the result is 3. Thanks for pointing that out!


So the good news for me is that it's not a bug in my book. The bad news is that the original problem still exists.


I would start by validating in your PHP script the values of $display and $total_results.

Link to comment
Share on other sites


  • Create New...