Well I'm completely new to PHP but after looking at different paging scripts they all seems overly complicated. Well I wrote my own to maybe help others out (and myself). Maybe there is a simpler script out there but I haven't found it..
Anyways this will display 20 records per page and display first_page, current_page-2, current_page, current_page+2, last_page.. So if you are on page 10 to 30 it will look like this..
[URL=http://]First[/URL] [URL=http://]8[/URL] [URL=http://]9[/URL] [10] [URL=http://]11[/URL] [URL=http://]12[/URL] [URL=http://]Last[/URL]
Let me know what you think since it's my first PHP script.. I hope it helps someone..
<?PHP
function displayPaging($totalpages, $currentpage, $link){
if ($totalpages > 0){
if ($currentpage == 0){$currentpage = 1;} //Sets current page from 0 to 1 for the link and display
$firstpage = "<A href='$link?PAGE=1'>First</A> "; //Writes the link for the first page.
$lastpage = " <A href='$link?PAGE=$totalpages'>Last</A>"; //Writes the link for the Last page.
if ($totalpages <= 5){ //If less or equals 5 then is just loops thru making links.
for ($i = 1; $i <= $totalpages; $i++) {
if ($currentpage == $i){
$RetStr .= "[$i] ";
}else{
$RetStr .= "<A href='$link?PAGE=$i'>$i</A> ";
}
}
}else{
if ($currentpage >= 4){$RetStr .= $firstpage;} //Writes the first page link is you are on page 4 or greater.
$posStart = max(1, $currentpage-2); //Finds the greater number between page 1 and current page-2. Currentpage-2 will display low two pages from current page (meaning if you are on page 6 you loop will start on 4).
$posEnd = min($totalpages, $currentpage+2); //Finds the least number between total pagest and current page +2. Currentpage+2 will display next two pages from current page (meaning if you are on page 6 you loop will end on 8).
if ($currentpage < 3){$posEnd = min(5, $totalpages);} //If page is less then 3 if finds the lower number between 5(total pages per list) and total pages (in case you only have 4 pages).
if (($totalpages - $currentpage) < 3){$posStart = $totalpages - 5;} //If total pages - current page (if you are on 9 or 10 pages) sets the start to total pages - 5(page links per page) so you will still display 5 links on the last page.
for ($i = $posStart; $i <= $posEnd; $i++) { //Loop thru your findings.. Will only loop 5 times but might be 40 to 45 or 5 to 10.
if ($currentpage == $i){
$RetStr .= "[$i] ";
}else{
$RetStr .= "<A href='$link?PAGE=$i'>$i</A> ";
}
}
if ($currentpage <= $totalpages - 3){$RetStr .= $lastpage;} //Writes the link for the last page.
}
}
return $RetStr; //Return you HTML.
}
$db_host="localhost";
$db_user="username";
$db_pass="password";
$db_name="database name";
$table="table name";
$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $db);
$displaytotal = 20; //Records per page.
$page = 1; //Default page = 1.
if (isset($_GET["PAGE"])){$page = $_GET["PAGE"];} //Gets the current page.
if ($page == 1){$page = 0;} //Sets the page to 0 to show record 0 if you are on page 1.
$sql = "SELECT * FROM $table WHERE";
$result = mysql_query($sql, $db);
$recordcount = mysql_numrows($result);
$totalpages = floor(($recordcount-1) / $displaytotal); //Divides the recordcount-1 by the records per page then floors for the lowest even number.
$paging = displayPaging($totalpages, $page,"display.php"); //Calls the paging function sending the total pages, current page and the link.
$sql = "SELECT * FROM $table ORDER BY ID LIMIT ".$page*$displaytotal.",$displaytotal"; //EXAMPLE: Page 2 / 20 records = 40. So start on record 40 and display the next 20 records.
$result = mysql_query($sql, $db);
//Now just loop thru your records (will be 20 cause $displaytotal is set to 20) and echo $paging where you want the pages to be displayed.
?>