[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 :).
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.
Re: [RESOLVED] Pagination Function
There is also a link to one in the PHP FAQ.
Re: [RESOLVED] Pagination Function
Slyke do you have a working example for this fucntion?
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");
}