PHP原生分页的编写
<?php
$link=mysql_connect("localhost","root","admin",'news')or die("connect error!");
$sql="select * from admin_login ";
$result=mysql_query($sql);
$cols=mysql_num_fields($result);//总列数
$total=mysql_num_rows($result);//总条数
$num=10;//每页显示条数
$url="mysql.php";//请求的URL
$cpage=isset($_GET["page"])?$_GET["page"]:1;//当前页
$pagenum=ceil($total/$num);//总页数
$offset=($cpage-1)*$num;//开始去数据的位置
$sql="select * from admin_login limit {$offset},{$num}";
$result=mysql_query($sql);
$start=$offset+1;//开始记录页
$end=($cpage==$pagenum)?$total : ($cpage*$num);//结束记录页
$next=($cpage==$pagenum)? 0:($cpage+1);//下一页
$prev=($cpage==1)? 0:($cpage-1);//前一页
echo '<table align="center" width="800" border="1">';
while($row=mysql_fetch_row($result)){
echo '<tr>';
foreach($row as $col){
echo '<td>'.$col.'</td>';
}
echo '</tr>';
}
echo '<tr><td colspan="'.$cols.'" align="right">';
echo "共<b>$total</b>条记录,本页显示<b>{$start}-{$end}</b> {$cpage}/{$pagenum}";
if($cpage==1)
echo " 首页 ";
else
echo " <a href='{$url}?page=1'>首页</a> ";
if($prev)
echo " <a href='{$url}?page={$prev}'>上一页</a> ";
else
echo " 上一页 ";
if($next)
echo " <a href='{$url}?page={$next}'>下一页</a> ";
else
echo " 下一页 ";
if($cpage==$pagenum)
echo " 尾页 ";
else
echo " <a href='{$url}?page={$pagenum}'>尾页</a> ";
echo '</td></tr>';
echo '</table>';
mysql_close();
?>