Tuesday, June 24, 2008

PHP Paging Code: Break up records into pages

<!--
============= Connection.php =================
//-->
<?
$server = "localhost";
$user = "root";
$pass = "";


$OpSe = mysql_connect($server, $user, $pass ) or die(mysql_error()):
$OpDb = mysql_select_db("DATABASE_NAME",$OpSe) or die(mysql_error());
?>





<!--
============= Paging.php =================
//-->
<style type="text/css">
a.spage:link{
border: 1px solid #000000;font-family:verdana; font-size:10px;padding-top:1px; padding-bottom:2px;padding-left:4px;padding-right:4px;color:white;font-weight:bold;text-decoration:none;background-color:#ffffff; color:#000000;
}


a.lspage:link{
border: 1px solid #000000;font-family:verdana; font-size:10px;padding-top:1px; padding-bottom:2px;padding-left:4px;padding-right:4px;color:white;font-weight:bold;text-decoration:none; color:#000000;
}
</style>
<?
// set record display per page....
$PP = 10;
//---------------
isset($_GET["st"]) && $_GET["st"]!="" ? $st=$_GET["st"] : $st=0;
?>
<table border="1">
<tr bgcolor="#999999">
<td><b>Field1 Date</b></td>
<td><b>Field2 Date</b></td>
<td><b>Field3 Date</b></td>
<td><b>Field4 Date</b></td>
</tr>
<?
$query ="SELECT field1, field2, field3, field4 FROM TABLE_NAME LIMIT ".$st.",".$PP;
$rs = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($rs)){
?>
<tr bgcolor="#CCCCCC">
<td><?=$row["field1"]?></td>
<td><?=$row["field2"]?></td>
<td><?=$row["field3"]?></td>
<td><?=$row["field4"]?></td>
</tr>
<?
}
?>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="99%" align="center">
<tr>
<td width="1%">
<? if($st>=$PP){?>
<a href="?st=<?=$st-$PP?>">Pre</a>
<? }?>
</td>
<td align="center">
<?
// Do not change below this line-----------------------------------------
$total--;
$pgv = $total/$PP;

if($pgv>1)
$pg_a = explode(".",$pgv);
else
$pg_a = $pgv;

if($pg_a[1]>0)
$pg = $pg_a[0]+1;
else
$pg = 0;

if($pg=="")$pg = $pgv;
$selpage = $st / $PP;
$selpage++;
$selpage = (int) $selpage;
for($ploop=1,$stl=1 ;$ploop<=$pg;$ploop++,$stl+=$PP){
echo ' <a href="?st='.$stl.'" class="';
echo $ploop==$selpage ? "spage" : "lspage";
echo '">'.$ploop.'</a> ';
}
// Do not change above this line-----------------------------------------
?>
</td>
<td align="right" width="1%">
<? if($st<$total-$PP){?>
<a href="?st=<?=$st+$PP?>">Next</a>
<? }?>
</td>
</tr>
</table>

No comments: