Posted By

jatkins on 03/29/11


Tagged

html generator navigation pagination


Versions (?)

Pagination Navigation Generation


 / Published in: PHP
 

Released into the public domain. Pass $page_uri as the URI of the paginated results page (use the [PAGE] placeholder where the page number would go, and this script will replace it accordingly).

  1. function paginate($number_of_links, $current_page, $page_uri, $max_pages) {
  2. $pagination = "<ul>\n<li>" . ($current_page == 1 ? "<span>&laquo;</span>" : "<a href=\"" . str_replace('[PAGE]', 1, $page_uri) . "\">&laquo;</a>") . "</li>\n<li>" . ($current_page == 1 ? "<span>&lsaquo;</span>" : "<a href=\"" . str_replace('[PAGE]', $current_page - 1, $page_uri) . "\">&lsaquo;</a>") . "</li>\n";
  3. $last_page = $current_page + round($number_of_links / 2);
  4. if($last_page<$number_of_links+1&&$last_page<$max_pages) {
  5. $last_page += ($number_of_links - $last_page) + 1;
  6. }
  7. if($last_page>$max_pages)
  8. $last_page = $max_pages + 1;
  9. for($i=$current_page<($number_of_links/2)?1:$last_page-$number_of_links;$i<$last_page;$i++) {
  10. $pagination .= "<li>" . ($i == $current_page ? "<span>$i</span>" : "<a href=\"" . str_replace('[PAGE]', $i, $page_uri) . "\">$i</a>") . "</li>\n";
  11. }
  12. $pagination .= "<li>" . ($current_page < $max_pages ? "<a href=\"" . str_replace('[PAGE]', $current_page + 1, $page_uri) . "\">&rsaquo;</a>" : "<span>&rsaquo;</span>") . "</li>\n<li>\n" . ($current_page != $max_pages ? "<a href=\"" . str_replace('[PAGE]', $max_pages, $page_uri) . "\">&raquo;</a>" : "<span>&raquo;</span>") . "</li>\n";
  13. $pagination .= "</ul>\n";
  14. return $pagination;
  15. }

Report this snippet  

You need to login to post a comment.