PHP Pagination Class Script

Paginate your MySQL resultset, using some extended link navigation row or simple back and forward links.

The pagination of records from a database is used on many dynamic websites, the most popular navigation we know is the one on Google. This PHP class (also called MyPagina) is extremely easy to use in your existing application, just use your own SQL statement and voila you get the information about your MySQL result set (page navigation links and the number of records). While using this script all other query string parameters are re-used inside each page link. We included a small database table which works with the standard example script. You can use a complete list of links or just simple back and forward links.

Visit here Script DemoPHP Script Download

How-to use the PHP Pagination Class

On this page we explain how-to use the class. We provide examples for the different navigation controls and the link information. All these examples are included inside the download (example.php). To run the example code (out of the box) you need to import the example data (example_table.sql) by using phpMyAdmin or any other MySQL client/tool.

How-to start?

Customize the settings at the top of the class file (my_pagina_class.php) and upload all files to your web server.

A special note about your database connection

In most of the times you use already a database connection on your site, if this is the case include that code in your script above the include statement for the class script. If you don’t use a database connection on your site, you need to enter the database settings in the class file as well. It’s also possible to disable the connection inside the class constructor method (using “false” will disable the connection within the class”):

$test = new MyPagina($rows = 50, $connect = false);

Creating the MySQL result set

The following code is used to create a MySQL result from our test data:

$test = new MyPagina($rows = 50, $connect = true);
$test->number_links = 4; // show four numeric links inside the navigation
$test->sql = "SELECT * FROM example ORDER BY id"; // the SQL statement, don't use a limit here
$result = $test->get_page_result(); // get the resultset
 
while ($obj = mysql_fetch_object($result)) { // loop through the resultset
    echo = $obj->id.' » '.$obj->title;
}

PHP pagination class examples (click here for a live demo)

Use the following code to create text based forward and backward links (from above):

$test->forw = "forward";
$test->back = "backward";
echo $test->back_forward_link();

You can use text symbols instead of text or use images using this code:

echo $test->back_forward_link(true);

If you like to use a navigation with numbers and additional links you can use this example:

echo $test->navigation(" | ", "currentStyle", false, false, false, true);

The value “currentStyle” is used in your CSS stylesheet to manipulate the style for the current page number. The four booleans are used for (from left to the right): $numbers_only = false, $only_back_forward = false, $use_images = false, $use_start_end = true

This information shows the current range of records and the total number of records.

echo $test->page_info("Result: %d - %d of %d records");

Using this function you need to keep the “%d” values because they are used for the number.

Change log for the PHP Pagination Class

Version 1.04

Inside the example script there is a new navigation method which is showing only numbers decorated in colored boxes, these are added as an alternative type of page navigation. There is also a new example for back- and forward links using a text string only. We fixed also some small bugs, inside the navigation() method, the start number for the page links was in some cases to high (if the number of maximum links and the number of the current link was the same) and one in the method get_page_result(). In this version there is a new function to show links for the first and the last page if the same link is not available by the other links or numbers. There are new variables created to used by this function and by the configuration file. We needed to update the methods navigation() and build_back_or_forward() to work with that new function. The new function is disabled by default, check the updated example file for how-to use the this feature.

Version 1.03

We created a new variable with the name $max_rows, this variable is used to limit the maximum number of results during a query. That will say if there are more rows then the number of $max_rows, only the last one will show up. Use the new variable $outstanding_rows if you like to inform the visitor about that. Leave the value inside the constructor method empty and the function is disabled. Several variables are removed to give this class a safer and better structure. The method page_info() which builds the string with page number information is changed, only one (input) string is needed now (we format the string with the PHP function sprintf()).

Version 1.02

The link text (and the new image function) for the forward and backward links will be created with the new method called build_back_or_forward(). We removed the variables $str_forward and $str_backward because they are not needed anymore. Check the example file for the possibility of using images instead of a text string for the back- and forward navigation and check also the modified navigation() method.

Version 1.01

There was a small bug inside the page_info() method if the script shows the last page of records. The error (last record) is fixed. There is also a small update in the method set_page(), the method checks now the $_REQUEST values instead of the $_GET values.