pagination - 10 pages per page

Posted by arthur on Stack Overflow See other posts from Stack Overflow or by arthur
Published on 2010-06-14T11:43:01Z Indexed on 2010/06/14 11:52 UTC
Read the original article Hit count: 202

Filed under:
|

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 hr_ef="?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
    if(($page) == $i)
    {
        $pagination .= $i;
    }
    else
    {
        $pagination .= '< a hr_ef="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;
?> 

© Stack Overflow or respective owner

Related posts about php

Related posts about pagination