CREATE TABLE joke ( id int(5) NOT NULL auto_increment, biaoti varchar(40) NOT NULL, neirong text NOT NULL, PRIMARY KEY (id) ); |
<html> <head> <title>分页显示的实现方法</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <script language="javascript"> /* 定义一弹出窗口,来显示具体内容*/ function popwin(url) { window.open(url,"","left=340, top=190, height=280, width=400, resizable=yes, scrollbars=yes, status=no, toolbar=no, menubar=no, location=no"); } </script> </head> <body leftMargin=0 topMargin=0 rightmargin=0 > <?php //连接数据库 $db=mysql_connect("localhost","root",""); mysql_select_db("XinXiKu",$db); //设定每一页显示的记录数 $pagesize=15; //取得记录总数,计算总页数用 $res=mysql_query("select count(*) from joke " ,$db); $myrow = mysql_fetch_array($res); $numrows=$myrow[0]; //计算总页数 $pages=intval($numrows/$pagesize); if ($numrows%$pagesize) $pages++; //判断页数设置与否,如无则定义为首页 if (!isset($page)) $page=1; //判断转到页数 if (isset($ys)) if ($ys>$pages) $page=$pages; else $page=$ys; //计算记录偏移量 $offset=$pagesize*($page-1); //取记录 $res=mysql_query("select id,biaoti from joke order by id desc limit $offset,$pagesize" ,$db); //循环显示记录 if ($myrow = mysql_fetch_array($res)) { $i=0; ?> <table width="101%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="5%" bgcolor="#E1E9FB"></td> <td width="95%" bgcolor="#E1E9FB"><font color="#FF6666" size="2">内容</font></td> </tr> <?php do { $i++; ?> <tr> <td width="5%" bgcolor="#E6F2FF"><?php echo $i;?></td> <td width="95%" bgcolor="#E6F2FF"><font size="2"> <a href=http://www.sheup.com/?"javascript:popwin('jokenr.php?id=<?php echo $myrow[0];?>')" ><?php echo $myrow[1];?></a></font></td> </tr> <?php } while ($myrow = mysql_fetch_array($res)); echo "</table>" ; } //显示总页数 echo "<div align='center'>共有".$pages."页(".$page."/".$pages.")<br>"; //显示分页数 for ($i=1;$i<$page;$i++) echo "<a href=http://www.sheup.com/?'fy.php?page=".$i."'>第".$i ."页</a> "; echo "第".$page."页 "; for ($i=$page+1;$i<=$pages;$i++) echo "<a href=http://www.sheup.com/?'fy.php?page=".$i."'>第".$i ."页</a> "; echo "<br>"; //显示转到页数 echo "<form action='fy.php' method='post'> "; //计算首页、上一页、下一页、尾页的页数值 $first=1; $prev=$page-1; $next=$page+1; $last=$pages; if ($page>1) { echo "<a href=http://www.sheup.com/?'fy.php?page=".$first."'>首页</a> "; echo "<a href=http://www.sheup.com/?'fy.php?page=".$prev."'>上一页</a> "; } if ($page<$pages) { echo "<a href=http://www.sheup.com/?'fy.php?page=".$next."'>下一页</a> "; echo "<a href=http://www.sheup.com/?'fy.php?page=".$last."'>尾页</a> "; } echo "转到<input type=text name='ys' size='2' value=".$page.">页"; echo "<input type=submit name='Submit' value='Go'>"; echo "</form>"; echo "</div>"; ?> </body> </html> |