
////////////////////////////////////////////////////////////////////////
// //
// Pagination V 1.1 //
// //
// A set of pagination functions for your website results pages //
// //
// Copyright The pixelbox homepage.com 13/10/2006 //
// //
// This program is free software; you can redistribute it and/or //
// modify it under the terms of the GNU General Public License //
// as published by the Free Software Foundation; either version 2 //
// of the License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 51 Franklin Street, Fifth Floor, //
// Boston, MA 02110-1301, USA. //
// //
////////////////////////////////////////////////////////////////////////
////////////////////////CHANGELOG///////////////////////////////////////
// //
// version 1.0 released 13/10/2006 //
// //
// version 1.01 released 11/11/2006 //
// fixed bug in pagination 4 when 11 links were presented //
// //
// version 1.1 released 25/1/2007 - current version //
// change function output to variable rather than echo //
// fixed output when total_pages =1; //
// bug fixes for pagination three //
////////////////////////////////////////////////////////////////////////
//
// to use a pagination method call as a function with the total_pages and
// current page as the parameters
//
// e.g. $pagination = pagination_one($total_pages,$page);
//
// you can then echo $pagination wherever you want the pagination to
// appear.
//
// See the index.php file for example usage
//
// An sql dump is provided as part of this download
//
// A link is not required if you use the methods though would be appreciated
//
// <a href="http://www.thepixelboxhomepage.com">the pixelBOX homepage.com</a>
//
//
/////////////////////////////////////////////////////////////////////////START
// the script name;
$webpage = basename($_SERVER['PHP_SELF']);
function pagination_one($total_pages,$page){
global $webpage;
//start_to build the $pagination
$pagination='<div class="page_numbers">
<ul>';
//if there are more than one page of results
if($total_pages!=1){
//loop up to the total_pages of results
for ($i=1;$i<$total_pages+1;$i++){
//if the current page no link
if($i==$page){
$pagination.='<li><a class="current">'.$i.'</a></li>
';
}
//else provide a link
else{
$pagination.='<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li>
';
}
}
}
//if one page of results
else{
$pagination.='<li><a href="" class="current">1</a></li>';
}
//finish and return
$pagination.='</ul>
</div>';
return($pagination);
}
function pagination_two($total_pages,$page){
global $webpage;
$pagination='<div class="page_numbers">
<ul>';
//more than one page of results
if($total_pages!=1){
//if the current page is greater than one then provide link to prev and first
if($page>'1'){
$pagination.='<li class="current"><a href="'.$webpage.'?page=1">First</a></li>
<li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li>
';
}
//loop through to provide the links
for ($i=1;$i<$total_pages+1;$i++){
//if current page no link
if($i==$page){
$pagination.='<li><a class="current">'.$i.'</a></li>
';
}
//else provide a link
else{
$pagination.= '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li>
</li>';
}
}
//if not the last page then provide link to the page
if(($page >='1')&&($page!=$total_pages)){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li>
<li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li>
';
}
}
//if one page of results
else{
$pagination.='<li><a href="" class="current">1</a></li>';
}
//finish and return
$pagination.='</ul>
</div>';
return($pagination);
}
function pagination_three($total_pages,$page){
global $webpage;
//start to build $pagination
$pagination = '<div class="page_numbers">
<ul>';
//if more than one page
if($total_pages!=1){
//first and prev links
if($page>'1'){
$pagination.='<li class="current"><a href="'.$webpage.'?page=1">First</a></li>
<li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li>
';
}
//$maximum_links is the starting maximum links on the page
$maximum_links = 10;
//if less pages than maximum links are to be needed
if($total_pages<=$maximum_links){
//set maximum links to the total pages
// plus 1 for the loop
$maximum_links = $total_pages+1;
}
//if more are needed
else{
//$maximum_links +1 for the loop
$maximum_links=$maximum_links+1;
//if the page is greater than maximum links then extend the loop by one
if($page>=$maximum_links){
$maximum_links=$page+1;
}
}
//loop through
for ($i=1;$i<$maximum_links;$i++){
//if page then no link
if($i==$page){
$pagination.='<li><a class="current">'.$i.'</a></li>
';
}
//else provide a link
else{
$pagination.= '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li>
';
}
}
//next and last links
if(($page >="1")&&($page!=$total_pages)){
$pagination.= '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li>
<li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li>
';
}
}
//if one page of results
else{
$pagination.='<li><a href="" class="current">1</a></li>';
}
//finish and return
$pagination.='</ul>
</div>';
return($pagination);
}
function pagination_four($total_pages,$page){
global $webpage;
$pagination='<div class="page_numbers">
<ul>';
//if more than one page
if($total_pages!=1){
//change these for links per page
//
// $max is the visible links
// $shirt is the page the links start to shift on
//
// e.g. $max-$shirt+1 = shifting page
$max = 10;
$shift = 5;
//used in the loop
$max_links = $max+1;
$h=1;
//if more pages than max links
if($total_pages>=$max_links){
//if page is greater than shifing page and the last page is not there
if(($page>=$max_links-$shift)&&($page<=$total_pages-$shift)){
//set the loop values based on the current page
$max_links = $page+$shift;
$h=$max_links-$max;
}
//if the last link is visible then set the top of the loop to
// the total_pages
// otherwise we get links to pages with no results
if($page>=$total_pages-$shift+1){
$max_links = $total_pages+1;
$h=$max_links-$max;
}
}
//if less pages than max links then set the top of the loop to total pages
else{
$h=1;
$max_links = $total_pages+1;
}
//first and prev buttons
if($page>'1'){
$pagination.= '<li class="current"><a href="'.$webpage.'?page=1">First</a></li>
<li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li>
';
}
//loop through the results;
for ($i=$h;$i<$max_links;$i++){
//if current page no link
if($i==$page){
$pagination.='<li><a class="current">'.$i.'</a></li>
';
}
//else provide a link
else{
$pagination.= '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li>
';
}
}
//next and last buttons
if(($page >='1')&&($page!=$total_pages)){
$pagination.= '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li>
<li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li>
';
}
}
//one page of results
else{
$pagination.='<li><a href="" class="current">1</a></li>';
}
$pagination.='</ul>
</div>';
return($pagination);
}
function pagination_five($total_pages,$page){
global $webpage;
$pagination='<div class="page_numbers">
<ul>';
//if more than one page of results
if($total_pages!=1){
//configure for the starting links per page
$max = 10;
//used in the loop
$max_links = $max+1;
$h=1;
//if page is above max link
if($page>$max_links){
//start of loop
$h=(($h+$page)-$max_links);
}
//if page is not page one
if($page>=1){
//top of the loop extends
$max_links = $max_links+($page-1);
}
//if the top page is visible then reset the top of the loop to the $total_pages
if($max_links>$total_pages){
$max_links=$total_pages+1;
}
//next and prev buttons
if($page>'1'){
$pagination.='<li class="current"><a href="'.$webpage.'?page=1">First</a></li>
<li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li>
';
}
//create the page links
for ($i=$h;$i<$max_links;$i++){
if($i==$page){
$pagination.='<li><a class="current">'.$i.'</a></li>';
}
else{
$pagination.='<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a> </li>';
}
}
//Next and last buttons
if(($page >="1")&&($page!=$total_pages)){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li>
<li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li>
';
}
}
//if one page of results
else{
$pagination.='<li><a href="" class="current">1</a></li>';
}
$pagination.='</ul>
</div>';
return($pagination);
}
function pagination_six($total_pages,$page){
global $webpage;
$pagination = '<div class="page_numbers">
<ul>';
if($total_pages!=1){
//the total links visible
$max_links=10;
//$max links_marker is the top of the loop
//$h is the start
$max_links_marker = $max_links+1;
$h=1;
//$link_block is the block of links on the page
//When this is an integer we need a new block of links
$link_block=(($page-1)/$max_links);
//if the page is greater than the top of th loop and link block
//is an integer
if(($page>=$max_links_marker)&&(is_int($link_block))){
//reset the top of the loop to a new link block
$max_links_marker=$page+$max_links;
//and set the bottom of the loop
$h=$max_links_marker-$max_links;
$prev=$h-1;
}
//if not an integer we are still within a link block
elseif(($page>=$max_links_marker)&&(!is_int($link_block))){
//round up the link block
$round_up=ceil($link_block);
$new_top_link = $round_up*$max_links;
//and set the top of the loop to the top link
$max_links_marker=$new_top_link+1;
//and the bottom of the loop to the top - max links
$h=$max_links_marker-$max_links;
$prev=$h-1;
}
//if greater than total pages then set the top of the loop to
// total_pages
if($max_links_marker>$total_pages){
$max_links_marker=$total_pages+1;
}
//first and prev buttons
if($page>'1'){
$pagination.='<li class="current"><a href="'.$webpage.'?page=1">First</a></li>
<li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li>';
}
//provide a link to the previous block of links
$prev_start = $h-$max_links;
$prev_end = $h-1;
if($prev_start <=1){
$prev_start=1;
}
$prev_block = "Pages $prev_start to $prev_end";
if($page>$max_links){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.$prev.'">'.$prev_block.'</a></li>';
}
//loop through the results
for ($i=$h;$i<$max_links_marker;$i++){
if($i==$page){
$pagination.= '<li><a class="current">'.$i.'</a></li>';
}
else{
$pagination.= '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a></li>';
}
}
//provide a link to the next block o links
$next_start = $max_links_marker;
$next_end = $max_links_marker+$max_links;
if($next_end >=$total_pages){
$next_end=$total_pages;
}
$next_block = "Pages $next_start to $next_end";
if($total_pages>$max_links_marker-1){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.$max_links_marker.'">'.$next_block.'</a></li>';
}
//link to next and last pages
if(($page >="1")&&($page!=$total_pages)){
$pagination.='<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li>
<li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li>';
}
}
//if one page of results
else{
$pagination.='<li><a href="" class="current">1</a></li>';
}
$pagination.='</ul>
</div>';
return($pagination);
}
UPDATE:
Here's an update I received today. A new function from Karl Steltenpohl. I didn't tested it yet but should be working fine. Let me know if you have issues with it.
//---------------------------------------------------------------------------------------
////////////////////////////////////////////
// PAGINATION FUNCTION //
// by: Karl Steltenpohl //
////////////////////////////////////////////
function pagination($table, $order, $searchstring, $pre, $pos, $nav, $page, $pages)
{
///////////////////////
// Get Current Url //
///////////////////////
$webpage = basename($_SERVER['PHP_SELF']);
global $webpage;
////////////////////////
// Sorter and Pagination Query Begin //
/////////////////////////////////////////
//$pre = $_REQUEST['pre'];
//$pos = $_REQUEST['pos'];
//$nav = $_REQUEST['nav'];
//$page = $_REQUEST['page'];
//$pages = $_REQUEST['pages'];
///////////////////////////////////////////
// Set Initial Pre Pos and Page Limits //
///////////////////////////////////////////
if($pre == "" and $pos == "" and $page == "")
{
$pre = 0;
$pos = 9;
$page = 1;
}
///////////////////////////////
// User Navigates Previous //
///////////////////////////////
if($nav == "prev")
{
$pre = ($pre - 10);
$pos = ($pos - 10);
$page = ($page - 1);
}
///////////////////////////
// User Navigates Next //
///////////////////////////
if($nav == "next")
{
$pre = ($pre + 10);
$pos = ($pos + 10);
$page = ($page + 1);
}
/////////////////////////////
// If page number to low //
/////////////////////////////
if($page < 1)
{
$pre = 0;
$pos = 9;
$page = 1;
}
//////////////////////////////
// If page number to high //
//////////////////////////////
if($page > $pages)
{
$pre = 0;
$pos = 9;
$page = 1;
}
//////////////////////////////////////////
// Select for total number or results //
//////////////////////////////////////////
$r = "SELECT DISTINCT * FROM $table $searchstring";
$re = mysql_query($r) or die("error 12547");
$nums = mysql_num_rows($re);
////////////////////////////////////////////
// Select for current displayed results //
////////////////////////////////////////////
$request = "SELECT DISTINCT * FROM $table $searchstring ORDER BY $order DESC LIMIT $pre, 10";
$result = mysql_query($request) or die("error 25352");
$num = mysql_num_rows($result);
///////////////////////////////////////
// Determine total number of pages //
///////////////////////////////////////
$pages = ceil($nums/10);
/////////////////////////////////
// Create Navigation Display //
/////////////////////////////////
$navigation = "
$nums entries on $pages Page(s)<br>
<a href=\"$webpage?page=$page&nav=prev&pre=$pre&pos=$pos&pages=$pages&view=view\">Previous</a> |
Page $page |
<a href=\"$webpage?page=$page&nav=next&pre=$pre&pos=$pos&pages=$pages&view=view\">Next</a><br>
Results $pre - $pos
";
/////////////////////////////////
// Create Paginagtion Array //
/////////////////////////////////
// result is the result of the limited query
$pagination = array($navigation, $result, $num);
/////////////////////////////////
// Return Paginagtion Array //
/////////////////////////////////
return $pagination;
}//end function
//--------------------------------------------------------------------------------------------
Here is an example of how to impliment it
//------------------------------------------------------------------------------------------
//this is the search string to the sql db
$searchstring = "WHERE `something` = 'something'";
//this is the table being searched
$table = "sql db table classifieds";
//this is the table field to order the results by
$order = "sql db orderby field";
// call the function
$pagination = pagination($table, $order, $searchstring, $pre, $pos, $nav, $page, $pages);
//this pulls out the display
$navigation = $pagination[0];
//this pulls out the results
$result = $pagination[1];
//this pulls out the num of results
$num = $pagination[2];
//echo the display onto the site
echo"$navigation";
//now all you need to do is loop the result based on the num
//each page will display 10 results.
Added by roScripts on April-18-2007, 3:53 pm

2007-06-20 | 02:36 pm