Pagination: Easy as PREV 1 2 3 NEXT (REVISED)

Pagination: Easy as PREV 1 2 3 NEXT (REVISED)

Alright. You’ve had enough, haven’t you? Pouring countless hours into trying to figure out pagination. Yep, I feel your pain. Don’t fret though, this tutorial will get you through it without too many bruises–it’s easier than you think.

For those of you who don’t know, pagination (in the Web world) is usually dividing a large list of items into several pages, and then providing a nifty link for it. You usually see pagination link displayed like this:

PREV 1 2 3 4 5 NEXT

Now pagination could be done easily with plain HTML: Just add X amount of items to a page, make a new page, add more items, and finally inter-link the pages. As easy as this seems, this method will start to become a burden once you have over 100+ items you want to list. The manual labor of adding the code would be simply mundane, not to mention the logistics of inter-linking all the pages–imagine if you had over 10,000 items, or if your list was updated regularly, or if it was sorted! You’d have to search through many pages to find out where your new entry fits. As you can see, static HTML pagination for large numbers, dynamic lists, or sorted lists can become a major pain in the… err… processor.

Despite the troubles of static pagination with HTML, dynamic pagination with PHP can be accomplished quite easy. Note, however, that you will need dynamic content to use this method of pagination. The dynamic content used in this tutorial is an imaginary MySQL table, but the concept can be used for other forms of dynamic content.

Now let’s get started on the NEXT page (no pun intended… ok, ok, it was intended).

Here’s were we start the code (the fun part). I’m going to split the code into sections so I can explain it easier. I will display the code in full–with minimal comments–at the end.

For this pagination example we’re going to be listing all the users in our database. Since we’re using a database, this first block of code is going to connect to our MySQL database.

PHP Example:


<?php
 
    // The first function connects to your database, the second selects a database
    @mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db("database") or die("ERROR--CAN'T CONNECT TO DB");
    
    /* Tip: The @ in front of these two functions will hide the username and
password if there is an error. The die() function will kill the script and show
an error statement if something goes wrong with the connect or select functions.
A mysql_connect() error usually means your username/password are wrong, and a
mysql_select_db() error usually means the database doesn't exist. */

That was easy, wasn’t it? Yep. Next, we’re going to tell the script: how many items we want displayed per page with the $limit variable (we want 25 per page); and we’re going to count the amount of items with a simple MySQL query.

PHP Example:


    $limit      = 25;              
    // Sets how many results shown per page
    
    $query_count    = "SELECT count(*) FROM table";    
    // Sets what we want to pull from the database
    // count(*) is better for large databases (thanks Greg!)
    
    $result_count   = mysql_query($query_count);    
    // Pulls what we want from the database
    
    $totalrows  = mysql_num_rows($result_count);    
    // This counts the number of users
 

Moving along. The first two lines in this next block of code make up an if statement. It checks to see what page we’re on; if it doesn’t find a page (first load of this script) it automatically sets the page number to the first (1).

Then, the $limitvalue variable is used to find where to start pulling the data from when we use our LIMIT statement in the MySQL query. Basically it starts from where we last left off. So if we’re on page one, it’ll start with the first entry; if we’re on page 2, it’ll start with the 26th entry. The MySQL query in this block of code pulls all the data we need to display.

PHP Example:


    if(empty($page)){    // Checks if the $page variable is empty (not set)
        $page = 1;      // If it is empty, we're on page 1
    }
 
    $limitvalue = $page * $limit - ($limit);
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25
    
    $query  = "SELECT * FROM table LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error());
    // Selects all the data from table.
    // mysql_error() will print an error if one occurs.
    
    /* Tip: The MySQL LIMIT value syntax is as follows:
    
    LIMIT $row_to_start_at, $how_many_rows_to_return
    
    */

Phew! On the next page, we’ll print the results on the screen.

Read Rest Of PHP Pagination Tutorial On PHP Freaks