日期:2014-05-17 浏览次数:20439 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页实例</title>
<style>
.page A{ padding:3px 5px; float:left; border:solid #CCCCCC 1px; text-decoration:none; font-size:10px; margin-left:1px; font-family:Verdana;color:#000000;}
.page A.currentpage{ color:#FF0000; border: solid red 1px;}
.page A:hover{ background-color:#CCCCCC;}
</style>
</head>
<body><?php
error_reporting(7);
//初始化翻页链接参数
$page=$_GET["page"]+0; //思考这里是用强制转换为整数还是用什么方法呢
$page= $page<=0 ? 1 : $page; //避免$_GET["page"]为负数和0的情况
//分页尺寸
$page_size=10;
$serverName = "(local)"; //数据库服务器地址
$uid = "sa"; //数据库用户名
$pwd = "ulptfstx"; //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"MSD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false) {
echo "连接失败!";
die( print_r( sqlsrv_errors(), true));
}
//计算总行数
$total_records=sqlsrv_num_rows(sqlsrv_query("SELECT MB001 from INVMB"));
//总页数
$total_page=ceil($total_records/$page_size); //进一法取整,避免最后一页不满$page_size,
$page=$page>$total_page?$total_page:$page; //避免$_GET["page"]大于总页数
//翻页链接开始
$page_link="";
if ($total_page>1)
{
$page_link="<a>一共{$total_page}页</a>";
$page_link.="<a>当前第{$page}页</a>";
$page_link.="<a href=?page=1>首页</a>";
if ($page>1){
//页码大于1的时候,显示上一页翻页链接
$pre_page=$page-1;
$page_link.="<a href='?page=$pre_page'><<</a>";
}
//翻页列表
for ($i=1;$i<=$total_page;$i++)
{
if ($i==$page)
{
//高亮当前页页码
$page_link.="<a href='?page=$i' class='currentpage'>$i</a>";
}
else
{
$page_link.="<a href='?page=$i'>$i</a>";
}
}
//页码小于总页数的时候显示下一页翻页链接
$next_page=$page+1;
if($next_page<$total_page)
{
$page_link.="<a href='?page={$next_page}'>>></a>";
}
$page_link.="<a href='?page=$total_page'>最后一页</a>";
}
//输出记录
$offset=($page-1)*$page_size;
$query=sqlsrv_query("select * from INVMB limit $offset,$page_size") or die(sqlsrv_error());
echo "<ul>";
while ($rs=sqlsrv_fetch_array($query))
{
echo "<li>{$rs["0"]}-{$rs["1"]}</li>";
}
echo "<ul>";
echo "<div class='page'>$page_link</div>";
?>
</body>
</html>
Warning: sqlsrv_