How to use pagination in php
DESCRIPTION
--------------
This set contains 3 different pagination functions that can be easily implemented
into any other script to paginate the output results retrieved from MySQL database
or elsewhere. Note that these functions do not query the DB by theirselves, you
should previously have counted the total number of matches and retrieved the
appropriate results, specified number of results per page etc.
USAGE
---------
1. Upload the appropriate function script to the root folder on your server.
2. Open demo.php in any text editor to find out the inclusion code for the
appropriate function.
3. Paste the code into your scripts. Note that if you use other variable names
in your scripts, you will have to provide those names in function calling
code instead the specified ones.
4. Feel free to link to paginate.css file from your scripts to stylize the links.
Start Coding
Start Coding
1.demo.php
<?php
//pagination example code
$adjacents = intval($_GET['adjacents']);
if(!$page) $page = 1;
if(!$adjacents) $adjacents = 4;
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&adjacents=" . $adjacents;
?>
<html>
<head>
<title>Pagination Example</title>
<link rel="stylesheet" type="text/css" href="paginate.css">
</head>
<body>
<div align="center">
<p><b>Pagination DEMO</b></p>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="get">
<table cellspacing="0" cellpadding="4">
<tr><th>Specify values for pagination test:</th></tr>
<tr><td>total pages: <input type="text" name="tpages" size="2" value="<?=$tpages;?>">,
adjacents: <input type="text" name="adjacents" size="2" value="<?=$adjacents;?>">
<input type="submit" value="Go"></td></tr>
</table>
</form>
<br>
<p><b>View Results for page <?=$page;?> of <?=$tpages;?> (adjacent pages: <?=$adjacents;?>)</b></p>
<p>paginate_one:</p>
<?php
include("pagination1.php");
echo paginate_one($reload, $page, $tpages);
?>
<br>
<p>paginate_two:</p>
<?php
include("pagination2.php");
echo paginate_two($reload, $page, $tpages, $adjacents);
?>
<br>
<p>paginate_three:</p>
<?php
include("pagination3.php");
echo paginate_three($reload, $page, $tpages, $adjacents);
?>
</div>
</body>
</html>
2.Attaching stylesheet pagination.css in demo.php
/* pagination style */
.pagin {
padding: 2px 0;
margin: 0;
font-family: "Verdana", sans-serif;
font-size: 7pt;
font-weight: bold;
}
.pagin * {
padding: 2px 6px;
margin: 0;
}
.pagin a {
border: solid 1px #666666;
background-color: #EFEFEF;
color: #666666;
text-decoration: none;
}
.pagin a:visited {
border: solid 1px #666666;
background-color: #EFEFEF;
color: #60606F;
text-decoration: none;
}
.pagin a:hover, .pagin a:active {
border: solid 1px #CC0000;
background-color: white;
color: #CC0000;
text-decoration: none;
}
.pagin span {
cursor: default;
border: solid 1px #808080;
background-color: #F0F0F0;
color: #B0B0B0;
}
.pagin span.current {
border: solid 1px #666666;
background-color: #666666;
color: white;
}
<?php
/*************************************************************************
php easy :: pagination scripts set - Version One
==========================================================================
********************/
function paginate_one($reload, $page, $tpages) {
$firstlabel = "First";
$prevlabel = "Prev";
$nextlabel = "Next";
$lastlabel = "Last";
$out = "<div class=\"pagin\">\n";
// first
if($page>1) {
$out.= "<a href=\"" . $reload . "\">" . $firstlabel . "</a>\n";
}
else {
$out.= "<span>" . $firstlabel . "</span>\n";
}
// previous
if($page==1) {
$out.= "<span>" . $prevlabel . "</span>\n";
}
elseif($page==2) {
$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
}
// current
$out.= "<span class=\"current\">Page " . $page . " of " . $tpages . "</span>\n";
// next
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" .($page+1) . "\">" . $nextlabel . "</a>\n";
}
else {
$out.= "<span>" . $nextlabel . "</span>\n";
}
// last
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $lastlabel . "</a>\n";
}
else {
$out.= "<span>" . $lastlabel . "</span>\n";
}
$out.= "</div>";
return $out;
}
?>
<?php
/*************************************************************************
php easy :: pagination scripts set - Version Two
==========================================================================
*************************************************************************/
function paginate_two($reload, $page, $tpages, $adjacents) {
$firstlabel = "« ";
$prevlabel = "‹ ";
$nextlabel = " ›";
$lastlabel = " »";
$out = "<div class=\"pagin\">\n";
// first
if($page>($adjacents+1)) {
$out.= "<a href=\"" . $reload . "\">" . $firstlabel . "</a>\n";
}
else {
$out.= "<span>" . $firstlabel . "</span>\n";
}
// previous
if($page==1) {
$out.= "<span>" . $prevlabel . "</span>\n";
}
elseif($page==2) {
$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
}
// 1 2 3 4 etc
$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
for($i=$pmin; $i<=$pmax; $i++) {
if($i==$page) {
$out.= "<span class=\"current\">" . $i . "</span>\n";
}
elseif($i==1) {
$out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . $i . "\">" . $i . "</a>\n";
}
}
// next
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" .($page+1) . "\">" . $nextlabel . "</a>\n";
}
else {
$out.= "<span>" . $nextlabel . "</span>\n";
}
// last
if($page<($tpages-$adjacents)) {
$out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $lastlabel . "</a>\n";
}
else {
$out.= "<span>" . $lastlabel . "</span>\n";
}
$out.= "</div>";
return $out;
}
?>
<?php
/*************************************************************************
php easy :: pagination scripts set - Version Three
==========================================================================
*************************************************************************/
function paginate_three($reload, $page, $tpages, $adjacents) {
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "<div class=\"pagin\">\n";
// previous
if($page==1) {
$out.= "<span>" . $prevlabel . "</span>\n";
}
elseif($page==2) {
$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
}
// first
if($page>($adjacents+1)) {
$out.= "<a href=\"" . $reload . "\">1</a>\n";
}
// interval
if($page>($adjacents+2)) {
$out.= "...\n";
}
// pages
$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
for($i=$pmin; $i<=$pmax; $i++) {
if($i==$page) {
$out.= "<span class=\"current\">" . $i . "</span>\n";
}
elseif($i==1) {
$out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . $i . "\">" . $i . "</a>\n";
}
}
// interval
if($page<($tpages-$adjacents-1)) {
$out.= "...\n";
}
// last
if($page<($tpages-$adjacents)) {
$out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
}
else {
$out.= "<span>" . $nextlabel . "</span>\n";
}
$out.= "</div>";
return $out;
}
?>
--------------
This set contains 3 different pagination functions that can be easily implemented
into any other script to paginate the output results retrieved from MySQL database
or elsewhere. Note that these functions do not query the DB by theirselves, you
should previously have counted the total number of matches and retrieved the
appropriate results, specified number of results per page etc.
USAGE
---------
1. Upload the appropriate function script to the root folder on your server.
2. Open demo.php in any text editor to find out the inclusion code for the
appropriate function.
3. Paste the code into your scripts. Note that if you use other variable names
in your scripts, you will have to provide those names in function calling
code instead the specified ones.
4. Feel free to link to paginate.css file from your scripts to stylize the links.
Start Coding
Start Coding
1.demo.php
<?php
//pagination example code
$adjacents = intval($_GET['adjacents']);
if(!$page) $page = 1;
if(!$adjacents) $adjacents = 4;
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&adjacents=" . $adjacents;
?>
<html>
<head>
<title>Pagination Example</title>
<link rel="stylesheet" type="text/css" href="paginate.css">
</head>
<body>
<div align="center">
<p><b>Pagination DEMO</b></p>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="get">
<table cellspacing="0" cellpadding="4">
<tr><th>Specify values for pagination test:</th></tr>
<tr><td>total pages: <input type="text" name="tpages" size="2" value="<?=$tpages;?>">,
adjacents: <input type="text" name="adjacents" size="2" value="<?=$adjacents;?>">
<input type="submit" value="Go"></td></tr>
</table>
</form>
<br>
<p><b>View Results for page <?=$page;?> of <?=$tpages;?> (adjacent pages: <?=$adjacents;?>)</b></p>
<p>paginate_one:</p>
<?php
include("pagination1.php");
echo paginate_one($reload, $page, $tpages);
?>
<br>
<p>paginate_two:</p>
<?php
include("pagination2.php");
echo paginate_two($reload, $page, $tpages, $adjacents);
?>
<br>
<p>paginate_three:</p>
<?php
include("pagination3.php");
echo paginate_three($reload, $page, $tpages, $adjacents);
?>
</div>
</body>
</html>
2.Attaching stylesheet pagination.css in demo.php
/* pagination style */
.pagin {
padding: 2px 0;
margin: 0;
font-family: "Verdana", sans-serif;
font-size: 7pt;
font-weight: bold;
}
.pagin * {
padding: 2px 6px;
margin: 0;
}
.pagin a {
border: solid 1px #666666;
background-color: #EFEFEF;
color: #666666;
text-decoration: none;
}
.pagin a:visited {
border: solid 1px #666666;
background-color: #EFEFEF;
color: #60606F;
text-decoration: none;
}
.pagin a:hover, .pagin a:active {
border: solid 1px #CC0000;
background-color: white;
color: #CC0000;
text-decoration: none;
}
.pagin span {
cursor: default;
border: solid 1px #808080;
background-color: #F0F0F0;
color: #B0B0B0;
}
.pagin span.current {
border: solid 1px #666666;
background-color: #666666;
color: white;
}
<?php
/*************************************************************************
php easy :: pagination scripts set - Version One
==========================================================================
********************/
function paginate_one($reload, $page, $tpages) {
$firstlabel = "First";
$prevlabel = "Prev";
$nextlabel = "Next";
$lastlabel = "Last";
$out = "<div class=\"pagin\">\n";
// first
if($page>1) {
$out.= "<a href=\"" . $reload . "\">" . $firstlabel . "</a>\n";
}
else {
$out.= "<span>" . $firstlabel . "</span>\n";
}
// previous
if($page==1) {
$out.= "<span>" . $prevlabel . "</span>\n";
}
elseif($page==2) {
$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
}
// current
$out.= "<span class=\"current\">Page " . $page . " of " . $tpages . "</span>\n";
// next
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" .($page+1) . "\">" . $nextlabel . "</a>\n";
}
else {
$out.= "<span>" . $nextlabel . "</span>\n";
}
// last
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $lastlabel . "</a>\n";
}
else {
$out.= "<span>" . $lastlabel . "</span>\n";
}
$out.= "</div>";
return $out;
}
?>
<?php
/*************************************************************************
php easy :: pagination scripts set - Version Two
==========================================================================
*************************************************************************/
function paginate_two($reload, $page, $tpages, $adjacents) {
$firstlabel = "« ";
$prevlabel = "‹ ";
$nextlabel = " ›";
$lastlabel = " »";
$out = "<div class=\"pagin\">\n";
// first
if($page>($adjacents+1)) {
$out.= "<a href=\"" . $reload . "\">" . $firstlabel . "</a>\n";
}
else {
$out.= "<span>" . $firstlabel . "</span>\n";
}
// previous
if($page==1) {
$out.= "<span>" . $prevlabel . "</span>\n";
}
elseif($page==2) {
$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
}
// 1 2 3 4 etc
$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
for($i=$pmin; $i<=$pmax; $i++) {
if($i==$page) {
$out.= "<span class=\"current\">" . $i . "</span>\n";
}
elseif($i==1) {
$out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . $i . "\">" . $i . "</a>\n";
}
}
// next
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" .($page+1) . "\">" . $nextlabel . "</a>\n";
}
else {
$out.= "<span>" . $nextlabel . "</span>\n";
}
// last
if($page<($tpages-$adjacents)) {
$out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $lastlabel . "</a>\n";
}
else {
$out.= "<span>" . $lastlabel . "</span>\n";
}
$out.= "</div>";
return $out;
}
?>
<?php
/*************************************************************************
php easy :: pagination scripts set - Version Three
==========================================================================
*************************************************************************/
function paginate_three($reload, $page, $tpages, $adjacents) {
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "<div class=\"pagin\">\n";
// previous
if($page==1) {
$out.= "<span>" . $prevlabel . "</span>\n";
}
elseif($page==2) {
$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
}
// first
if($page>($adjacents+1)) {
$out.= "<a href=\"" . $reload . "\">1</a>\n";
}
// interval
if($page>($adjacents+2)) {
$out.= "...\n";
}
// pages
$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
for($i=$pmin; $i<=$pmax; $i++) {
if($i==$page) {
$out.= "<span class=\"current\">" . $i . "</span>\n";
}
elseif($i==1) {
$out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
}
else {
$out.= "<a href=\"" . $reload . "&page=" . $i . "\">" . $i . "</a>\n";
}
}
// interval
if($page<($tpages-$adjacents-1)) {
$out.= "...\n";
}
// last
if($page<($tpages-$adjacents)) {
$out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if($page<$tpages) {
$out.= "<a href=\"" . $reload . "&page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
}
else {
$out.= "<span>" . $nextlabel . "</span>\n";
}
$out.= "</div>";
return $out;
}
?>
0 Response to "How to use pagination in php"
Post a Comment