1. Computing

Pagination of MySQL Query Results

By , About.com Guide

4 of 4

Query and Results Explained
The first thing we do is re-run our query from earlier, only with one slight change. This time we are including the $max variable to limit the query results to those that belong on our current page. After your query you would display your results as normal (using any formatting you wish.)

After the results are displayed, we let the user know what page they are currently on, and the total number of pages that exist. This is not necessary but it is nice information for the user to know.

Then we generate the navigation. We assume that if you are on the first page, then you don't need a link to the first page. And as it is the first result, no previous page exists. So we check (if ($pagenum == 1) ) to see if we are on page one. If we are, then nothing happens. If we aren't on page one, then we use PHP_SELF and the page numbers to generate links to both the first page, and the the previous page.

We do almost the exact same thing to generate the links on the other side, however this time we are checking to make sure we aren't on the last page. If we are, then we don't need a link to the last page, nor does a next page exist.

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP with MySQL
  5. Pagination - PHP Pagination - PHP MySQL Pagination

©2013 About.com. All rights reserved.