I have a pagination script with PHP. When page records are some hundreds, the pagination result is too big. How I can limit the page numbers/links?
Example: < 1 | 2 ... 37 | 38 | 39 | 40 | 41 | 42 ... 82 | 83 >
This is my PHP script
<?php
$ppp = 10;
$rows = mysql_num_rows($query);
$nmpages = ceil($rows/$ppp);
// if current page is not 1, draw PREVIOUS link
if ($pg > 1 && $nmpages != 0) {
echo "< ";
}
For($i = 1 ; $i <= $nmpages ; $i++) {
If($i == $pg) {
echo "<b>".$i."</b> ";
} else {
echo "".$i." ";
}
}
// if current page less than max pages, draw NEXT link
if ($pg < $nmpages && $nmpages != 0) {
echo ">";
}
?>
Do you have an ideas how I can do this with the specific PHP script that I have?
Try this :
<?php
$link = "";
$page = $_GET['pg']; // your current page
// $pages=20; // Total number of pages
$limit=5 ; // May be what you are looking for
if ($pages >=1 && $page <= $pages)
{
$counter = 1;
$link = "";
if ($page > ($limit/2))
{ $link .= "1 ... ";}
for ($x=$page; $x<=$pages;$x++)
{
if($counter < $limit)
$link .= "".$x." ";
$counter++;
}
if ($page < $pages - ($limit/2))
{ $link .= "... " . "".$pages." "; }
}
echo $link;
?>
OUTPUT :
//At page=1
1 2 3 4 ... 20
//At page=12
1 ... 12 13 14 15 ... 20
//At page=18
1 ... 18 19 20
An improvement or rather re-write based on #Makesh's code.
function get_pagination_links($current_page, $total_pages, $url)
{
$links = "";
if ($total_pages >= 1 && $current_page <= $total_pages) {
$links .= "1";
$i = max(2, $current_page - 5);
if ($i > 2)
$links .= " ... ";
for (; $i < min($current_page + 6, $total_pages); $i++) {
$links .= "{$i}";
}
if ($i != $total_pages)
$links .= " ... ";
$links .= "{$total_pages}";
}
return $links;
}
OUTPUT:
page = 1
1 2 3 4 5 6 ... 20
page = 10
1 ... 5 6 7 8 9 10 11 12 13 14 15 ... 20
page = 19
1 ... 14 15 16 17 18 19 20
The answer for this question was basically to visit a page about Digg style pagination which includes code samples.
So that's the answer, but this question is basically a duplicate.
Try to make a page bracket for example 10 less and 10 more than the actual page, change for example the for statement for this:
For($i = $pg-10 ; $i <= $pg+10 ; $i++)
I wanted to get an array of numbers by the current page and total page. So this is what I came up with. I hope this helps others -
Caution - this work if total page is more than 10. I felt if the total
page is less than 10 then just show it in a single for loop.
function getSmartPageNumbers($currentPage, $totalPage)
{
$pageNumbers = [];
$diff = 2;
$firstChunk = [1, 2, 3];
$lastChunk = [$totalPage - 2, $totalPage - 1, $totalPage];
if ($currentPage < $totalPage) {
$loopStartAt = $currentPage - $diff;
if ($loopStartAt < 1) {
$loopStartAt = 1;
}
$loopEndAt = $loopStartAt + ($diff * 2);
if ($loopEndAt > $totalPage) {
$loopEndAt = $totalPage;
$loopStartAt = $loopEndAt - ($diff * 2);
}
if (!in_array($loopStartAt, $firstChunk)) {
foreach ($firstChunk as $i) {
$pageNumbers[] = $i;
}
$pageNumbers[] = '.';
}
for ($i = $loopStartAt; $i <= $loopEndAt; $i++) {
$pageNumbers[] = $i;
}
if (!in_array($loopEndAt, $lastChunk)) {
$pageNumbers[] = '.';
foreach ($lastChunk as $i) {
$pageNumbers[] = $i;
}
}
}
return $pageNumbers;
}
Test:
getSmartPageNumbers(8, 20);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => .
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 10
[9] => .
[10] => 18
[11] => 19
[12] => 20
)
getSmartPageNumbers(1, 20);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => .
[6] => 18
[7] => 19
[8] => 20
)
Related
So i wrote a simple pagination function:
function pagination ( $iTotalItems )
{
if( is_array( $iTotalItems ) )
$totalItems = sizeof ( $iTotalItems );
if( is_int( $iTotalItems ) )
$totalItems = $iTotalItems;
$page = (int)( empty( $_GET['page'] ) ? 1 : $_GET['page'] );
if ($page <= 0) $page = 1;
$itemsPerPage = PAG_PER_PAGE;
$prev = $page - 1;
$next = $page + 1;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$totalPages = ceil( $iTotalItems / $itemsPerPage );
$perPage = $page == 'all' ? $totalItems : $itemsPerPage;
$adjacents = 2;
if( $totalItems > $itemsPerPage )
{
$filler = '';
$NumStart = '';
$NumEnd = '';
$pageNavigation = '<div class="Pagination">'. PHP_EOL;
$previousLink = $prev !== 0 ? "<a href='?p=items&page={$prev}'>{$prevlabel}</a>". PHP_EOL : "<span class='noneLink'>{$prevlabel}</span>". PHP_EOL;
$nextLink = $page < $totalPages ? "<a href='?p=items&page={$next}'>{$nextlabel}</a>". PHP_EOL : "<span class='noneLink'>{$nextlabel}</span>". PHP_EOL;
for( $i = 1; $i < $totalPages + 1; $i++ )
{
if( $i <= 10 ) { $NumStart .= $page == $i ? '<span class="noneLink">'.$i.'</span>' : ''.$i.''. PHP_EOL; }
else { $filler = '...'; }
if( $i > $totalPages - 3 ) {$NumEnd .= $page == $i ? "<span class='noneLink'>{$i}</span>" : "<a href='?p=items&page={$i}'>{$i}</a>". PHP_EOL; }
}
}
return $pageNavigation . $previousLink . $NumStart . $filler . $NumEnd . $nextLink . '</div>';
}
Now it works great and all but Im not sure how to make it change depending on the page. For example: with 40 pages and while on page 1 it looks like this.
‹ Prev 1 2 3 4 5 67 8 9 10 ...38 39 40 Next ›
However, even if im at page.. say 20 it still looks the same.
Whats the best and most simple way to code something like this while on page 1:
1 2 3 4 5 6 7 8 9 … 38 39 40 Next ›
and something like this while on page 15:
‹ Prev 1 2 3 … 11 12 13 14 15 16 17 18 19 … 38 39 40 Next ›
Thanks in advance!
Right now my pagination would show up something like this
[1] [2] [3] [4] [5] [6] [7] [8] [9]
How would i make it show up like this
[1] [2] [3] [4] [5]... [9]
<?php
$per_page = 10;
$pages_query = mysql_query ("SELECT COUNT(`message_id`) FROM `messages`") or die(mysql_error());
$pages = ceil(mysql_result ($pages_query, 0) / $per_page);
$page = (isset ($_GET['page'])) ? (int) $_GET['page'] : 1;
$start = ($page - 1) * $per_page;
?>
Relevant if statement that echo out pagination
<?php
if ($pages >=1 && $page <= $pages) {
for ($x=1; $x<=$pages;$x++) {
echo "".$x."";
}
}
?>
Try this :
<?php
$link = "";
// $page = $_GET['page'];
// $pages=20; // Hardcoded for testing purpose
$limit=5 ;
if ($pages >=1 && $page <= $pages)
{
$counter = 1;
$link = "";
if ($page > ($limit/2))
{ $link .= "1 ... ";}
for ($x=$page; $x<=$pages;$x++)
{
if($counter < $limit)
$link .= "".$x." ";
$counter++;
}
if ($page < $pages - ($limit/2))
{ $link .= "... " . "".$pages." "; }
}
echo $link;
?>
OUTPUT :
//At page=1
1 2 3 4 ... 20
//At page=12
1 ... 12 13 14 15 ... 20
//At page=18
1 ... 18 19 20
<?php
if ($pages >=1 && $page <= $pages) {
$counter = 1;
$link = "";
for ($x=1; $x<=$pages;$x++) {
if($counter < 5)
$link .= "".$x."";
$counter++;
}
$link .= "...";
$link .= "".$pages."";
}
echo $link;
?>
Basically i want to limit the amount of numbers displayed on my page, but currently it's displaying 31 numbers, because their is 31 pages of results, i'd like to show 5 links and just increment after every page,
Here's how they're currently being displayed
[1] [2] [3] [4] [5] [6] [7] [8] etc..
Here's what i'd like
[1] [2] [3] [4] [5]
What i'd like i just but once you select for example number 8, it'd place that number in the middle and 2 links either side for example like this
[6] [7] [8] [9] [10]
Here's the current code that i'm using
function showPageNumbers($totalrows,$page,$limit){
$query_string = $this->queryString();
$pagination_links = null;
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
$pagination_links .= '<li class="active">'.$i.'</li>';
}else{
$pagination_links .= '<li>'.$i.'</li>';
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
$pagination_links .= '<li class="active">'.$i.'</li>';
}else{
$pagination_links .= '<li>'.$i.'</li>';
}
}
return $pagination_links;
}
You can have modified function like:
function showPageNumbers($totalrows, $page, $limit){
$query_string = $this->queryString();
$pagination_links = null;
$numofpages = $totalrows / $limit;
$linksPerPage = 5;
$firstNum = $page - round(($linksPerPage)/2);
if($firstNum < 1)
$firstNum =1;
$lastNum = $firstNum + $linksPerPage -1;
if($lastNum > $numofpages)
$lastNum = $numofpages;
for($i = $firstNum; $i <= $lastNum; $i++){
if($i == $page){
$pagination_links .= '<li class="active">'.$i.'</li>';
}else{
$pagination_links .= '<li>'.$i.'</li>';
}
}
return $pagination_links;
}
I use this code for my custom paging :
global $wpdb, $table_prefix, $current_user;
get_currentuserinfo();
$umail = $current_user->user_email;
$paged = $wpdb->get_results("SELECT * FROM {$table_prefix}comments WHERE comment_author_email = '$umail'");
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = COUNT($paged);
$pages = ceil($pages / 2);
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") $querystring .= "$key=$value&";
}
// Pagination
for ($i = 1; $i <= $pages; $i++) {
echo "<a " . ($i == $page ? "class=\"selected\" " : "");
echo "href=\"?{$querystring}page=$i";
echo "\">$i</a> ";
}
This code paginate my comments look like this : 1 2 3 4 5 6 7 8 9 10 11
How can change code to get paginate look like this: 1 2 3 ... 11
Thanks for any help.
Try the following loop:
$page = 8;//current page
$pages = 15;//count of all pages
$batch_middle = 5;//the approximate number of pages in the middle links to display. Current page will be in the middle
$batch_lr = 1;//number of pages in the left and right links
for($i = 1; $i <= $pages;$i++)
{
//display first links
if ($i <= $batch_lr)
{
echo $i . ' ';
continue;
}
//display last links
if ($i > $pages-$batch_lr)
{
echo $i . ' ';
continue;
}
//display middle links
if ($i>=($page-floor($batch_middle/2)) && $i<=($page+floor($batch_middle/2)))
{
echo $i . ' ';
continue;
}
//placeholder
echo ' ... ';
//move the pointer
$i = ($i < $page) ? ($page-floor($batch_middle/2)-1) : ($pages-$batch_lr) ;
}
//output example 1: 1 2 ... 14 15
//output example 2: 1 2 ... 7 8 9 ... 14 15
//output example 3: 1 2 3 4 5 ... 14 15
//output example 4: 1 ... 6 7 8 9 10 ... 15
Replace $page and $pages with your logic with getting a current page and count of total pages.
Replace the $batch_middle and $batch_lr to configure a number of links in the first, second and third batches of the links
So I'm doing this in PHP but it is a logic issue so I'll try to write it as generically as possible.
To start here's how this pagination script works:
for (draw first three pages links)
if (draw ellipsis (...) if there are pages between #1's pages and #3's pages)
for (draw current page and two pages on each side of it links)
if (draw elipsis (...) if there are pages between #3's pages and #5's pages)
for (draw final three pages links)
The problem is that when there are low amounts of pages (I noticed this when the page count was at 10) there should be an ellipsis but none is drawn.
Onto the code:
$page_count = 10; //in actual code this is set properly
$current_page = 1; //in actual code this is set properly
for ($i = 1;$i <= 3;$i++)
{
if ($page_count >= $i)
echo $i;
}
if ($page_count > 3 && $current_page >= 7)
echo "...";
for ($i = $current_page - 2;$i <= current_page + 2;$i++)
{
if ($i > 3 && $i < $page_count - 2)
echo $i;
}
if ($page_count > 13 && $current_page < $page_count - 5)
echo "...";
for ($i = $page_count - 2;$i <= $page_count;$i++)
{
if ($page_count > 3)
echo $i;
}
So I figure the best idea would to be to modify one of the two ellipsis if statements to include a case like this, however I've tried and am stumped.
Also please note that I condensed this code for readability sake so please don't give tips like "those for loops are ineffective because they will recalculate current_page - 2 for each iteration" because I know :)
For those whom want to see a breakdown of how this logic currently works, here is example output ( modified ) with iterating $page_count and $current_page.
http://rafb.net/p/TNa56h71.html
<?php
/**
* windowsize must be odd
*
* #param int $totalItems
* #param int $currentPage
* #param int $windowSize
* #param int $anchorSize
* #param int $itemsPerPage
* #return void
*/
function paginate($totalItems, $currentPage=1, $windowSize=3, $anchorSize=3, $itemsPerPage=10) {
$halfWindowSize = ($windowSize-1)/2;
$totalPages = ceil($totalItems / $itemsPerPage);
$elipsesCount = 0;
for ($page = 1; $page <= $totalPages; $page++) {
// do we display a link for this page or not?
if ( $page <= $anchorSize ||
$page > $totalPages - $anchorSize ||
($page >= $currentPage - $halfWindowSize &&
$page <= $currentPage + $halfWindowSize) ||
($page == $anchorSize + 1 &&
$page == $currentPage - $halfWindowSize - 1) ||
($page == $totalPages - $anchorSize &&
$page == $currentPage + $halfWindowSize + 1 ))
{
$elipsesCount = 0;
if ($page == $currentPage)
echo ">$page< ";
else
echo "[$page] ";
// if not, have we already shown the elipses?
} elseif ($elipsesCount == 0) {
echo "... ";
$elipsesCount+=1; // make sure we only show it once
}
}
echo "\n";
}
//
// Examples and output
//
paginate(1000, 1, 3, 3);
// >1< [2] [3] ... [98] [99] [100]
paginate(1000, 7, 3, 3);
// [1] [2] [3] ... [6] >7< [8] ... [98] [99] [100]
paginate(1000, 4, 3, 3);
// [1] [2] [3] >4< [5] ... [98] [99] [100]
paginate(1000, 32, 3, 3);
// [1] [2] [3] ... [31] >32< [33] ... [98] [99] [100]
paginate(1000, 42, 7, 2);
// [1] [2] ... [39] [40] [41] >42< [43] [44] [45] ... [99] [100]
This is probably an overcomplicated solution, but it works.
I've used an array here instead of just printing, which lets me "do-over" the logic.
Part of the problem occurs when "left and right of page" happens to coincide with left-and-right shoulders.
function cdotinator ( $current_page, $page_count )
{
$stepsize = 3;
$elipse = '...';
# Simple Case.
if ( $page_count <= 2 * $stepsize )
{
$out = range( 1, $page_count );
$out[$current_page - 1 ] = '*' . $current_page . '*';
return $out;
}
#Complex Case
# 1) Create All Pages
$out = range( 1, $page_count );
# 2 ) Replace "middle" pages with "." placeholder elements
for( $i = $stepsize+1 ; $i <= ( $page_count - $stepsize ) ; $i ++ )
{
$out[ $i - 1 ] = '.' ;
}
# 3.1 ) Insert the pages around the current page
for( $i = max(1,( $current_page - floor($stepsize / 2) )) ;
$i <= min( $page_count,( $current_page + floor( $stepsize/2)));
$i ++ )
{
$out[ $i - 1] = $i;
}
# 3.2 Bold Current Item
$out[ $current_page - 1 ] = '*' . $current_page . '*' ;
# 4 ) Grep out repeated '.' sequences and replace them with elipses
$out2 = array();
foreach( $out as $i => $v )
{
# end, current == peek()
end($out2);
if( current($out2) == $elipse and $v == '.' )
{
continue;
}
if( $v == '.' )
{
$out2[] = $elipse;
continue;
}
$out2[]= $v;
}
return $out2;
}
Output can be seen here: http://dpaste.com/92648/