Click to See Complete Forum and Search --> : Pagination Coding Logic


aj_nsc
02-04-2007, 08:00 PM
I am creating a guestbook script and I am curious about the programming logic used in pagination of entries. I am using a database and I have 3 options presented to the viewer, select page, select entries to show per page, and select in ascending or descending order of date. I found that when I am making up the links of pages per entry, I also have to take into account how many entries there are and the current page the user is viewing...

i.e. if a user is viewing 10 entries per page on the 3rd page, I'd have to do some complicated (but do-able) math to determine what page they would be on if they selected to view 25 entries per page.

I am just wondering if there is an easy method to create these kinds of links in something like a forum or what the standard methodology of pagination is.

Thanks.

NightShift58
02-04-2007, 09:21 PM
This is an older script that still seems to work. It include a pagination logic based on four pagination elements: Jump to First Page, Previous Page, Next Page and Last Page.

It also includes the output section, which you may want to modify or replace with your own.

Here goes:<?
// Script expects 2 GET parameters: 'pp' and 'pn'
// pp: rows per page
// pn: page no. to display
// Other settings:
// $TBL: set below to the table name to display
// $DEFperPAGE: set below as a safety in case user removes 'pp' from URL
// $MAXperPAGE: set below as a safety in case user changes 'pp'to a value greater than acceptable to you
// $ALTcolorx: set below to alternate row background colors

require_once "YourDBconnect.inc.php";
$TBL = "YourTableName";
$DEFperPAGE = 20;
$MAXperPAGE = 50;
$ALTcolor1 = "#FFFFFF";
$ALTcolor2 = "#e5e5e5";

//----------------------------------------
$sql = "select count(*) from `$TBL`";
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());

$recROW = mysql_fetch_row($qry);
$recCOUNT = $recROW[0];

IF ($recCOUNT == 0) :
print "<center>Sorry, there is currently no data available. Please try again later...<center>";
ELSE :
$perPAGE = (isset($_GET['pp']) AND intval($_GET['pp']) <> 0) ? intval($_GET['pp']) : $DEFperPAGE;
$perPAGE = ($perPAGE < 1) ? $DEFperPAGE : $perPAGE;
$perPAGE = ($perPAGE > $MAXperPAGE) ? $DEFperPAGE : $perPAGE;

$thisPAGENO = (isset($_GET['pn']) AND intval($_GET['pn'])) <> 0 ? intval($_GET['pn']) : 0;
$thisMAXPG = intval($recCOUNT / $perPAGE) + (($recCOUNT % $perPAGE) ? 1 : 0);
$thisPAGENO = (($thisPAGENO > $thisMAXPG) ? $thisMAXPG : (($thisPAGENO < 1) ? 1 : $thisPAGENO));

$thisFRSTlnk = ($thisPAGENO == 1) ? "" : "<a href='" . $_SERVER['PHP_SELF'] . "?pn=1&pp=" . $perPAGE . "'>First Page</a>";
$thisLASTlnk = ($thisPAGENO == $thisMAXPG) ? "" : "<a href='" . $_SERVER['PHP_SELF'] . "?pn=" . $thisMAXPG . "&pp=" . $perPAGE . "'>Last Page</a>";

IF ($thisPAGENO == 1) :
$thisPREVlnk = "";
ELSE :
$thisPREVPG = $thisPAGENO - 1;
$thisPREVlnk = "<a href='" . $_SERVER['PHP_SELF'] . "?pn=" . $thisPREVPG . "&pp=" . $perPAGE . "'>Previous Page</a>";
ENDIF;

$thisNEXTPG = $thisPAGENO + 1;
IF ($thisNEXTPG > $thisMAXPG) :
$thisNEXTlnk = "";
ELSE :
$thisNEXTlnk = $_SERVER['PHP_SELF'] . "?pn=" . $thisNEXTPG;
$thisNEXTlnk = "<a href='" . $_SERVER['PHP_SELF'] . "?pn=" . $thisNEXTPG . "&pp=" . $perPAGE . "'>Next Page</a>";
ENDIF;

$thisFROM = (($thisPAGENO - 1) * $perPAGE) + 1;
$sql = "SELECT * FROM `$TBL` LIMIT $thisFROM, $perPAGE";
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());

//---------------------- DISPLAY -------------------------
print "<h2 style='text-align:center;'>TITLE BLAH-BLAH-BLAH</h2>\n";
print "<p>\n";
print "<table style='width:70%;' align='center'>\n";
print "<tr style='background-color:silver;'><td>&nbsp;</td><td><b>HEADING1</b></td><td><b>HEADING2</b></td><td><b>HEADING3</b></td></tr>\n";
WHILE ($row = mysql_fetch_row($qry)) :
$bgcolor = "style='background-color:" . (($thisFROM++ % 2) ? $ALTcolor1 : $ALTcolor2) . ";'";
print "<tr $bgcolor>\n";
print " <td align=right>$thisFROM.&nbsp;&nbsp;</td>\n";
print " <td>" .$row[0]. "</td>\n";
print " <td>" .$row[1]. "</td>\n";
print " <td>" .$row[2]. "</td>\n";
print "</tr>\n";
ENDWHILE;
print "</table>\n";

print "<p>";
print "<table style='width:70%;' align='center'>\n";
print "<tr style='background-color:silver;'>";
print "<td width='25%' align='left'>&nbsp; $thisFRSTlnk</td>";
print "<td width='25%' align='left'>&nbsp; $thisPREVlnk</td>";
print "<td width='25%' align='right'>$thisNEXTlnk &nbsp;</td>";
print "<td width='25%' align='right'>$thisLASTlnk &nbsp;</td>";
print "</tr>";
print "<tr><td colspan='4' align='center'>(Page <b>$thisPAGENO</b> of <b>$thisMAXPG</b>)</td></tr>";
print "</table>";
ENDIF;
?>

bokeh
02-05-2007, 06:48 AM
I am just wondering if there is an easy method to create these kinds of linksfunction pagination_links($page, $num_rows, $results_per_page, $each_direction = 5, $fieldname = 'page')
{
/*******************************************************

FUNCTION NAME:
pagination_links

PURPOSE:
To print pagination links

DESCRIPTION:
string pagination_links( int page, int num_rows, int results_per_page [, int each_direction [, string fieldname ]] )

INPUTS:
$page (int) represents the current page number.
$num_rows (int) is the total number of items that need to be paginated.
$results_per_page (int) is the number of items to display per page.
$each_direction (int, optional, defaults to 5) is the number of pages
to link to above and below the current page.
$fieldname (string, optional, defaults to "page") is the $_GET variable
used to denote which page we are currently viewing,
(http://site.com/file.php?page=5)

RETURN VALUE:
A string of formated HTML mark-up containing the pagination links.

*********************************************************/

$word_for_previous = 'prev';
$word_for_next = 'next';
$total_pages = $num_rows ? ceil($num_rows / $results_per_page) : 1 ;
if($total_pages < 2)
{
return null;
}
$page = ((is_numeric($page)) and ($page >= 1) and ($page <= $total_pages)) ? (int)$page : 1 ;
$output = null;
$query_string = '';
foreach($_GET as $k => $v)
{
if($k != $fieldname)
{
$query_string .= get_magic_quotes_gpc() ?
urlencode(stripslashes($k)).'='.urlencode(stripslashes($v)).'&amp;':
urlencode($k).'='.urlencode($v).'&amp;';
}
}
if($page > 1)
{
$output .= '<a href="'.htmlentities($_SERVER['PHP_SELF']).'?'.$query_string.$fieldname.'='.($page - 1).'">'.$word_for_previous.'</a>'."\n | \n";
}
for($i = $page - $each_direction; $i <= $page + $each_direction; $i++)
{
if(($i > 0) and ($i <= $total_pages))
{
$output .= isset($spacer) ? $spacer : null ;
$spacer = ' | '."\n";
if($page != $i)
{
$output .= '<a href="'.htmlentities($_SERVER['PHP_SELF']).'?'.$query_string.$fieldname.'='.$i.'">'.$i.'</a>'."\n";
}
else
{
$output .= '<span class="current-page">' . $i . "</span>\n";
}
}
}
if($page < $total_pages)
{
$output .= " | \n".'<a href="'.htmlentities($_SERVER['PHP_SELF']).'?'.$query_string.$fieldname.'='.($page + 1).'">'.$word_for_next.'</a>';
}
return "\n<p class=\"pagination-links\">\n$output\n</p>\n";
}