日期:2014-05-17 浏览次数:20578 次
<!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><? error_reporting(7); //初始化翻页链接参数 $page=$_GET["page"]+0;//思考这里是用强制转换为整数还是用什么方法呢? $page= $page<=0 ? 1 : $page;//避免$_GET["page"]为负数和0的情况 //分页尺寸 $page_size=10; $conn=mysql_connect("localhost","root","root") or die(mysql_error()); mysql_select_db("test"); //计算总行数 $total_records=mysql_num_rows(mysql_query("SELECT id from product")); //总页数 $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=mysql_query("select * from product limit $offset,$page_size") or die(mysql_error()); echo "<ul>"; while ($rs=mysql_fetch_array($query)) { echo "<li>{$rs["id"]}-{$rs["ename"]}</li>"; } echo "<ul>"; echo "<div class='page'>$page_link</div>"; ?> </body> </html>