Hey guys, here's the pagination function that I've been working on. It is based off of Websavvy's pagination program so I don't want to take full credit for it. Everything works as I want to except one feature and I will explain that later. But here's the code.
Code:
/* GET CURRENT PAGE */
$get_page = $_GET['p'];
/* SET PAGE LIMIT TO 25 FOR CURRENT TESTING */
$page_limit = "25";
/* QUERY TO GET TOTAL NUMBER OF ENTRIES FOR PAGINATE FUNCTION */
$query_total = "SELECT * FROM schedules";
function pagination($get_page,$display,$query_total,$page_limit) {
/* SET SOME VARIABLES */
if(!$get_page) $p = 1;
if(!$display) $display = 5;
$page_next = ($get_page + 1);
$page_prev = ($get_page - 1);
/* GET TOTAL NUMBER OF ENTRIES FIRST */
$results = mysql_query($query_total);
$totalrows = mysql_num_rows($results);
/* CALCULATE LAST PAGE LINK */
$page_last = ceil($totalrows/$page_limit);
/* USE $start VAR FOR DATABASE QUERY ex: LIMIT $start,$page_limit */
$start = ($get_page - 1) * $page_limit;
$starting_no = $start + 1;
if (($totalrows - $start) < $page_limit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $page_limit) {
$end_count = $start + $page_limit;
}
/* CREATE PAGES TO DISPLAY */
for ($i=1; $i<=$page_last; $i++) {
if ($i != $get_page) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
else $display_pages[] = " $i";
}
/* IF ON PAGE 1, UNLINK FIRST AND PREV LINKS */
if($get_page == 1) {
$first = "First";
$prev = "Prev";
}else{
$first = "<a href=\"$_SERVER[PHP_SELF]?p=1\">First</a>";
$prev = "<a href=\"$_SERVER[PHP_SELF]?p=$page_prev\">Prev</a>";
}
/* IF ON LAST PAGE, UNLINK LAST AND NEXT LINKS */
if($get_page == $page_last) {
$last = "Last";
$next = "Next";
}else{
$last = "<a href=\"$_SERVER[PHP_SELF]?p=$page_last\">Last</a>";
$next = "<a href=\"$_SERVER[PHP_SELF]?p=$page_next\">Next</a>";
}
return (array($first,$prev,$display_pages,$next,$last,$totalrows,$starting_no,$start,$page_limit,$end_count));
}###END FUNCTION
list($first,$prev,$display_pages,$next,$last,$totalrows,$starting_no,$start,$page_limit,$end_count) = pagination($_GET[p],5,$query_total,$page_limit);
echo "<span class=\"style13\">" . "$first | $prev" . implode(' ',$display_pages) . " $next | $last" . "</span>";
Code:
OUTPUT YOUR RECORDS: SECOND QUERY USING LIMIT
$query2 = "SELECT * FROM schedules LIMIT $start,$page_limit";
$result2 = mysql_query($query2);
etc etc etc......
Ok, so here is what the output is:
First | Prev 1 2 3 4 Next | Last
and
Viewing Items (1 - 25 of 76 Entries)
Everything works pretty well. When you are on the first page,
the "First" and "Prev" gets unlinked since there's no need to click on them. If you are on the last page, the same thing happens for the "Next" and "Last" links..
There is only one issue that I have, and that is the "1 2 3 4" part. That is based on how many entries are in the table divided by how many items to list per page.
Anyways, if you have a lot of entries, say 1,000. You will get too many links like this: "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ....etc".
What I would like to do is keep it "clean". Displaying only 5 at a time with the "current" page in the middle:
So say you are on page 3. It would look like this.
First | Prev 1 2 3
4 5 Next | Last
If you're on page 7. It would look like this.
First | Prev 5 6 7
8 9 Next | Last
If anyone has "hints" please let me know, it's one of those things that you can either figure out in 5 minutes or it takes a day...I don't think it's hard, just a matter of "figuring" it out...but I know there are a lot of people here with "pagination" experience.
Also, PLEASE comment on my code, if you have suggestions to make it better, let me know, I won't be offended...
Just don't tell me to do it in OO because I'm still new to that and that will be my next project...