完整代码如下:
!
html
head
-="-type"=";=GBK"
styletype="text/css"
body{font-size:12px;font-family:verdana;width:100%;
}
div.page{
text-align:center;
}
div.content{
height:300px;
}
div.pagea{
border:#aaaadd1pxsolid;text-decoration:none;padding:2px5px2px5px;margin:2px;}
div.pagespan.current{
border:#0000991pxsolid;background-color:#000099;padding:2px5px2px5px;margin:2px;color:#fff;font-weight:bold;}
div.pagespan.disable{
border:#eee1pxsolid;padding:2px5px2px5px;margin:2px;color:#ddd;}
div.pageform{
display:inline;
}
/stylebody?php
//设置级别错误,通知类除外error_reporting('E_ALL~E_NOTICE');
/**1---传入页码,使用GET获取**/$page=$_GET['p'];
/**2---根据页码取出数据:php-mysql处理**/$host="localhost";
$username="root";
$password="";
$db="test";
$pageSize=10;
$showPage=5;
//连接数据库,面向过程$conn=mysqli_connect($host,$username,$password);
if(!$conn){
echo"数据库连接失败";
exit;
}
//选择所要操作的数据库mysqli_select_db($conn,$db);
//设置数据库编码格式mysqli_query($conn,"SETNAMESUTF8");
//编写sql获取分页数据SELECT*FROM表名LIMIT起始位置,显示条数$sql="SELECT*FROMpageorderbyidASCLIMIT".($page-1)*$pageSize.",{$pageSize}";
//把sql语句传送到数据库$result=mysqli_query($conn,$sql);
//将数据显示到table中,并未table设置格式echo"divclass='content'
echo"tableborder=1cellspacing=0width=30%align=center
echo"trtdID/tdtdNAME/td/tr
while($row=mysqli_fetch_assoc($result)){
echo"tr
echo"td{$row['ID']}/td
echo"td{$row['NAME']}/td
echo"tr
}
echo"/table
echo"/div
//释放结果mysqli_free_result($result);
//获取数据总条数$total_sql="SELECTCOUNT(*)FROMpage";
$total_result=mysqli_fetch_array(mysqli_query($conn,$total_sql));
$total=$total_result[0];
$total_pages=ceil($total/$pageSize);
//关闭数据库mysqli_close($conn);
/**3---显示数据+显示分页条**/$page_banner="divclass='page'
//计算偏移量$pageoffset=($showPage-1)/2;
//两种情况下首页、上一页的显示效果if($page1){
$page_banner.="ahref='".$_SERVER['PHP_SELF']."?p=1'首页/a
$page_banner.="ahref='".$_SERVER['PHP_SELF']."?p=".($page-1)."'上一页/a
}else{
$page_banner.="spanclass='disable'首页/span
$page_banner.="spanclass='disable'上一页/span
}
//显示$start=1;
$end=$total_pages;
//当总条数大于分页数时if($total_pages$showPage){
if($page$pageoffset+1){
$page_banner.="...";
}
if($page$pageoffset){
$start=$page-$pageoffset;
$end=$total_pages$page+$pageoffset?$page+$pageoffset:$total_pages;//三段式}
//最前面几个特殊页号的显示。当前指的是页号1或者2时else{
$start=1;
$end=$showPage;
}
//最后面几个特殊页号的显示,当前显示的是页号7和8if($page+$pageoffset$total_pages){
$start=$start-($page+$pageoffset-$end);//注意理解这一句}
}
//显示页码for($i=$start;$i=$end;$i++){
//当前页页码上显示背景色if($page==$i){
$page_banner.="spanclass='current'{$i}/span
}
//非当前页码显示else{
$page_banner.="ahref='".$_SERVER['PHP_SELF']."?p=".$i."'{$i}/a
}
}
if($total_pages$showPage$total_pages$page+$pageoffset){
$page_banner.="...";
}
//两种情况下的尾页、下一页的显示效果if($page$total_pages){
$page_banner.="ahref='".$_SERVER['PHP_SELF']."?p=".($page+1)."'下一页/a
$page_banner.="ahref='".$_SERVER['PHP_SELF']."?p=$total_pages'尾页/a
}else{
$page_banner.="spanclass='disable'尾页/span
$page_banner.="spanclass='disable'下一页/span
}
$page_banner.="共{$total_pages}页,";
$page_banner.="formaction='mypage.php'method='get'
$page_banner.="到第inputtype='text'size=2value='1'name='p'页";
$page_banner.="inputtype='submit'value='确定'
$page_banner.="/form
$page_banner.="/div
echo$page_banner;
?/body/html