Monday, March 3, 2014

PHP Pagination (cont)

Create a file "pagenum.php" and copy the code below

pagenum.php
<?php
function getPageNum($totalPage = 50, $currentPage = 1,$page = "index.php"){
$totalNum = 5; // minimum is 2

$stx = 1;
$enx = ($totalNum < 2) ? 3 : ($totalNum + 1);

for($t=0;$t<$totalPage;$t++){
$n = ($totalNum*$t)+1;
$start = ($n == 1) ? $n : $n-$t; 
$end = $start+$totalNum;
if($start <= $totalPage){
if($currentPage > $start && $currentPage < $end){
$stx = $start;
$enx = $end;
}
}
}
$prev = $currentPage-1;
$pclass = "button";
$plink = '<a href="'.$page.'&page=1"><<</a>';
$plinkx = '<a href="'.$page.'&page='.$prev.'"><</a>';
if($currentPage <= 1){
$pclass = "ndis";
$plink = "<span><<</span>";
$plinkx = "<span><</span>";
}
$result = '<div id="'.$pclass.'">'.$plink.'</div>';
$result .= '<div id="'.$pclass.'">'.$plinkx.'</div>';
for($i=$stx;$i<=$enx;$i++){
$num = ($i <= $totalPage) ? $i : "";
if($num != ""){
$class = ($num == $currentPage) ? "pnums" : "pnum";
$result .= '<div id="'.$class.'">
<a href="'.$page.'&page='.$num.'">'.$num.'</a>
</div>';
}
}
if($enx < $totalPage){
$result .= '<div id="dots">.......</div>';
$result .= '<div id="pnum"><a href="'.$page.'&page='.$totalPage.'">'.$totalPage.'</a></div>';
}
$next = $currentPage+1;
$nclass = "button";
$nlink = '<a href="'.$page.'&page='.$next.'">></a>';
$nlinkx = '<a href="'.$page.'&page='.$totalPage.'">>></a>';
if($currentPage >= $totalPage){
$nclass = "ndis";
$nlink = "<span>></span>";
$nlinkx = "<span>>></span>";
}
$result .= '<div id="'.$nclass.'">'.$nlink.'</div>';
$result .= '<div id="'.$nclass.'">'.$nlinkx.'</div>';

return $result;
} ?>

Click link below to continue.
style.css

No comments:

Post a Comment