11

I have a pagination script that displays a list of all pages like so:
prev [1][2][3][4][5][6][7][8][9][10][11][12][13][14] next
But I would like to only show ten of the numbers at a time:
prev [3][4][5][6][7][8][9][10][11][12] next

How can I accomplish this? Here is my code so far:

<?php
/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */
$max_results = 2;

/* Calculate the offset */
$from = (($page * $max_results) - $max_results);

/* Query the db for total results. 
   You need to edit the sql to fit your needs */
$result = mysql_query("select title from topics");

$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $max_results);

$pagination = '';

/* Create a PREV link if there is one */
if($page > 1)
{
    $pagination .= '< a href="?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
    {
        $pagination .= $i;
    }
    else
    {
        $pagination .= '< a href="index.php?page='.$i.'">'.$i.'</a>';
    }
}

/* Print NEXT link if there is one */
if($page < $total_pages)
{
    $pagination .= '< a hr_ef="?page='.$next.'"> Next</a>';
}

/* Now we have our pagination links in a variable($pagination) ready to
   print to the page. I pu it in a variable because you may want to
   show them at the top and bottom of the page */

/* Below is how you query the db for ONLY the results for the current page */
$result=mysql_query("select * from topics LIMIT $from, $max_results ");

while ($i = mysql_fetch_array($result))
{
    echo $i['title'].'<br />';
}
echo $pagination;
?> 
Carson Myers
  • 34,352
  • 35
  • 118
  • 164
arthur
  • 113
  • 1
  • 1
  • 4
  • 1
    We'd like to answer questions, not doing your job for you. – Your Common Sense Jun 14 '10 at 11:48
  • how do you intend to automatically move the page, after showing 10 records. You need JS for that you know – Starx Jun 14 '10 at 11:48
  • 2
    why is your href with a "_" in between – Starx Jun 14 '10 at 11:51
  • cuz this is the first time I post something here, and it was shouting smth about link limits when posting first time – arthur Jun 14 '10 at 11:54
  • I fixed the href's for you but I'm reluctant to perform anymore edits, lest this question fall into the community wiki pit of doom. I think the reason it was shouting about link limits is because you didn't have your code formatted correctly, so it thought you were actually trying to put links in your post. – Carson Myers Jun 14 '10 at 11:55

3 Answers3

32

10 next pages

for($i = $page + 1; $i <= min($page + 11, $total_pages); $i++)

or if you want 5 prev and 5 next

for($i = max(1, $page - 5); $i <= min($page + 5, $total_pages); $i++)
Svisstack
  • 15,015
  • 6
  • 59
  • 97
  • thanks for your quick answer, It was realy helpful.. never thought It would be so fast! :) – arthur Jun 14 '10 at 12:11
  • ahh.. didn't know about that accept thing. just last question.. How to make that all the time there is constant amount of pages(numbers) displayed. - when I open, it displays pages 1-6, every time when I press next, it adds 1 extra - 1-7; 1-8; 1-9; and closing to the end it removes one by one... – arthur Jun 14 '10 at 13:36
  • 1
    @arthur: use second for, it works like a 1-10 if you are on page 5, and if you click next you are going to page 6 and have 2-11, on page 7 you have 3-12. And you can accept question by clicking on mark under the votes number on left side of my post. – Svisstack Jun 14 '10 at 13:45
  • I did so, thanks for that, but I was just wondering how to make it show 10 pages at the beginning(on the 1st page), 10 pages in the middle( on the 6th or 7th page) and 10 pages on the last page.. thanks anyway! – arthur Jun 14 '10 at 14:14
4

I've just been looking for an answer to the same original question, and couldn't find it, so this is what I came up with. I hope someone else finds it useful.

$totalPages  = 20;
$currentPage = 1;

if ($totalPages <= 10) {
    $start = 1;
    $end   = $totalPages;
} else {
    $start = max(1, ($currentPage - 4));
    $end   = min($totalPages, ($currentPage + 5));

    if ($start === 1) {
        $end = 10;
    } elseif ($end === $totalPages) {
        $start = ($totalPages - 9);
    }
}

for ($page = $start; $page <= $end; $page++) {
    echo '[' . $page . ']';
}

Results:

$currentPage = 1;  // [1][2][3][4][5][6][7][8][9][10]
$currentPage = 4;  // [1][2][3][4][5][6][7][8][9][10]
$currentPage = 10; // [6][7][8][9][10][11][12][13][14][15]
$currentPage = 17; // [11][12][13][14][15][16][17][18][19][20]
$currentPage = 20; // [11][12][13][14][15][16][17][18][19][20] 
Will
  • 61
  • 4
-1
$page = 3;
$totalPages = 33;
$count = 9;
$startPage = max(1, $page - $count);
$endPage = min( $totalPages, $page + $count);

if($page-1 > 0){
    echo '<a class="btn btn-default" href="/search-results?page="'.($page-
1).'"><< Prev</a>';
} 

for($i = $startPage; $i < $endPage; $i++): if($i <= $totalPages):

    echo '<a class="btn btn-<?=$i == $page || $i == 1 && $page == "" ? 
    'success' : 'primary';?>"style="margin-right:2px;" href="/search-
    results?page="'.$i.'">'.$i.'</a>';

endif; endfor;
if($page < $totalPages){
    echo '<a class="btn btn-default" href="/search-results?page="'.
    ($page+1).'">Next >></a>';
}
Awais Mustafa
  • 173
  • 2
  • 9