After receiving your PM I decided to write a better code, it is still a quick hack but I think a better quality quick hack:
<?php
$page = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
// string append_amp_or_question(string url)
// This is to add parameters to the query string. It decides whether it is the first parameter
// which would require a '?', or if it is an additional parameter which would require a '&'
// @url string: the URL in question
function append_amp_or_question($url) {
$chr = (strpos($url,'?') === false)? '?' : '&';
return $url.$chr;
}
// string page_loop(int begin, int end, int page, string url)
// This loops through displaying the links for each number.
// @begin string: The number to begin with.
// @end string: The number to end with.
// @end string: The current page the user is on.
// @url string: The URL in question.
function page_loop($begin, $end, $page, $url)
{
$pagination_string = '';
for ($i=$begin; $i <= $end; $i++) {
$pagination_string .= ($i == $begin) ? '' : ',';
$pagination_string .= ($i == $page) ? ' <b>'. $i .'</b>' : ' <a href="'. append_sid(append_amp_or_question($url) .'page='. $i) .'">'. $i .'</a>';
}
return $pagination_string;
}
//string generate_pagination(string url, int total_rows, int rows_per_page, int page)
// This is the only function you should have to call directly, it has all the main logic
// @url: The URL in question.
// @total_rows: The total amount of rows are being used.
// @rows_per_page: The amount of rows_per_page
// @page: The current page the user is on.
function generate_pagination($url, $total_rows, $rows_per_page, $page)
{
$pagination_string = '';
$pages = ceil($total_rows / $rows_per_page);
// If page requested out of range.
if ($page < 1 || $page > $pages) {
$page = 1;
}
// No numbers needed.
if ($pages == 1) {
return '';
}
// If pages less than 10
if ($pages <= 10) {
$pagination_string = page_loop(1, $pages, $page, $url);
}
// If pages more than 10 and the current page is 5 or less
// We display first 3 pages, and 3 last pages.
elseif ($page <= 5) {
if ($page < 3) {
$x=3;
}
else {
$x=$page;
}
$pagination_string = page_loop(1, $x, $page, $url);
$pagination_string .= ' ...';
$pagination_string .= page_loop($pages-2, $pages, $page, $url);
}
// Otherwise we will display it differently.
else {
// We display our first 3 pages, as always.
$pagination_string = page_loop(1, 3, $page, $url);
$pagination_string .= ' ...';
// If we are more than 4 pages away from the end, we display (from the current page)
// the previous page and the next page, a break, then the last 3 pages
if (($pages - $page) > 4) {
$pagination_string .= page_loop($page-1, $page+1, $page, $url);
$pagination_string .= ' ...';
$pagination_string .= page_loop($pages-2, $pages, $page, $url);
}
// If the current page is not the last page, we show the previous page to the last page
elseif ($page != $pages) {
$pagination_string .= page_loop($page-1, $pages, $page, $url);
}
// We show the last three pages.
else {
$pagination_string .= page_loop($pages-2, $pages, $page, $url);
}
}
$pagination_string = ($page > 1) ? '<a href="'. append_sid(append_amp_or_question($url) .'page='. ($page-1)) .'">Prev</a>,' . $pagination_string : $pagination_string;
$pagination_string = ($page < $pages) ? $pagination_string . ', <a href="'. append_sid(append_amp_or_question($url) .'page='. ($page+1)) .'">Next</a>' : $pagination_string;
return $pagination_string;
}
?>
Basically, call generate_pagination() with the required parameters and it will return the string. Let me know if you need further help, and if you want to donate to charity do so.
Edit: Opps, I forgot to add Previous and Next links. Added them now.