日期:2014-05-17 浏览次数:20470 次
<!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; $conn=mysql_connect("localhost","root","root") or die(mysql_error()); mysql_select_db("xinyang"); //计算总行数 $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>"; } //翻页列表 //步进分页,翻页列表的数字始终只显示9个或者自定义的个数,本例定义只显示9个,当前页左右各显示4个页码 if ($total_page>9) { if ($page>4) { $from=$page-4; $to=$page+4; if ($to>$total_page) { $from=$total_page-8; $to=$total_page; } } else { $from=1; $to=9; } } else { $from=1; $to=$total_page; } for ($i=$from;$i<=$to;$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>