tags:

views:

102

answers:

3

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;
?> 
A: 

Have you had a look at Zend_Paginator?

Kevin Sedgley
yes, but I need something less complicated. This code i gave works fine, but it doesn't limit displayed pages...
arthur
A: 

If you just want a quick fix, you might try modifying your for loop a little. For example, you won't want to start at 1, and you won't necessarily want to loop while $i <= $total_pages.

Displaying an odd number of pagination links might make more sense: you would display the current page, then four to the left of it, and four to the right. Something like this:

for($i = $page_number - 4; $i <= $page_number + 4; $i++) {

but you would obviously need to do a little bit more to ensure you weren't displaying negative numbers, or displaying more links than there are pages.

Carson Myers
This solution is a buggy, dont work if you are on page 4 or less, and dont work correctly when you are on last 4 pages
Svisstack
hence the last paragraph -- I was merely providing a starting point
Carson Myers
A: 

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
thanks for your quick answer, It was realy helpful.. never thought It would be so fast! :)
arthur
@arthur: then accept my answer;-)
Svisstack
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
@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
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