Pagination in PHP is easy and you can show pagination by following code. There are 4 files (don’t go to the number of files, separate sfiles have been created so that you can easily understand the code), I could have combined it in 1 but 4 seems to be better.
These are: index.php, paging.php, function.php, style.css
index.php
[php]<html>
<head>
<title>Pagination</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Pagination Example</h2>
<?php
$targetpageurl="index.php";
$total_records="100";
$paging_recordsperpg="20";
$paging_linksshow="5";
include "function.php";
include "paging.php";
echo "<div class=’paging’>";
echo "<div class=’paging_displaying’>Displaying Page $paging_currentpg (of $paging_pages) and $paging_recordsshowing Records (of $total_records Total Records)</div>";
echo "<div class=’paging’>$paging_navigation</div>";
echo "<div>";
for($i=$paging_recordstart; $i<=$paging_recordend; $i++)
{
echo "Record $i<br>";
}
echo "</div>";
echo "<div class=’paging’>$paging_navigation</div>";
echo "</div>";
?>
</body>
</html>
[/php]
function.php
[php]<?php
<?php
function paging($mode, &$paging_array)
{
$paging_pgurl=$paging_array[‘paging_pgurl’];
$paging_records=$paging_array[‘paging_records’];
$paging_recordsperpg=$paging_array[‘paging_recordsperpg’];
$paging_linksshow=$paging_array[‘paging_linksshow’];
$paging_currentpg=$paging_array[‘paging_currentpg’];
//$paging_records=$total_records;
//$paging_recordsperpg="20";
$paging_pages=ceil($paging_records/$paging_recordsperpg);
//$paging_linksshow="5";
if($paging_pages<$paging_linksshow)
{
$paging_linksshow=$paging_pages;
}
if($paging_currentpg>$paging_pages)
{
$paging_currentpg="1";
}
$paging_recordstart=(($paging_currentpg-1)*($paging_recordsperpg))+1;
if($paging_recordstart<1)
{
$paging_recordstart="1";
}
$paging_array[‘paging_recordstart’]=$paging_recordstart;
$paging_recordend=$paging_recordstart+$paging_recordsperpg-1;
if($paging_recordend>$paging_records)
{
$paging_recordend=$paging_records;
}
$paging_array[‘paging_recordend’]=$paging_recordend;
$paging_from=$paging_currentpg-ceil($paging_linksshow/2);
if($paging_from<‘1′)
{
$paging_from="1";
}
$paging_to=$paging_currentpg+ceil($paging_linksshow/2);
if($paging_to>$paging_pages)
{
$paging_to=$paging_pages;
}
$paging_prevpg=$paging_currentpg-1;
$paging_nextpg=$paging_currentpg+1;
if($paging_currentpg>’1’)
{
$paging_array[‘paging_navigation’].=" <a href=’?paging_pgnum=1′>First</a> ";
$paging_array[‘paging_navigation’].=" <a href=’?paging_pgnum=$paging_prevpg’>Previous</a> ";
}
else
{
$paging_array[‘paging_navigation’].=" <a>First</a> ";
$paging_array[‘paging_navigation’].=" <a>Previous</a> ";
}
for($i_paging=$paging_from; $i_paging<=$paging_to; $i_paging++)
{
if($i_paging==$paging_currentpg)
{
$paging_array[‘paging_navigation’].=" <b class=’selected’>$i_paging</b> ";
}
else
{
$paging_array[‘paging_navigation’].=" <a href=’?paging_pgnum=$i_paging’>$i_paging</a> ";
}
}
if($paging_currentpg<$paging_pages)
{
$paging_array[‘paging_navigation’].=" <a href=’?paging_pgnum=$paging_nextpg’>Next</a> ";
$paging_array[‘paging_navigation’].=" <a href=’?paging_pgnum=$paging_pages’>Last</a> ";
}
else
{
$paging_array[‘paging_navigation’].=" <a>Next</a> ";
$paging_array[‘paging_navigation’].=" <a>Last</a> ";
}
$paging_recordsshowing=$paging_recordend-$paging_recordstart+1;
$paging_array[‘paging_currentpg’]=$paging_currentpg;
$paging_array[‘paging_pages’]=$paging_pages;
$paging_array[‘paging_recordsshowing’]=$paging_recordsshowing;
}
?>[/php]
paging.php
[php]<?php
if(isset($_GET[‘paging_pgnum’]))
{
$paging_currentpg=$_GET[‘paging_pgnum’];
}
else
{
$paging_currentpg="1";
}
$paging_array=array();
$paging_array[‘paging_pgurl’]=$targetpageurl;
$paging_array[‘paging_records’]=$total_records;
$paging_array[‘paging_recordsperpg’]=$paging_recordsperpg;
$paging_array[‘paging_linksshow’]=$paging_linksshow;
$paging_array[‘paging_currentpg’]=$paging_currentpg;
paging(0, $paging_array);
$paging_recordstart=$paging_array[‘paging_recordstart’];
$paging_recordend=$paging_array[‘paging_recordend’];
$paging_navigation=$paging_array[‘paging_navigation’];
$paging_currentpg=$paging_array[‘paging_currentpg’];
$paging_pages=$paging_array[‘paging_pages’];
$paging_recordsshowing=$paging_array[‘paging_recordsshowing’];
?>[/php]
style.css
[php].paging
{
padding: 5px 0px 10px 0px;
}
.paging_displaying
{
font-weight:bold;
}
.paging b
{
color: #FFFFFF;
background: #3BB9FF;
border: 2px #3090C7 solid;
padding: 3px;
}
.paging a
{
color: #3090C7;
background: #eeeeee;
border: 2px #cccccc solid;
padding: 2px;
}[/php]