/ 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).
Expand |
Embed | Plain Text
function paginate($number_of_links, $current_page, $page_uri, $max_pages) { $pagination = "<ul>\n<li>" . ($current_page == 1 ? "<span>«</span>" : "<a href=\"" . str_replace('[PAGE]', 1, $page_uri) . "\">«</a>") . "</li>\n<li>" . ($current_page == 1 ? "<span>‹</span>" : "<a href=\"" . str_replace('[PAGE]', $current_page - 1, $page_uri) . "\">‹</a>") . "</li>\n"; if($last_page<$number_of_links+1&&$last_page<$max_pages) { $last_page += ($number_of_links - $last_page) + 1; } if($last_page>$max_pages) $last_page = $max_pages + 1; for($i=$current_page<($number_of_links/2)?1:$last_page-$number_of_links;$i<$last_page;$i++) { $pagination .= "<li>" . ($i == $current_page ? "<span>$i</span>" : "<a href=\"" . str_replace('[PAGE]', $i, $page_uri) . "\">$i</a>") . "</li>\n"; } $pagination .= "<li>" . ($current_page < $max_pages ? "<a href=\"" . str_replace('[PAGE]', $current_page + 1, $page_uri) . "\">›</a>" : "<span>›</span>") . "</li>\n<li>\n" . ($current_page != $max_pages ? "<a href=\"" . str_replace('[PAGE]', $max_pages, $page_uri) . "\">»</a>" : "<span>»</span>") . "</li>\n"; $pagination .= "</ul>\n"; return $pagination; }
You need to login to post a comment.
