Sunday, September 14, 2008

Pagination in PHP

For every website project, you will have to write a pagination (for navigating to other pages).  You will need to use an algorithm for a pagination.  Here is my algorithm which you can copy and paste into your project:

function get_pagination($offset, $limit, $number_of_all_items, $page_string, $url) {
  $pagination = '';
  
  $number_of_pages = ceil($number_of_all_items / $limit);

  $offset++;
  
  $current_page = round($offset / $limit);
  if ($offset % $limit != 0) {
    $current_page++;
  }
  
  $window = 10; # Number of visible pagers per page.
  
  $left_link_count;
  if (($current_page - 1) > round($window / 2)) {
    $left_link_count = round($window / 2) - 1;
  }
  else {
    $left_link_count = $current_page - 1;
  }
  
  $page_no = $current_page - $left_link_count;
  
  if ($page_no > 1) {
    $pagination .= "<<";
  }
  
  $counter = 0;
  $break = false;
  
  if ($number_of_all_items != 0) {
    while (!$break && ($counter < ($window - 1))) {
      if ($page_no <= $number_of_pages) {
        if ($page_no == $current_page) {
          $pagination .= " " . $page_no . " ";
        }
        else {
          $pagination .= " <a class=\"button_link\" href=\"" . $url . "&" . $page_string . "=" . $page_no . "\">" . $page_no . "</a> ";
        }
        
        $page_no++;
        $counter++;
      }
      else {
        $break = true;
      }
    }
  }
  
  if ($page_no < $number_of_pages) {
    $pagination .= "&gt;&gt;";
  }
  
  return $pagination;
}

#
# Print "previous page"/"next page" link for pagination.
#
function print_previous_page_next_page_for_pagination($current_page, $limit, $number_of_all_items, $page_string, $url) {
  $number_of_pages = $number_of_all_items / $limit;
  if ($number_of_all_items % $limit != 0) {
    $number_of_pages++;
  }

  if ($current_page == 1) {
    echo "<span class=\"previous_page_next_page_arrow_off\">&lt;&lt;</span>";
  }
  else {
    echo "<span class=\"previous_page_next_page_arrow_on\" title=\"" . htmlspecialchars($TEXTS['PAGINATION_PREVIOUS'], ENT_QUOTES) . "\"><a href=\"" . htmlspecialchars($url, ENT_QUOTES) . "&page=" . ($current_page - 1) . "\">&lt;&lt;</a></span>";
  }
  
  echo "&nbsp;&nbsp;&nbsp;&nbsp;";
  
  if ($current_page == $number_of_pages) {
    echo "<span class=\"previous_page_next_page_arrow_off\">&gt;&gt;</span>";
  }
  else {
    echo "<span class=\"previous_page_next_page_arrow_on\" title=\"" . htmlspecialchars($TEXTS['PAGINATION_NEXT'], ENT_QUOTES) . "\"><a href=\"" . htmlspecialchars($url, ENT_QUOTES) . "&page=" . ($current_page + 1) . "\">&gt;&gt;</a></span>";
  }
}

To use it:

$pagination = get_pagination($offset, G_NUMBER_OF_SEARCH_RESULTS_PER_PAGE, $number_of_all_results, 'page', 'article_search.php?action_type=search&article_name=' . urlencode($article_name));

$offset is the offset in your sql select query.
G_NUMBER_OF_SEARCH_RESULTS_PER_PAGE is the maximum number of rows (that is LIMIT) in your sql select query.
$number_of_all_results is the number of all results of your sql select query without LIMIT.
'page' is the name of the variable you want to use as an HTTP GET variable to store the current page the user is in.
The last parameter is the URL you want for each link.

0 comments:

Post a Comment