Page 1 of 1

Pagination [First][<][1][2][3]...[7][8][9][>][Last] display

Posted: Fri Mar 13, 2009 2:26 pm
by Sindarin

Code: Select all

<?php
 
/* DB CONNECT */
$host = "localhost"; //your sql host, usually 'localhost'
$user = "root"; //username to connect to database
$pass = ""; //password to connect to database
$db = "articles"; //the name of the database
 
mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
mysql_select_db($db) or die("ERROR DB:".mysql_error()); 
 
 
//entries to show per page
$results_per_page = 4;
 
//get current page
$current_page = $_GET['page'];
//if page not set, set it to 1
if(!isset($current_page)){$current_page = 1;}
 
//limit for sql query
$limits = ($current_page - 1) * $results_per_page; 
 
//previous and next pages
$previouspage=$current_page-1;
$nextpage=$current_page+1;
 
 
//return articles
$sql = mysql_query("SELECT * FROM articles LIMIT ".$limits.",$results_per_page") or die(mysql_error());
 
//the total rows in the table
$total_results = mysql_result(mysql_query("SELECT COUNT(article_id) AS tot FROM articles"),0);  
 
//the total number of pages
$total_pages = ceil($total_results / $results_per_page); 
 
//loop and echo
while($row = mysql_fetch_array($sql))
 
{
$get_id = $row['article_id'];
$title = $row['article_title'];
$author = $row['article_content'];
 
echo "<table><tr><td>RESULT: $get_id $title $author</td></tr></table>";
 
}
 
/* PAGINATION navigation */
 
//first page
if ($current_page==1)
{}
else
{
echo " <a href='index.php?page=1'>[ First ]</a> ";
}
 
//previous page link
if ($current_page==1)
{}
else
{
echo "<a href='index.php?page=$previouspage'>[ << ]</a>";
}
 
//pagination number links
for($i = 1; $i <= $total_pages; $i++)
{ 
 
echo "[ <a href='index.php?page=$i'>$i</a> ] ";
 
}
 
//next page link
if ($total_pages==$current_page)
{}
else
{
echo " <a href='index.php?page=$nextpage'>[ >> ]</a> ";
}
 
//final page link
if ($total_pages==$current_page)
{}
else
{
echo " <a href='index.php?page=$total_pages'>[ End ]</a> ";
}
 
//echo on what page we're on
echo "<br/>Page: $current_page of $total_pages";
 
?>
So, this displays:

[First][<<][1][2][3][4][5][6][7][8][9][10][11][>>][Last], how can I make so it "eats" some of the pages in the middle as in,
[First][<<][1][2]..[29][30][ 31 ][32][33]..[155][156][>>][Last] and always display the first 2 pages and 2 before end pages.

Re: Pagination [First][<][1][2][3]...[7][8][9][>][Last] display

Posted: Fri Mar 13, 2009 2:40 pm
by pickle
Well, you know the total number of pages, so divide that by 2, floor() it, then show links to pages x-3, x-2, x-1, x, x+1, x+2, x+3, etc