Reputation: 497
I'm currently using the php pagination script below. The pagination displays six results per page with previous/next links. I was wondering if anyone might know what I could add so that the pagination also displays links to the first two pages and the last two pages? Like the pagination in this example page: http://www.winelog.net/wines/Oregon
$data=file("data.txt");
$pages=0;
foreach($data as $temp){
$x=explode("|",$temp);
if($x[0] > 0){
$pages=$pages+1;
}
}
if($_GET['p']){
$page=$_GET['p'];
}
if($_GET['i']){
$index=$_GET['i'];
}
if($index == "p"){
$page=$page-1;
}
if($index == "n"){
$page=$page+1;
}
if($page < 1){
$page=1;
}
if($page > $pages){
$page=$pages;
}
$line=$data[$page-1];
$fields=explode("|",$line);
The displayed navigation:
$show=6;
echo "<li><a href='?i=p&p=$page'>« PREV</li></a>";
if($page-($show/2) > 1){
$temp=$page-$show;
echo "<li><a href='?p=$temp'>...</li></a>";
}
if($page-($show/2) >= 1 && $page+($show/2) <= $pages){
$start=$page-($show/2);
$stop=$page+($show/2);
}
if($page-($show/2) < 1){
$start=1;
$stop=$show;
}
if($page+($show/2) > $pages){
$start=$pages-$show;
$stop=$pages;
}
for($i=$start; $i<=$stop; $i++){
if($page==$i){
echo "<li class='active'>$i</li></a>";
}
else{
echo "<li><a href='?p=$i'>$i</li></a>";
}
}
if($page+($show/2) < $pages){
$temp=$page+$show;
echo "<li><a href='?p=$temp'>...</li></a>";
}
echo "<li><a href='?i=n&p=$page'>NEXT »</li></a>";
Upvotes: 1
Views: 3640
Reputation: 535
The link to the winelog site is broken. Hence, I couldn't see the sample you are interested in. I have written a pagination script in PHP which shows two adjacent pages to the active page, i.e.
< 1 2 3 4 5 > // 3 being the active page
< 1 2 3 4 > // 2 being the active page
Here is the code:
/* set total records */
$total_records = 26; // this is the total count of values in an array or query results from the database
/* set records per page */
$records_per_page = 5;
/* calculate total pages */
$total_pages = ceil($total_records / $records_per_page);
/* get and set value for current page */
if (isset($_GET['page'])) {
$current_page = $_GET['page'];
} else {
$current_page = 1;
}
/* calculate and set previous and next page values */
$previous = $current_page - 1;
$next = $current_page + 1;
/* set start page value */
$start_page = 1;
/* set number of pages to display to the left */
/* maximum value should be 4 */
$pages_to_left = 2;
/* set number of pages to display to the right */
/* maximum value should be 4 */
$pages_to_right = 2;
/* show previous pages to the left */
if ($current_page <= $total_pages && $current_page > $start_page + $pages_to_left) {
$start_page = $current_page - $pages_to_left;
}
/* show next pages to the right */
if ($current_page <= $total_pages && $current_page > $start_page - $pages_to_right) {
$end_page = $current_page + $pages_to_right;
if ($current_page == $total_pages || $current_page + 1 == $total_pages || $current_page + 2 == $total_pages || $current_page + 3 == $total_pages) {
$end_page = $total_pages;
}
} else {
$end_page = $total_pages;
}
/* show previous button */
if ($current_page > 1) {
echo '<a href="?page='.$previous.'">«</a>';
echo " ";
}
/* display pages */
for ($page = $start_page; $page <= $end_page; $page++) {
echo '<a href="?page='.$page.'">'.$page.'</a>';
echo " ";
}
/* show last page button */
if ($end_page + $pages_to_right <= $total_pages || $end_page != $total_pages) {
echo '<a href="?page='.$total_pages.'">…' . $total_pages . '</a>';
}
/* show next button */
if ($current_page < $total_pages) {
echo " ";
echo '<a href="?page='.$next.'">»</a>';
}
Here is a pastebin link to the script: https://pastebin.com/espTvimy
Kindly check it out and let me know if it works for you.
Upvotes: 0
Reputation: 922
The full code of their pagination script is available for free @Stranger Studios There is a php
and a Perl
script available for download.
Code PHP version:
<?php
//function to return the pagination string
function getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 1, $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;
}
?>
Change the code to fit your needs and add some CSS
.
There are a lot of questions about pagination, I hope this is helpful to users who in need of a pagination script..
Upvotes: 1
Reputation:
Here's what you need to do.
First get a count of the number of 'pages' that there are.
If there are less than, say, 10 pages - output all the pages.
Otherwise:
Output from current page - 5 to current page + 5.
Before the output, put a 'First' button - page = 1
After the output, put a 'Last' button - page = total number of pages.
If you want a second-to-last button, just go page = total number of pages - 1, etc.
You might want to look into Zend_Paginator - You don't need to use the whole Zend Framework to use individual parts of it, it's designed so it can be pulled apart.
Upvotes: 2