In Pagination you can distribute number of records on different pages and specify links so that user can go from one page to next page. Generally what user see is First, Previous, Next, Last links along with few page numbers like 1,2,3,4,5 and if he is on page 9 then something like 7,8,9,10,11 or 9,10,11,12,13 etc.
But what when there are large number of pages say 1000 and a user wants to go to page number 400 then according to simple pagination he can click on Page 5 then page 7 then 9 (when there are 5 pages being shown) and page 5 then 12 then 19 (when there are 15 pages being shown) etc. and this would take time to reach Page Number 400. So, in such cases we should display more pages also.
With this code you will be able to view more pages. Lets take an example. Say there are 10000 records and on each page 10 records are being shown. So, number of pages is 1000. Currently selected Page is 500. So, links shown will be of Page 1,2,3,497,498,499,500,501,502,503,998,999,1000 and pages between 3-497 and 503-998 range with some page number difference. Here, we have taken links to be shown = 3, that is why Page 1,2,3 are showing.
Pagination in PHP is easy and you can show advance pagination by following code. There are 4 files (don’t go to the number of files, separate files 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>Advance Pagination</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Advance Pagination Example</h2>
<p>In Pagination you can distribute number of records on different pages and specify links so that user can go from one page to next page. Generally what user see is First, Previous, Next, Last links along with few page numbers like 1,2,3,4,5 and if he is on page 9 then something like 7,8,9,10,11 or 9,10,11,12,13 etc.</p>
<p>But what when there are large number of pages say 1000 and a user wants to go to page number 400 then according to simple pagination he can click on Page 5 then page 7 then 9 (when there are 5 pages being shown) and page 5 then 12 then 19 (when there are 15 pages being shown) etc. and this would take time to reach Page Number 400. So, in such cases we should display more pages also.</p>
<p>With this code you will be able to view more pages. Lets take an example. Say there are 10000 records and on each page 10 records are being shown. So, number of pages is 1000. Currently selected Page is 500. So, links shown will be of Page 1,2,3,497,498,499,500,501,502,503,998,999,1000 and pages between 3-497 and 503-998 range with some page number difference. Here, we have taken links to be shown = 3, that is why Page 1,2,3 are showing.</p>
<?php
$targetpageurl="index.php";
$total_records="10000";
$paging_recordsperpg="10";
$paging_linksshow="3";
$paging_linksshowmax="25";
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
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_linksshowmax=$paging_array[‘paging_linksshowmax’];
//$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);
$paging_from=$paging_currentpg-$paging_linksshow;
if($paging_from<‘1′)
{
$paging_from="1";
}
//$paging_to=$paging_currentpg+ceil($paging_linksshow/2);
$paging_to=$paging_currentpg+$paging_linksshow;
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> ";
}
//Link Page Number Links
if($paging_pages<$paging_linksshowmax)
{
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> ";
}
}
}
else
{
//Front Page Links
$paging_linksshowmax_left=$paging_linksshowmax-((4*$paging_linksshow)+1);
$paging_frontcurrent_linksshow=floor($paging_linksshowmax_left/2);
$paging_currentlast_linksshow=floor($paging_linksshowmax_left/2);
if($paging_from>$paging_linksshow)
{
$paging_front_start=1;
$paging_front_end=$paging_linksshow;
}
elseif($paging_from>1)
{
$paging_front_start=1;
$paging_front_end=$paging_from-1;
}
else
{
$paging_front_start=1;
$paging_front_end=0;
}
//Current Page Links
$paging_current_start=$paging_from;
$paging_current_end=$paging_to;
//Last Page Links
if($paging_to<$paging_pages)
{
if($paging_pages-$paging_to<$paging_linksshow)
{
$paging_last_start=$paging_to+1;
$paging_last_end=$paging_pages;
}
else
{
$paging_last_start=$paging_pages-$paging_linksshow+1;
$paging_last_end=$paging_pages;
}
}
else
{
$paging_last_start=1;
$paging_last_end=0;
}
//Front-Current Page Links
if($paging_current_start>$paging_front_end)
{
$paging_frontcurrent_pgdiff=FLOOR(($paging_current_start-$paging_front_end)/$paging_frontcurrent_linksshow);
if($paging_frontcurrent_pgdiff>’0′)
{
$paging_frontcurrent_start=$paging_front_end+$paging_frontcurrent_pgdiff;
$paging_frontcurrent_next=$paging_frontcurrent_start;
}
else
{
$paging_frontcurrent_next=$paging_current_start;
}
}
else
{
$paging_frontcurrent_next=$paging_current_start;
}
//Current-Last Page Links
if($paging_last_start>$paging_current_end)
{
$paging_currentlast_pgdiff=FLOOR(($paging_last_start-$paging_current_end)/$paging_currentlast_linksshow);
if($paging_currentlast_pgdiff>’0′)
{
$paging_currentlast_start=$paging_current_end+$paging_currentlast_pgdiff;
$paging_currentlast_next=$paging_currentlast_start;
}
else
{
$paging_currentlast_next=$paging_last_start;
}
}
else
{
$paging_currentlast_next=$paging_last_start;
}
//Printing Front Page Links
for($i_paging=$paging_front_start; $i_paging<=$paging_front_end; $i_paging++)
{
$paging_array[‘paging_navigation’].=" <a href=’?paging_pgnum=$i_paging’>$i_paging</a> ";
}
//Printing Front-Current Page Links
while($paging_frontcurrent_next<$paging_current_start)
{
$i_paging=$paging_frontcurrent_next;
$paging_array[‘paging_navigation’].="..<a href=’?paging_pgnum=$i_paging’>$i_paging</a>..";
$paging_frontcurrent_next=$paging_frontcurrent_next+$paging_frontcurrent_pgdiff;
}
//Printing Current Page Links
for($i_paging=$paging_current_start; $i_paging<=$paging_current_end; $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> ";
}
}
//Printing Current-Last Page Links
while($paging_currentlast_next<$paging_last_start)
{
$i_paging=$paging_currentlast_next;
$paging_array[‘paging_navigation’].="..<a href=’?paging_pgnum=$i_paging’>$i_paging</a>..";
$paging_currentlast_next=$paging_currentlast_next+$paging_currentlast_pgdiff;
}
//Printing Last Page Links
for($i_paging=$paging_last_start; $i_paging<=$paging_last_end; $i_paging++)
{
$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_array[‘paging_linksshowmax’]=$paging_linksshowmax;
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;
text-decoration:none;
}[/php]