Ben Griffiths - Freelance PHP Web Developer
Ben Griffiths - Freelance PHP Developer

PHP Pagination Function

05
Apr

This page is now out of date - you can find version 2 of this function here.

Heres a function that I wrote to get a decent form of pagination with php and MySQL. I wrote this before I ever started using frameworks, and for quickly getting paginated results, it works quite well. It’s by no means perfect, but does the job!

As noted in the comments, some poeple don’t know what a pagination script does. Put simply, when you pull hundreds of records out of a database, a script like this can seperate your results into say 10 per page, with some previous/next links for navigating pages.

Make sure you change your site in the SITEPATH definition.

You can download the original as a zip here: [pagination.zip 1.4kb]

Here is the source code:

<?

define
("SITEPATH""http://www.my-site.com");

function 
pagination($querystring,$page_number,$num_rows_to_return,$extra_url_info) {
    global 
$mysql_queries_debugging;
    global 
$dev_mode;
    
$base_url SITEPATH.$extra_url_info;
    if (
$page_number == null){
        
$page_number 0;
    }
    
$p_dash_position strpos($base_url'/p');
    if (
$p_dash_position === false) {
        
// do nowt
    
} else {
        
$p_position strrpos($base_url"/p");
        
$base_url_length strlen($base_url);
        
$base_url substr($p_position$p_position$base_url_length);
    }
    
// If SQL_CALC_FOUND_ROWS inst in the string - add it!
    
if(stristr($querystring'SQL_CALC_FOUND_ROWS') === FALSE){
        
$querystring str_replace('SELECT ''SELECT SQL_CALC_FOUND_ROWS '$querystring);
    }
    
// Build query for return
    
$offset_limit = ($page_number $num_rows_to_return);
    
$start_time explode(' 'microtime());
    
$start_time =  $start_time[1] + $start_time[0];
    
$get_results mysql_query($querystring." LIMIT ".$offset_limit.", ".$num_rows_to_return."") or die(mysql_error());
    
$micro_time explode(' 'microtime());  
    
$query_execution_time $micro_time[0] +  $micro_time[1] - $start_time;
    if(
$dev_mode == 1){
    
$mysql_queries_debugging .= '
Query: '
.$querystring." LIMIT ".$offset_limit.", ".$num_rows_to_return.'
Time Taken: '
.round($query_execution_time5).'
'
;
}
    
// Get total number of rows
    
$start_time explode(' 'microtime());
    
$start_time =  $start_time[1] + $start_time[0];
    
$count_rows_query mysql_query("SELECT FOUND_ROWS();");
    
$micro_time explode(' 'microtime());  
    
$query_execution_time $micro_time[0] +  $micro_time[1] - $start_time;
    if(
$dev_mode == 1){
    
$mysql_queries_debugging .= '
Query: '
."SELECT FOUND_ROWS();".'
Time Taken: '
.round($query_execution_time5).'
'
;
}
    
$count_rows mysql_fetch_array($count_rows_query);
    
// Calculate the total pages we will have
    
$total_pages ceil($count_rows[0] / $num_rows_to_return);
    
$total_pages $total_pages 1;
    
$total_results_feedback $count_rows[0];
    
// Build page links section
    
$prev_link_page $page_number 1;
    
$next_link_page $page_number 1;
    if (
$prev_link_page 0){
        
$prev_link_page 0;
    }
    if (
$next_link_page $total_pages){
        
$next_link_page $total_pages;
    }
    
$middle_page_links '';
    
$pages_start = ($page_number 3) + 1;
    if (
$pages_start 1){
        
$pages_start 1;
    }
    
$count_to $pages_start 6;
    if (
$count_to > ($total_pages 1)){
        
$count_to = ($total_pages 1);
    }
    
$first_mid_link '';
    
$last_mid_link '';
    for (
$counter $pages_start$counter <= $count_to$counter += 1) {
        
$page_link $counter 1;
        if (
$counter != ($page_number 1)){
            
$middle_page_links .= '<a href="'.$base_url.'page:'.$page_link.'/">'.$counter.'</a>';
            if (
$counter $count_to){
                
$middle_page_links .= ' &middot; ';
            }
            if(
$first_mid_link == ''){
                    
$first_mid_link $page_link;
                }
            
$last_mid_link $page_link;
        } else {
            
$middle_page_links .= ' <strong>'.$counter.'</strong>';
            if (
$counter $count_to){
                
$middle_page_links .= ' &middot; ';
            }
        }
    }
    if(
$page_number == 0){
        
$first_link 'First &middot; Prev | ';
        
$first_dots '';
    } else {
        
$first_link '<a href="'.$base_url.'page:0/">First</a> &middot; <a href="'.$base_url.'page:'.$prev_link_page.'/">Prev</a> | ';
        if(
$page_number 3){
            if(
$page_number != 1){
                
$first_dots ' <a href="'.$base_url.'page:0/">1</a> ... ';
            }
        } else {
            
$first_dots '';
        }
    }
    if(
$page_number == $total_pages){
        
$last_link ' | Next &middot; Last';
        
$last_dots '';
    } else {
        
$last_link ' | <a href="'.$base_url.'page:'.$next_link_page.'/">Next</a> &middot; <a href="'.$base_url.'page:'.$total_pages.'/">Last</a>';
        if(
$last_mid_link <= ($total_pages 1) || $last_mid_link != $total_pages){
            
$last_dots ' ... <a href="'.$base_url.'page:'.$total_pages.'/">'.($total_pages 1).'</a> ';
        } else {
            
$last_dots '';
        }
    }
    
$output_page_link '<div>'.$first_link.$first_dots.$middle_page_links.$last_dots.$last_link.'</div>';

    if(
$total_pages == -1){
        
$output_page_link '<div>First &middot; Prev |  <strong>1</strong> | Next &middot; Last</div>';
    }
    
    
$pagination_array = Array (
        
"page_links" => $output_page_link,
        
"results" => $get_results,
        
"total_pages" => ($total_pages 1),
        
"total_results" => $total_results_feedback
    
);
    return 
$pagination_array;
}    

?>

Client Tools

Blog Entries & PHP Tutorials

Connect with Me

Most Popular Pages

My Daily Reads

Sweet Flickry Goodness

100_0415100_0410Rainbow in rotherhamDanny at the parkDanny AsleepThe DashThe Back SeatMy New Car!
Flex PHP Framework