php分页例子实现读取mysql数据分页显示
以下代码是PHP分页案例,测试通过,主要是PHP+mysql实现分页,代码来处百度空间,有兴趣看的话可以了解一下PHP是如何分页的?
<?php $link = mysql_connect("localhost","root", "2855") //连接数据库 or die("连接不上服务器:".mysql_error()); mysql_select_db("aming"); $ittype=$_GET['ittype']; if($ittype==""){ echo "请输入查询条件"; exit; } $PageSize = 4;//每页显示记录数 $StartRow = 0; //开始显示记录的编号 //获取需要显示的页数,由用户提交 if(empty($_GET['PageNo'])){ //如果为空,则表示第1页 if($StartRow == 0){ $PageNo = $StartRow + 1; //设定为1 } }else{ $PageNo = $_GET['PageNo']; //获得用户提交的页数 $StartRow = ($PageNo-1) * $PageSize; //获得开始显示的记录编号 $StartRow = ($PageNo - 1) * $PageSize; } //因为显示页码的数量是动态变化的 //假如总共有一百页,则不可能同时显示100个链接 //而是根据当前的页数显示一定数量的页面链接 //设置显示页码的初始值 if($PageNo % $PageSize == 0){ $CounterStart = $PageNo - ($PageSize - 1); }else{ $CounterStart = $PageNo - ($PageNo % $PageSize) + 1; } //显示页码的最大值 $CounterEnd = $CounterStart + ($PageSize - 1); ?> <html> <head> <title>分页显示记录</title> <link rel="stylesheet" href="include/style.css" type="text/css"></head> <?php /*$itname=$_POST['itname']; if($itname ==""){ echo "请输入查询条件"; exit; }*/ if($ittype) $TRecord = mysql_query("select *from pc_it where ittype=$ittype"); $result = mysql_query("select *from pc_it where ittype=$ittype ORDER BY id DESC LIMIT $StartRow,$PageSize"); //获取总记录数 $RecordCount = mysql_num_rows($TRecord); //获取总页数 $MaxPage = $RecordCount % $PageSize; if($RecordCount % $PageSize == 0){ $MaxPage = $RecordCount / $PageSize; }else{ $MaxPage = ceil($RecordCount / $PageSize); } ?> <body class="UsePageBg"> <table width="100%" border="0" class="InternalHeader"> <tr> <td width="24%"><font size=4>分页显示记录</font></td> <td width="76%"> <font size=4> <?php print "总共 $RecordCount 条记录 - 当前页: $PageNo of $MaxPage" ?> </font> </td> </tr> </table> <br> <table width="100%" border="0" class="NormalTableTwo"> <tr> <td class="InternalHeader">型号</td> <td class="InternalHeader" >参数</td> <td class="InternalHeader" >价格</td> </tr> <?php $i = 1; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $bil = $i + ($PageNo-1)*$PageSize; // $bil = $i + ($PageNo-1)*$PageSize; ?> <tr> <td class="NormalFieldTwo" ><?php echo $row[1] ?></td> <td class="NormalFieldTwo" ><?php echo $row[3] ?></td> <td class="NormalFieldTwo" ><?php echo $row[4] ?></td> </tr> <?php $i++; }?> </table><br><table width="100%" border="0" class="InternalHeader"> <tr> <td> <div align="center"> <form action = search2.php method = GET > <?php echo "<font size=4>"; //显示第一页或者前一页的链接 //如果当前页不是第1页,则显示第一页和前一页的链接 if($PageNo != 1){ $PrevStart = $PageNo - 1; print "<a href=search2.php?PageNo=1>首页 </a>: "; print "<a href=search2.php?PageNo=$PrevStart&ittype=$ittype>上一页 </a>"; } print " [ "; $c=0; //打印需要显示的页码 for($c=$CounterStart;$c<=$CounterEnd;$c++){ if($c < $MaxPage){ if($c == $PageNo){ if($c % $PageSize == 0){ print "$c "; }else{ print "$c,"; } }else if($c % $PageSize == 0){ echo "<a href=search2.php?ittype=$ittype&PageNo=$c>$c</a> "; //echo "<a href=search2.php?PageNo=$c&ittype=$ittype>$c</a> "; }else{ echo "<a href=search2.php?ittype=$ittype&PageNo=$c>$c,</a> "; //echo "<a href=search2.php?PageNo=$cittype=$ittype>$c</a> ,"; } //END IF }else{ if($PageNo == $MaxPage){ print "$c "; break; }else{ echo "<a href=search2.php?ittype=$ittype&PageNo=$c>$c</a> "; //echo "<a href=search2.php?PageNo==$c&ittype=$ittype>$c</a> "; break; }//END IF }//END IF }//NEXT echo "] "; if($PageNo < $MaxPage){ //如果当前页不是最后一页,则显示下一页链接 $NextPage = $PageNo + 1; echo "<a href=search2.php?PageNo=$NextPage&ittype=$ittype>下一页</a>"; } //同时如果当前页补上最后一页,要显示最有一页的链接 if($PageNo < $MaxPage){ $LastRec = $RecordCount % $PageSize; if($LastRec == 0){ $LastStartRecord = $RecordCount - $PageSize; } else{ $LastStartRecord = $RecordCount - $LastRec; } print " : "; echo "<a href=?PageNo=$MaxPage&ittype=$ittype>末页</a>"; } echo "<form action='search2.php' method='post'> "; echo "转到第<input type=text name='PageNo' size='2' value=".$PageNo.">页"; echo "<input name=itname type=hidden value=".$ittype.">"; echo "<input type=submit name='Submit' value='Go'>"; echo "</form>"; echo "</font>"; ?> </form> </div> </td> </tr> </table> <?php mysql_free_result($result); mysql_free_result($TRecord); ?> </body> </html>