this demo might help. I’ve included the sql to create the table the demo uses.
index.php
<?php
session_start();
//----------------------------------------------------------------------------------------------------------------------
//connect to the database
$DBUserName = ""; //database user account name
$DBPassword = ""; //database user account password
$DBName = ""; //name of database
@$conn = mysql_connect("localhost",$DBUserName,$DBPassword) or die('<br />1-Cannot connect to the database at the moment.<br /><br />Please try again later.<br />'); //connect to mysql
@$isDbSelected = mysql_select_db($DBName, $conn) or die('<br />1-Cannot connect to the database at the moment.<br /><br />Please try again later.<br />'); //connect to the db
//----------------------------------------------------------------------------------------------------------------------
include('paginator.php');
if(!isset($_SESSION['pageMaker'])) {
$linesPerPage = 3; //number of lines to print per page
$numLinksDisplay = 5; //number of page links to display at a time
$dbTable = 'tblperson'; //database table to page results
$tableCols = array('*'); //array of column names of records to be retrieved
$pageMaker = new paginator($linesPerPage,$numLinksDisplay,$dbTable,$tableCols,$conn);
} else {
$pageMaker = unserialize($_SESSION['pageMaker']);
}
//set current page
if(isset($_GET['type'])) $pageMaker->setCurrPage($_GET['type'],$_GET['txtPgNum']);
//------------------------------------------------------------
//Code to retrieve the rows to display on the current page
//------------------------------------------------------------
if(!$rs = $pageMaker->getPageRecords($conn)) die('<p>**ERROR - cannot get records for the page at the moment.</p>');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Pagination</title>
<!-- add the stylesheet for the page links -->
<?php echo $pageMaker->getLinkStyles(); ?>
</head>
<body>
<!-- Display the page links -->
<div id="page_links_wrapper">
<?php $pageMaker->showLinks(); ?>
</div>
<!-- Code block to display the DB records for this page-->
<table>
<?php
while($row=mysql_fetch_assoc($rs)) {
echo '<tr><td>'.$row['fldPersonID'].'</td><td>'.$row['fldGivenName'].'</td><td>'.$row['fldFamilyName'].'</td></tr>';
}
mysql_free_result($rs);
mysql_close($conn);
?>
</table>
<!-- End of code block to display the DB records for this page-->
<?php
//hide or display the 'previous' and 'next' buttons as required
$pageMaker->applyLinkStyles();
//serialise the session's pageMaker object for the next call to this page
$_SESSION['pageMaker'] = serialize($pageMaker);
?>
</body>
</html>
paginator.php
<?php
class paginator {
// Properties
protected $linesPerPage; //number of lines to print per page
protected $numLinksDisplay; //number of page links to display at a time
protected $colNames = array(); //array of column names
protected $table; //database table to get records from
protected $currPage;
protected $totRows;
protected $totPages;
protected $offset;
/* * *********************************************************************
Class Constructor
* ********************************************************************* */
public function __construct($numLines, $numLinks, $dbTable, $tableCols, $conn) {
$this->linesPerPage = ceil($numLines);
$this->numLinksDisplay = ceil($numLinks);
$this->table = $dbTable;
$this->colNames = (in_array('*', $tableCols)) ? array('*') : $tableCols;
$this->currPage = 1;
$this->offset = 0;
$this->initialise($conn);
}
/* * *********************************************************************
Class Accessor Methods
* ********************************************************************* */
public function setCurrPage($type, $pageNum) {
$pageNum = ceil($pageNum);
//check which link was clicked
switch ($type) {
case 'pageLink':
$this->currPage = $pageNum;
break;
case 'prevNext':
$this->currPage = $this->currPage + $pageNum;
if ($this->currPage < 1)
$this->currPage = 1;
if ($this->currPage > $this->totPages)
$this->currPage = $this->totPages;
break;
case 'firstPg':
$this->currPage = 1;
break;
case 'lastPg':
$this->currPage = $this->totPages;
break;
}
//calculate the offset of the first record number to retrieve from the DB for this page
$this->offset = ($this->currPage * $this->linesPerPage) - $this->linesPerPage;
if ($this->offset < 0)
$this->offset = 0;
if ($this->offset > $this->totRows)
$this->offset = $this->totRows;
//echo '<br /><br />type = '.$type,'<br />Page request = '.$pageNum.'<br />Current page = '.$this->currPage.'<br />Offset = '.$this->offset.'<br /><br />';
}
//-----------------------------------------------
public function getCurrPage() {
return $this->currPage;
}
/* * *********************************************************************
Class Methods
* ********************************************************************* */
private function initialise($conn) {
//count all the records to work out max number of rows and number of pages needed
$query = 'select count(*) "numRows" from ' . $this->table;
$rs = @mysql_query($query, $conn) or die("<p>3-Server is busy.<br />Please try again later.</p>");
$row = mysql_fetch_assoc($rs);
$this->totRows = $row['numRows']; //total number of rows to display
@mysql_free_result($rs);
if ($this->totRows % $this->linesPerPage == 0) {
$this->totPages = $this->totRows / $this->linesPerPage;
} else {
$this->totPages = round(($this->totRows / $this->linesPerPage) + 0.5); //total number of pages required
}
$this->numLinksDisplay = ($this->numLinksDisplay > $this->totPages) ? $this->totPages : $this->numLinksDisplay;
//echo $this->totRows.'<br />'.$this->totPages.'<br />'.$this->numLinksDisplay;
}
//-------------------------------------------------------------------------------------
public function getPageRecords($conn) {
//build string of column names for the query
$str = '';
foreach ($this->colNames as $val) {
$str .= $val . ',';
}
$str = substr_replace($str, '', strlen($str) - 1); //remove the last comma
$query = 'select ' . $str . ' from ' . $this->table . ' limit ' . $this->offset . ',' . $this->linesPerPage;
//echo '<br />query = '.$query,'<br />';
return @mysql_query($query, $conn);
}
//-------------------------------------------------------------------------------------
public function showLinks() {
if ($this->totPages <= 1)
return; //no need to display any links
echo '<div id="page_links_summ">' .
'<p id="totPages">Total pages: ' . $this->totPages . '</p>' .
'<p id="totRows">Total records: ' . $this->totRows . '</p>' .
'</div>' .
'<div id="page-links-container">' .
'<ul id="page-links">' .
'<li id="liFirstPage"><a href="' . $_SERVER['PHP_SELF'] . '?txtPgNum=-1&type=firstPg" title="Click to view first page">First</a></li>' .
'<li id="liPrevPage"><a href="' . $_SERVER['PHP_SELF'] . '?txtPgNum=-1&type=prevNext" title="Click to view previous page">Previous</a></li>';
//calculate the min and max link numbers to display for this page
if ($this->numLinksDisplay % 2 == 0) { //even number of links to display
$limit1 = $this->currPage - $this->numLinksDisplay / 2;
$limit2 = $this->currPage + ($this->numLinksDisplay / 2) - 1;
} else { //odd number of links to display
$limit1 = $this->currPage - ($this->numLinksDisplay - 1) / 2;
$limit2 = $this->currPage + ($this->numLinksDisplay - 1) / 2;
}
if ($limit1 < 1 && $this->currPage < $this->numLinksDisplay)
$limit1 = 1;
if ($limit2 > $this->totPages)
$limit2 = $this->totPages;
//adjust the link numbers for when we are within $_SESSION['numLinksDisplay']/2 of either end
if ($this->currPage <= $this->numLinksDisplay / 2)
$limit2 = $this->numLinksDisplay;
if ($this->currPage > $this->totPages - $this->numLinksDisplay / 2)
$limit1 = $this->totPages - $this->numLinksDisplay + 1;
//echo '<br />'.$_SESSION['currPage'].'<br />'.$limit1.'<br />'.$limit2.'<br /><br />';
//display the page links
for ($i = $limit1; $i <= $limit2; $i = $i + 1) {
echo '<li id="liPg' . $i . '"><a href="' . $_SERVER['PHP_SELF'] . '?txtPgNum=' . $i . '&type=pageLink" title="Go to page ' . $i . '">' . $i . '</a></li>';
}
echo '<li id="liNextPage"><a href="' . $_SERVER['PHP_SELF'] . '?txtPgNum=1&type=prevNext" title="Click to view next page">Next</a></li>' .
'<li id="liLastPage"><a href="' . $_SERVER['PHP_SELF'] . '?txtPgNum=-1&type=lastPg" title="Click to view last page">Last</a></li>' .
'</ul>' .
'</div>';
}
//-------------------------------------------------------------------------------------
public function getLinkStyles() {
if ($this->totPages <= 1)
return; //no need to display any links
$str = '<style type="text/css"> ' .
'#page-links-container { ' .
'font-size: 10pt; ' .
'font-family: tahoma, arial, sans serif; ' .
'margin: 0px 0px 0px 0px; ' .
'padding: 0px 0px 0px 0px} ' .
'#page-links-container ul { ' .
'clear: both; ' .
'padding: 0px 0px 0px 0px; ' .
'margin: 0px 0px 20px 0px;' .
'list-style-type: none} ' .
'#page-links-container ul li { ' .
'display: inline; ' .
'color: rgb(0,0,205); ' .
'padding: 3px 4px 3px 4px; ' .
'margin: 0px 0px 0px 6px} ' .
'#page-links-container ul li a { ' .
'text-decoration: none; ' .
'font-size: 10pt} ' .
'#page-links-container ul li a:hover { ' .
'text-decoration: underline} ' .
'#page-links-container ul li a:visited { ' .
'color: rgb(0,0,205);} ' .
'#page_links_summ { ' .
'font-size: 10pt; ' .
'font-family: tahoma, arial, sans serif; ' .
'width: 250px} ' .
'#totPages { ' .
'float: left} ' .
'#totRows { ' .
'float: right} ' .
'</style> ';
return $str;
}
//--------------------------------------------------------------------------------------
public function applyLinkStyles() {
if ($this->totPages <= 1)
return; //no need to display any links
//hide or display the 'previous' and 'next' buttons as required
if ($this->totPages == 1)
echo '<script type="text/javascript">document.getElementById("page-links-container").style.display="none";</script>';
if ($this->currPage == 1) {
echo '<script type="text/javascript">document.getElementById("liPrevPage").disabled = true;</script>';
} else {
echo '<script type="text/javascript">document.getElementById("liPrevPage").disabled = false;</script>';
}
if ($this->currPage == $this->totPages) {
echo '<script type="text/javascript">document.getElementById("liNextPage").disabled = true;</script>';
} else {
echo '<script type="text/javascript">document.getElementById("liNextPage").disabled = false;</script>';
}
//highlight the current page's page link
echo '<script type="text/javascript">document.getElementById("liPg' . $this->currPage . '").style.backgroundColor="rgb(200,200,200)";</script>';
echo '<script type="text/javascript">document.getElementById("liPg' . $this->currPage . '").style.border="1px solid rgb(0,0,0)";</script>';
}
//--------------------------------------------------------------------------------------
}
//end of class
?>
sql script to create the db table
CREATE TABLE `tblperson` (
`fldPersonID` int(11) NOT NULL AUTO_INCREMENT,
`fldFamilyName` varchar(20) DEFAULT NULL,
`fldGivenName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`fldPersonID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
/*Data for the table `tblperson` */
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Sui','Steven');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Mary');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Sam');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Malik','Moore');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','fred');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('rubble','barney');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','wilma');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','pebbles');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('smart','maxwell');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Sui','Steven');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Mary');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Sam');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Malik','Moore');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','fred');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('rubble','barney');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','wilma');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','pebbles');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('smart','maxwell');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Sui','Steven');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Mary');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Sam');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Malik','Moore');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','fred');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('rubble','barney');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','wilma');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','pebbles');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('smart','maxwell');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Sui','Steven');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Mary');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Student','Sam');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Malik','Moore');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','fred');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('rubble','barney');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','wilma');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('flinstone','pebbles');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('smart','maxwell');
insert into `tblperson`(`fldFamilyName`,`fldGivenName`) values ('Soo','Malinda');