COMPUTER LEARNING CENTER
WWW.HUUKHANG.COM
4.
TAÄP TIN DUØNG CHUNG
Ngoaøi caùch cheøn ôû treân, neáu baïn coù nhöõng haøm söû duïng chung cho caùc trang PHP khaùc
thì baïn khai baùo thaønh moät trang PHP khaùc sau ñoù duøng cuù phaùp cheøn taäp tin ñeå cheøn
chuùng vaøo khi coù nhu caàu.
Ví duï trong tröôøng hôïp naøy chuùng ta muoán söû duïng chung haøm coù teân getPaging nhaän 5
tham soá $totalRows (toång soá maåu tin), $curPg (soá trang hieän haønh), $pg (soá trang trình
baøy), $re (soá maåu tin treân 1 trang), $file (trang php caàn goïi) trong taäp tin paging.php.
<?php
function paging($totalRows,$curPg,$pg,$re,$file)
{
$paging="";
$mxR = $re;
$mxP = $pg;
if($totalRows%$mxR==0)
$totalPages
=
(int)($totalRows/$mxR);
else
$totalPages
=
(int)($totalRows/$mxR+1);
$curRow = ($curPg-1)*$mxR+1;
if($totalRows>$mxR)
{
$start=1;
$end=1;
$paging1 ="";
for($i=1;$i<=$totalPages;$i++)
{
if(($i>((int)(($curPg-1)/$mxP))* $mxP) && ($i<=((int)(($curPg-
1)/$mxP+1))* $mxP))
{
if($start==1)
$start=$i;
if($i==$curPg)
$paging1 .= $i." ";
else
{
$paging1 .= "<a class=lslink href='$file";
$paging1
.="&page=".$i."'>".$i;
$paging1
.="</a> ";
}
$end=$i;
}
}
$paging.= "Trang : " ;
if($curPg>$mxP)
{
$paging .="<a class=lslink href='$file";
$paging
.="&page=".($start-1);
$paging
.="'>Previous</a> ";
}
$paging.=$paging1;
if(((($curPg-1)/$mxP+1)*$mxP) < $totalPages)
{
$paging .= "<a class=lslink href='$file";
$paging
.="&page=".($end+1);
$paging
.="'>Next</a> ";
}
}
return $paging;
}
?>
Giaùo vieân: Phaïm Höõu Khang