|
-
May 9th, 2009, 12:28 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Pagination Function
I'm basically working on a pagination function. Here's what I have so far:
PHP Code:
<?php
function printPageIndexes($currentPage, $totalPages, $theURL, $pageGET, $printExtras="")
{
if ($printExtras!="")
{
if ($currentPage!=1)
{
$toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . '"=1"><<</a> ';
$toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . ($currentPage-1) . '"><</a> ';
}
}
if ($totalPages >=10)
{
if ($currentPage<=5 || $currentPage>=($totalPages-5))
{
while($i <= $totalPages)
{
if ($i!=$currentPage)
{
// ******************************
if ($currentPage==1 || $currentPage==$totalPages)
{
if ($i <=5 || $i >=($totalPages-5))
{
$toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $i . '">' . $i . '</a> ';
}
else
{
if (!$dotsPrinter)
{
$dotsPrinter=true;
$toScreen = $toScreen . '[...]';
}
}
// ******************************
}
else
{
$toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $i . '">' . $i . '</a> ';
}
}
else
{
$toScreen = $toScreen . $i;
}
$i++;
}
}
}
else
{
while($i <= $totalPages)
{
if ($i!=$currentPage)
{
$toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $i . '">' . $i . '</a> ';
}
else
{
$toScreen = $toScreen . $i;
}
$i++;
}
}
if ($printExtras!="")
{
if ($currentPage!=$totalPages)
{
$toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . ($currentPage+1) . '">></a> ';
$toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $totalPages . '">>></a> ';
}
}
return $toScreen;
}
It's not finished yet as you can tell.
I basically want to get results like this:
1 2 3 4 5 [...] 56 57 58 59 60 61
or
1 2 3 4 5 [...] 26 27 28 29 30 31 32 33 34 35 36 [...] 56 57 58 59 60 61
or
1 2 3 4 5 6 7 8 9 10 11 12 [...] 56 57 58 59 60 61
or
1 [...] 3 4 5 6 7 8 9 10 11 12 13 [...] 56 57 58 59 60 61
Where the bolded number is the current page.
It's basically show 5 pages surrounding the current page, unless it's within 5 of the first page, or the total pages.
My problem is that I can't figure out how to do it.
I'm going to keep trying to work it out, but any help is appreciated .
-
May 9th, 2009, 12:59 PM
#2
Thread Starter
Fanatic Member
Re: Pagination Function
Figured it out .
PHP Code:
<?php /* Author: Steven Lawler (Slyke The Phoxenix) Email: [email protected] Website: http://www.stevenlawler.name
Useage: Leave credits in here, but you are free to modify the code as you wish. Just include this file in your code and call the 'printPageIndexes' function. */
function printPageIndexes($currentPage, $totalPages, $theURL, $pageGET, $controlIndexLimit=5, $printExtras="", $showAllText="") {
// currentPage = The current page you are on. // totalPages = Total amount of pages to be displayed. // theURL = The URL that you have for your page. // pageGET = The page variable you use in PHP to pass pages to your script from requests. You will need to place a ? or & where necessary. // controlIndexLimit = The amount of indexes that will be shown around your current page. EG if you are on page 35, and set this to 5, it would show 30 31 32 33 34 [35] 36 37 38 39 40. // printExtras = show <<, <, > & >> // showAllText = Show the "All Pages" hyperlink, place the data into here that you use in your script to show all items (no pagination).
if ($printExtras!="") { if ($currentPage!=1) { $toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . '=1"><<</a> '; $toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . ($currentPage-1) . '"><</a> '; } }
if ($totalPages >=10) { while($i <= $totalPages) {
if ($i!=$currentPage) { if ($i<=$controlIndexLimit || $i >=($totalPages-$controlIndexLimit)) { $toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $i . '">' . $i . '</a> '; } elseif ($i>=($currentPage-$controlIndexLimit) && $i<=($currentPage+$controlIndexLimit)) { $toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $i . '">' . $i . '</a> '; } elseif ($i<=($currentPage-$controlIndexLimit)) { if (!$showDotsLower) { $showDotsLower=true; $toScreen = $toScreen . '[...]'; } } elseif ($i>=($currentPage+$controlIndexLimit)) { if (!$showDotsUpper) { $showDotsUpper=true; $toScreen = $toScreen . '[...]'; } } } else { $toScreen = $toScreen . $i; } $i++; } } else { while($i <= $totalPages) { if ($i!=$currentPage) { $toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $i . '">' . $i . '</a> '; } else { $toScreen = $toScreen . $i; } $i++; } }
if ($printExtras!="") { if ($currentPage!=$totalPages) { $toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . ($currentPage+1) . '">></a> '; $toScreen = $toScreen . ' <a href="' . $theURL . $pageGET . "=" . $totalPages . '">>></a> '; } }
if ($showAllText!="") { $toScreen = $toScreen . ' | <a href="' . $theURL . $pageGET . "=" . $showAllText . '">All Pages</a> '; }
return $toScreen; }
Last edited by Slyke; May 9th, 2009 at 01:12 PM.
-
May 9th, 2009, 01:43 PM
#3
Re: [RESOLVED] Pagination Function
I was writing my own function to help with this, but I found a function that does it well already.
PHP Code:
//function to return the pagination string function getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 5, $targetpage = "/", $pagestring = "?page=") { //defaults if(!$adjacents) $adjacents = 1; if(!$limit) $limit = 15; if(!$page) $page = 1; if(!$targetpage) $targetpage = "/"; //other vars $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\""; if($margin || $padding) { $pagination .= " style=\""; if($margin) $pagination .= "margin: $margin;"; if($padding) $pagination .= "padding: $padding;"; $pagination .= "\""; } $pagination .= ">";
//previous button if ($page > 1) $pagination .= "<a href=\"$targetpage$pagestring$prev\">« prev</a>"; else $pagination .= "<span class=\"disabled\">« prev</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination .= "<span class=\"current\">$counter</span>"; else $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; } } elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 3)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination .= "<span class=\"current\">$counter</span>"; else $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; } $pagination .= "<span class=\"elipses\">...</span>"; $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>"; $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>"; $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>"; $pagination .= "<span class=\"elipses\">...</span>"; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination .= "<span class=\"current\">$counter</span>"; else $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; } $pagination .= "..."; $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>"; $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>"; $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>"; $pagination .= "<span class=\"elipses\">...</span>"; for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination .= "<span class=\"current\">$counter</span>"; else $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next »</a>"; else $pagination .= "<span class=\"disabled\">next »</span>"; $pagination .= "</div>\n"; } return $pagination;
}
courtesy these guys.
the only real difference is that this function requires the number of items and the number of items you'd like per page, rather than the total number of pages.
-
May 11th, 2009, 01:17 AM
#4
Re: [RESOLVED] Pagination Function
There is also a link to one in the PHP FAQ.
-
May 11th, 2009, 09:03 AM
#5
Re: [RESOLVED] Pagination Function
Slyke do you have a working example for this fucntion?
-
May 11th, 2009, 03:25 PM
#6
Thread Starter
Fanatic Member
Re: [RESOLVED] Pagination Function
I sure do, I'll explain it a little for people who find it hard to follow my code:
The software itself is rather huge, but I've posted up a modified snippet of it, where it pulls the data out of the database and paginates it. The example I've provided actually performs the SQL Query twice (One time to get the amount of results, and the second time to get the actual data), but you can use "$numRows = count($currentPageRows);" if you can figure out a way to do so.
Basically, you just need to add this to your SQL:
PHP Code:
if ($currentPage!="all") { if ($currentPage < 1) { $currentPage = 1; }
$numRowsQueryResult = mysql_query($MySQL_Query . ";", $dbConn); $numRows = mysql_num_rows($numRowsQueryResult); $MySQL_Query .= "LIMIT " . (($currentPage*$numberOfResultsPerPage)-$numberOfResultsPerPage) . ", " . $numberOfResultsPerPage; }
$currentPageRows = mysql_query($MySQL_Query . ";", $dbConn);
You would get the current page number and place it into "$currentPage" from $_GET.
Here's what calls the pagination:
PHP Code:
if ($currentPage!="all" && ceil($numRows/$numberOfResultsPerPage) >= 1) { echo "<center>Page: "; $totalPages=ceil($numRows/$numberOfResultsPerPage); echo printPageIndexes($currentPage, $totalPages, $myCurrentURL, "&page", 5, true, "all"); }
Last edited by Slyke; May 11th, 2009 at 03:32 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|