MYSQL Pagination Class
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
I think the current setup is confusingn00b Saibot wrote:First, Last Links will make sense even morePimptastic wrote:Gonna make it place "..." at the end of the pages numbers when there are more pages so that it makes more sense
edit | oh, and with that I am a GURU, yay!
The output looks like this
Code: Select all
Prev | Next | 1 | 2 | 3 | 4 | 5 | First | Last- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
I guess changing to following or something similar will make it less confusing
Code: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | ... | 9 | Next | Last- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I've updated to output it as follows
or
or
Code: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | 6 | ... | Next | LastCode: Select all
Prev | First | ... | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ... | Next | LastCode: Select all
Prev | First | ... | 7 | 8 | 9 | 10 | 11 | 12 | Next | Last- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
Okay, i have tested this out.
Seems to be a weird issue...
I have 11 pages of results
When on the first page the navigation looks like this
...when i got to page 2, the navigation looks like this
...notice the link to page 6 has gone...now i click the page 3 link
notice the page 6 link is now back

Seems to be a weird issue...
I have 11 pages of results
When on the first page the navigation looks like this
Code: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | 6 | ... | Next | LastCode: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | ... | Next | LastCode: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | 6 | ... | Next | Last- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
Pimptastic wrote:Okay, i have tested this out.
Seems to be a weird issue...
I have 11 pages of results
When on the first page the navigation looks like this
...when i got to page 2, the navigation looks like thisCode: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | 6 | ... | Next | Last
...notice the link to page 6 has gone...now i click the page 3 linkCode: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | ... | Next | Last
notice the page 6 link is now backCode: Select all
Prev | First | 1 | 2 | 3 | 4 | 5 | 6 | ... | Next | Last
![]()
![]()
![]()
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Check out this function for outputting page numbering like phpBB
1 2 3 ... 15 16 17 .... 55 56 57
Also read up here: http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
on FOUND_ROWS()
1 2 3 ... 15 16 17 .... 55 56 57
Also read up here: http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
on FOUND_ROWS()
A SELECT statement may include a LIMIT clause to restrict the number of rows the server returns to the client. In some cases, it is desirable to know how many rows the statement would have returned without the LIMIT, but without running the statement again. To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT statement, and then invoke FOUND_ROWS() afterward:
Code: Select all
function create_pagination($url, $page, $maxPage) {
// if there is no need for pagination return the stuff empty
if($maxPage == 1) {
return ' <b>1</b> ';
}
$pagination = '';
$current_page = $page;
//if there are 10 or more links create the special pagination
if ( $maxPage >= 10 ) {
//show the first 3 links
if($current_page <= 5) {
//this is to prevent that if page 1 is selected only 2 links are shown instead of 3.
if($current_page == 1) {
$maximum = $current_page + 2;
} else {
$maximum = $current_page + 1;
}
for($i = 1; $i <= $maximum; $i++) {
$pagination .= ($i == $current_page) ? '<strong>'.$i.'</strong>' : '<a href="' .$url .$i. '">' .$i. '</a>';
if($i < $maximum) {
$pagination .= ', ';
}
}
} else {
//if the page is not in the first row of links show the the first 3 links.
$maximum = 3;
for($i = 1; $i <= $maximum; $i++) {
$pagination .= ($i == $page) ? '<strong>'.$i.'</strong>' : '<a href="' .$url .$i. '">' .$i. '</a>';
if($i < $maximum) {
$pagination .= ', ';
}
}
}
if($current_page > 5) {
//show the first dots
$pagination .= ' ... ';
//and show the link in front and behind $current_page
if($current_page <= $maxPage) {
//this creates the links if page is higher then 5.
if($current_page == $maxPage) {
$start_num = $current_page - 2;
} else {
$start_num = $current_page - 1;
}
$max_num = 3;
for($i = 1; $i <= $max_num; $i++) {
$pagination .= ($start_num == $current_page) ? '<strong>'.$start_num.'</strong>' : '<a href="' .$url .$start_num. '">' .$start_num. '</a>';
if($i < $maximum) {
$pagination .= ', ';
}
$start_num++;
}
}
//see if there have to be dots at the end.
}
if($current_page <= ($maxPage - 5)) {
//its smaller so we have to show the dots.
$pagination .= ' ... ';
}
//see how many links we should put at the end of the links row.
//if the current page is is the last or the one before the last we display no links.
if($current_page == $maxPage) {
$max_num = 0;
}
if($current_page == ($maxPage -1)) {
$max_num = 0;
}
if($current_page == ($maxPage -2)) {
$max_num = 1;
}
if($current_page == ($maxPage -3)) {
$max_num = 2;
}
if($current_page <= ($maxPage -4)) {
$max_num = 3;
}
//little thing to check the output of the above if functions.
if($max_num !== 0) {
$start_num = $maxPage - ($max_num - 1);
if($current_page >= ($maxPage - 4)) {
$pagination .= ', ';
}
for($i = 1; $i <= $max_num; $i++) {
$pagination .= '<a href="' .$url .$start_num. '">' .$start_num. '</a>';
if($start_num < $maxPage) {
$pagination .= ', ';
}
$start_num++;
}
}
} else {
//if there are 9 links or less create a link string
$nextLink = array();
for($i = 1; $i <= $maxPage; $i++) {
$nextLink[] = ($i == $page) ? '<strong>'.$i.'</strong>' : '<a href="' .$url .$i. '">' .$i. '</a>';
}
$pagination .= implode(', ', $nextLink);
}
return $pagination;
}