Working PHP Pagination Function

Are you looking for PHP pagination function? here one slick PHP function that will paginate your database records. You can easily combine this function in your project to paginate your database records. Just drop the function within your PHP script and call it wherever you'll need the pagination. pagination_php Pagination links will look similar to picture above, as you can see I have added some CSS to style the links, and below you will find the whole pagination function, which you can copy into your projects.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
function paginate($item_per_page, $current_page, $total_records, $total_pages, $page_url) { $pagination = ''; if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number $pagination .= '<ul class="pagination">'; $right_links = $current_page + 3; $previous = $current_page - 3; //previous link $next = $current_page + 1; //next link $first_link = true; //boolean var to decide our first link if($current_page > 1){ $previous_link = ($previous==0)?1:$previous; $pagination .= '<li class="first"><a href="'.$page_url.'?page=1" title="First">«</a></li>'; //first link $pagination .= '<li><a href="'.$page_url.'?page='.$previous_link.'" title="Previous"><</a></li>'; //previous link for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links if($i > 0){ $pagination .= '<li><a href="'.$page_url.'?page='.$i.'">'.$i.'</a></li>'; } } $first_link = false; //set first link to false } if($first_link){ //if current active page is first link $pagination .= '<li class="first active">'.$current_page.'</li>'; }elseif($current_page == $total_pages){ //if it's the last active link $pagination .= '<li class="last active">'.$current_page.'</li>'; }else{ //regular current link $pagination .= '<li class="active">'.$current_page.'</li>'; } for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links if($i<=$total_pages){ $pagination .= '<li><a href="'.$page_url.'?page='.$i.'">'.$i.'</a></li>'; } } if($current_page < $total_pages){ $next_link = ($i > $total_pages)? $total_pages : $i; $pagination .= '<li><a href="'.$page_url.'?page='.$next_link.'" >></a></li>'; //next link $pagination .= '<li class="last"><a href="'.$page_url.'?page='.$total_pages.'" title="Last">»</a></li>'; //last link } $pagination .= '</ul>'; } return $pagination; //return pagination links }

Usage

Just pass few arguments to the function. Group of links, current page number, total database records and page url. You will need to get total records of database and current page number of the page from URL.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
<?php $db_username = 'root'; //database username $db_password = ''; //dataabse password $db_name = 'test'; //database name $db_host = 'localhost'; //hostname or IP $item_per_page = 5; //item to display per page $page_url = "http://localhost/ajax-pagination/"; $mysqli_conn = new mysqli($db_host, $db_username, $db_password,$db_name); //connect to MySql if ($mysqli_conn->connect_error) { //Output any connection error die('Error : ('. $mysqli_conn->connect_errno .') '. $mysqli_conn->connect_error); } if(isset($_GET["page"])){ //Get page number from $_GET["page"] $page_number = filter_var($_GET["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number }else{ $page_number = 1; //if there's no page number, set it to 1 } $results = $mysqli_conn->query("SELECT COUNT(*) FROM paginate"); //get total number of records from database $get_total_rows = $results->fetch_row(); //hold total records in variable $total_pages = ceil($get_total_rows[0]/$item_per_page); //break records into pages ################# Display Records per page ############################ $page_position = (($page_number-1) * $item_per_page); //get starting position to fetch the records //Fetch a group of records using SQL LIMIT clause $results = $mysqli_conn->query("SELECT id, name, message FROM paginate ORDER BY id ASC LIMIT $page_position, $item_per_page"); //Display records fetched from database. echo '<ul class="contents">'; while($row = $results->fetch_assoc()) { echo '<li>'; echo $row["id"]. '. <strong>' .$row["name"].'</strong> — '.$row["message"]; echo '</li>'; } echo '</ul>'; ################### End displaying Records ##################### //create pagination echo '<div align="center">'; // We call the pagination function here. echo paginate($item_per_page, $page_number, $get_total_rows[0], $total_pages, $page_url); echo '</div>';

CSS

Here's CSS you will need to make your pagination links pretty.
CSS
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
.pagination{ margin:0; padding:0; } .pagination li{ display: inline; padding: 6px 10px 6px 10px; border: 1px solid #ddd; margin-right: -1px; font: 13px/20px Arial, Helvetica, sans-serif; background: #FFFFFF; } .pagination li a{ text-decoration:none; color: rgb(89, 141, 235); } .pagination li.first { border-radius: 5px 0px 0px 5px; } .pagination li.last { border-radius: 0px 5px 5px 0px; } .pagination li:hover{ background: #EEE; } .pagination li.current { background: #89B3CC; border: 1px solid #89B3CC; color: #FFFFFF; }
Below is the link to gist if you want to see whole implementation of this function. View & Download Gist File
  • Thank you for the great functions. With some minor modifications (some reported previosly by others), it works great. What i will add, except the ones commented by others, is regarding the filtering if the page is number. You don;t have to end the script if is not numeric, but you can clean the request easly. So, this part:
    • 1
    • 2
    $page_number = filter_var($_GET["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
    ...can be modified as follows:
    • 1
    $page_number = mysqli_real_escape_string($conn,preg_replace('/[^0-9]/', '', $_GET["page"])); //clean the reques by replacing everything except numbers
    You can clean it also by not using mysqli_real_escape_string() function by just using preg_replace, too, if you will not pass it to db. I prefer to use as I stated on before. This way, your request will be cleaned (every time will be number) and your script can continue to show the results. Hope it was helpful for everyone.
  • Thanks for the function it's really helpful but there is something wrong with the $previous variable. It should be like this;
    • 1
    $previous = $current_page - 1;
New question is currently disabled!