Contact / Hire Me

PHP - Pagination Function

PLEASE NOTE:

This has now been superseeded by my PHP / MySQLI Pagination Class.

This PHP pagination function is somehing I wrote for myself, but I thought I'd share it for others. You can see it in action in the blog area of this site.

To use the function, you'll need to make a call to this function:

// Function paginated_query()
paginated_query($sql_query,$base_url,$page_number,$results_per_page);

// Heres an example:
$query = paginated_query("SELECT id,title FROM blog","http://www.mysite.com/blog/",1,6);

while($row = mysql_fetch_assoc($query['results'])){
  echo $row['title'].'
'; } // To echo the page number links: echo $query['page_links'];

There are to parts - the PHP, and the CSS. Here's the code:

function paginated_query($mysql_query,$base_url,$page_number=0,$number_of_rows_to_return=20)
{
  $page_number = $page_number - 1;
  // If SQL_CALC_FOUND_ROWS inst in the string - add it!
  if(stristr($mysql_query, 'SQL_CALC_FOUND_ROWS') === FALSE){
    $mysql_query = str_replace('SELECT ', 'SELECT SQL_CALC_FOUND_ROWS ', $mysql_query);
  }
  // Build query for return
  $offset_limit = ($page_number * $number_of_rows_to_return);
  $get_results = mysql_query($mysql_query." LIMIT ".$offset_limit.", ".$number_of_rows_to_return);
  // Get total number of rows
  $count_rows_query = mysql_query("SELECT FOUND_ROWS();");
  $count_rows = mysql_fetch_array($count_rows_query);
  // Calculate the total pages we will have
  $total_pages = ceil($count_rows[0] / $number_of_rows_to_return);
  $total_pages = $total_pages;
  $total_results_feedback = $count_rows[0];
  
  $links_current_page = $page_number + 1;
  // Build PREV link
  if($links_current_page == 1){
    $links_prev = '';
  } else {
    $links_prev = '«';
  }
  // Build NEXT link
  if($links_current_page == $total_pages){
    $links_next = '';
  } else {
    $links_next = '»';
  }
  // Build MIDDLE Links
  $links_middle = '';
  if($total_pages <= 14){
    $counter = 1;
    while($counter <= $total_pages){
      if($counter == $links_current_page){
        $links_middle .= ''.$counter.'';
      } else {
        $links_middle .= ''.$counter.'';
      }
      $counter++;
    }
  }
  if($total_pages > 14){
    if($links_current_page <= 8){
      $counter = 1;
      while($counter <= 10){
        if($counter == $links_current_page){
          $links_middle .= ''.$counter.'';
        } else {
          $links_middle .= ''.$counter.'';
        }
        $counter++;
      }
      $links_middle .= ' ... ';
      $links_middle .= ''.($total_pages - 1).'';
      $links_middle .= ''.$total_pages.'';
    }
    if($links_current_page > 8 && ($links_current_page < $total_pages - 8)){
      $links_middle = '1';
      $links_middle .= '2';
      $links_middle .= ' ... ';
      $counter = $links_current_page - 4;
      while($counter <= ($links_current_page + 4)){
        if($counter == $links_current_page){
          $links_middle .= ''.$counter.'';
        } else {
          $links_middle .= ''.$counter.'';
        }
        $counter++;
      }
      $links_middle .= ' ... ';
      $links_middle .= ''.($total_pages - 1).'';
      $links_middle .= ''.$total_pages.'';
    }
    if($links_current_page > 8 &&  ($links_current_page >= $total_pages - 8)){
      $links_middle = '1';
      $links_middle .= '2';
      $links_middle .= ' ... ';
      $counter = $total_pages - 9;
      while($counter <= $total_pages){
        if($counter == $links_current_page){
          $links_middle .= ''.$counter.'';
        } else {
          $links_middle .= ''.$counter.'';
        }
        $counter++;
      }
    }
  }
  // Join the PREV, MIDDLE and NEXT link blocks
  $output_page_link = '';
  // If no links, build EMPTY link block
  if($total_pages == 0){
    $output_page_link = '';
  }
  // Return the array
  $pagination_array = Array (
    "page_links" => $output_page_link,
    "results" => $get_results,
    "total_pages" => ($total_pages),
    "total_results" => $total_results_feedback
  );
  return $pagination_array;
}

And here's the CSS:

.pagination_links {
	text-align: right;
  width: 400px;
  float: right;
	padding-top: 20px;
	padding-bottom: 10px;
  font-family: verdana, sans-serif;
  font-size: 12px;
}

.pagination_links a, .pagination_links a:link {
	color: #1f506b;
	text-decoration: none;
  margin: 0px;
}

.pagination_links a:hover {
	color: #1f506b;
	text-decoration: none;
}

.pagination_link {
	padding: 5px;
	border: 0px solid #9a9a9a;
	color: #666666;
	margin-right: 4px;
  margin-left: 0px;
	text-decoration: none;
}

.pagination_link_active {
	padding: 5px;
	border: 0px solid #1f506b;
	color: #1f506b;
	margin-right: 0px;
	text-decoration: none;
}

.pagination_link_active:hover {
	padding: 5px;
	border: 0px solid #1f506b;
	color: #1f506b;
	margin-right: 0px;
	text-decoration: none;
}

.pagination_link_active a, .pagination_link_active a:link {
	color: #1f506b;
	text-decoration: none;
}

.pagination_link_active a:hover {
	color: #1f506b;
	text-decoration: none;
}

.pagination_link_active_current {
	padding: 5px;
	border: 0px solid #1f506b;
	background-color: #1f506b;
	margin-right: 0px;
	font-weight: bold;
	color: #ffffff;
}

.pagination_link_active_current a, .pagination_link_active_current a:link, .pagination_link_active_current a:hover {
	color: #ffffff;
	text-decoration: none;
}