最近学了PHP的分页,简单的写(模仿)了一个从数据库读取数据然后数据显示在网站上的分页代码。
效果:
代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hsdm</title> <style type="text/css"> .content { height: 300px; } .page a{ text-decoration: none; border:1px solid #aaaadd; padding:2px 5px 2px 5px; margin:2px; } .page { text-align: center; } .page span.current { border: 1px solid #000099; background-color:#000099; padding:2px 5px 2px 5px; margin:2px; color:#fff; font-weight:bold; } .page span.disable { border: #eee 1px solid; padding:2px 5px; margin:2px; color:#ddd; } .page form { display: inline; } </style> </head> <body> <?php error_reporting('E_ALL&~E_NOTICE'); $page_size=10; $show_page=5; $page=$_GET['p']; if(!$page) $page=1; $page_banner="<div class='page'>"; $host="localhost:3306"; $user="root"; $passwd=""; $conn=mysqli_connect($host,$user,$passwd); if(!$conn) die("数据库连接失败:".mysqli_error($conn)); mysqli_select_db($conn,'new'); mysqli_query($conn,'set names utf8'); $sql='select * from num order by id asc limit '.(($page-1)*$page_size).",$page_size;"; $result=mysqli_query($conn,$sql); echo '<div class="content">'; echo '<table border="1px" cellspacing="0px" align="center" width="30%">'; echo "<tr><th>ID</th><th>NAME</th></tr>"; while($row=mysqli_fetch_array($result,MYSQL_ASSOC)) { echo"<tr><td>{$row['id']}</td><td>{$row['name']}</td></tr>"; } echo "</table>"; echo "</div>"; mysqli_free_result($result); $total_sql="select count(*) from num"; $total_result=mysqli_fetch_array(mysqli_query($conn,$total_sql),MYSQL_NUM); $total=$total_result[0]; $total_page=ceil($total/$page_size); mysqli_close($conn); $pageoffset=($show_page-1)/2; if($page>1){ $page_banner.="<a href=".$_SEAVER['PHP_SELF']."?p=1>首页</a>"; $page_banner.="<a href=".$_SEAVER['PHP_SELF']."?p=".($page-1).">上一页</a>"; } else { $page_banner.="<span class='disable'>首页</span>"; $page_banner.="<span class='disable'>上一页</span>"; } $start=1; $end=$total_page; if($total_page>$show_page){ if($page>$pageoffset+1){ $page_banner.='...'; } if($page>$pageoffset) { $start=$page-$pageoffset; $end=$total_page>$page+$pageoffset?$page+$pageoffset:$total_page; } else { $start=1; $end=$show_page; } if($page+$pageoffset>$total_page){ $start=$start-($page+$pageoffset-$end); } } for($i=$start;$i<=$end;$i++) { if($page==$i) $page_banner.="<span class='current'>{$i}</span>"; else $page_banner.="<a href=".$_SERVER['PHP_SELF']."?p=$i>".$i."</a>"; } if($total_page>$show_page&&$total_page>$page+$pageoffset) $page_banner.="..."; if($page<$total_page) { $page_banner.="<a href=".$_SEAVER['PHP_SELF']."?p=".($page+1).">下一页</a>"; $page_banner.="<a href=".$_SEAVER['PHP_SELF']."?p=".$total_page.">尾页</a>"; } else { $page_banner.="<span class='disable'>尾页</span>"; $page_banner.="<span class='disable'>下一页</span>"; } $page_banner.="共{$total_page}页,"; $page_banner.="<form action='mypage.php' method='get'>"; $page_banner.="到第<input type='text' size=2 name='p'>页"; $page_banner.="<input type='submit' value='确定'>"; $page_banner.="</form>"; $page_banner.="</div>"; echo $page_banner; ?> </body> </html>