Dynamic Pagination Plugin For PHP Scripts

          I was recently engaged with a PHP application where dynamic pagination is needed. I faced many problems and took much time to build dynamic pagination function. If you are a person who involved in developing a site with pagination, here is the right plugin. Don’t waste your time by building it from scratch. Here, I have provided the function and methods to use. The script is user-friendly and allows you to style as you need.

Screen shots with different pages selected:


The Function:

function pagination($total_num_results,$posts_per_page,$cur_page) {
  $no = $total_num_results / $posts_per_page;
  $no = ceil($no);

  if($end_diff < 2) {
  while($start_page < 1) { $start_page=$start_page+1; }
  if($start_diff < 2) {
  while($end_page > $no) { $end_page=$end_page-1; }

  //creating div with `id`=pagination
  $pages_html .= "<div id='pagination' >";

  //displaying current page out of the total number of pages available
  $pages_html .= "<label class='disptext' >Page ".$cur_page." of ".$no."</label>";
  //appending first page button
  if($start_diff > 2) { $pages_html .= "<a href='?page=1' > << First </a>"; }
  //appending previous page button
  if($cur_page > 1) { $prev=$cur_page-1; $pages_html .= "<a href='?page=".$prev."' > < Prev </a>"; }

  //appending the page number buttons
  for($i=$start_page; $i<=$end_page; $i++) { $pages_html .= "<a href='?page=".$i."' id='page".$i."' > ".$i." </a>"; }

  //appending the next page button
  if($cur_page < $no) { $next=$cur_page+1; $pages_html .= "<a href='?page=".$next."' > Next > </a>"; }

  //Appending the last page button
  if($end_diff > 2) { $pages_html .= "<a href='?page=".$no."' > Last >> </a>"; }
  //jquery script to highlight the current page button
  $pages_html .= "<script type='text/javascript'>var d = document.getElementById('page".$cur_page."');
  d.className += ' selectedpage';</script>";
  $pages_html .= "</div><!--pagination-->";

  return $pages_html; //returns html content.

Copy the above PHP function any where on your page and call it where it is required.

Calling The Function:

       //or get the count dynamically as shown below.
       $query=mysql_query("YOUR QUERY TO FETCH RESULTS FROM DATABASE"); //query to show results from.
       $total_num_results = mysql_num_rows($query); //Total count of the fetched results.
  $posts_per_page="15"; //how many results do you need to show on a single page.
  $cur_page="1"; //current page. by default it is set to 1.
  if(isset($_GET['page'])) { $cur_page=$_GET['page']; } //if variable `page` is set in url parameters, then change the current page.
  $html=pagination($total_num_results,$posts_per_page,$cur_page); //calling pagination function.
  echo $html; //printing the result.

The above code is pasted where you need to call the function. All the variables declared above are required (Description about each variable is given in comments).

Default CSS:

 #pagination { width: 600px;margin: 0 auto; }
 #pagination a { padding: 5px 10px; margin: 2px 3px 0px 3px; float: left; font-size: 12px; line-height: 15px;
                 font-family: Arial, Helvetica, Sans-serif; color: #666; background-color:#eeeeee;
                 text-decoration: none; border-radius: 5px; }
 #pagination a.selectedpage, #pagination a:hover { background-color: lightblue; color: #fff;}
 #pagination .disptext{ margin: 5px 5px 0 10px; float: left; color: #666;
                 font-family: Muli, 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Tahoma, Arial, sans-serif;}

I have provided the default css styles. You can edit easily however you need.

If you got struck at any point, feel free to leave a comment. I will help you out as soon as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *