<table cellspacing="0" cellpadding="0"> <tr> <td class="t_f" id="postmessage_26137"> <div class="blockcode"><div id="code_D1B"><ol> <li> <li><?php <li>/* <li>test数据库,一个test表,唯一字段Id,作为测试使用 <li>@link http://bbs.it-home.org<li>*/ <li>$conn = mysql_connect("localhost","root",""); <li>$maxnum = 2; //每页显示记录条数 <li>mysql_select_db("test", $conn); <li>$query1 = "SELECT COUNT(*) AS totalrows FROM test "; <li>$result1 = mysql_query($query1, $conn) or die(mysql_error()); <li>$row1 = mysql_fetch_assoc($result1); <li>$totalRows1 = $row1['totalrows']; //数据集数据总条数 <li>$totalpages = ceil($totalRows1/$maxnum);//计算可分页总数,ceil()为上舍函数 <li>if(!isset($_GET['page']) || !intval($_GET['page']) || $_GET['page'] > $totalpages) $page = 1; //对3种出错进行默认处理 </li> <li>//在url参数page不存在时,page不为10进制数时,page大于可分页数时,默认为1 </li> <li>else $page = $_GET['page']; </li> <li>$startnum = ($page - 1)*$maxnum; //从数据集第$startnum条开始取,注意数据集是从0开始的 </li> <li>$query = "SELECT * FROM test LIMIT $startnum,$maxnum";//选择出符合要求的数据 从$startnum条数据开始,选出$maxnum行 </li> <li>$result = mysql_query($query, $conn) or die(mysql_error()); </li> <li>$row = mysql_fetch_assoc($result); </li> <li>?> </li> <li><html> </li> <li><head> </li> <li><meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </li> <li><title>分页示例</title> </li> <li><script language="JavaScript" type="text/JavaScript"> </li> <li><!-- <li>function MM_jumpMenu(targ,selObj,restore){ //v3.0 <li>eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); <li>if (restore) selObj.selectedIndex=0; <li>} <li>//--> </li> <li></script> </li> <li><style type="text/css"> </li> <li>a{text-decoration:none;} </li> <li>a:hover{text-decoration:underline} </li> <li>table{font-size:12px;} </li> <li>.tb{background-color:#73BB95} </li> <li>.tr{background-color:#FFFFFF} </li> <li></style> </li> <li></head> </li> <li><body> </li> <li><table width="30%" border="0" align="center" cellpadding="0" cellspacing="1" class="tb"> </li> <li><tr> </li> <li><td height="24"><div align="left">分页示例</div></td> </li> <li></tr> </li> <li><?php if($totalRows1) {//记录集不为空显示 <li>do { <li>?> </li> <li><tr class="tr"> </li> <li><td height="24"><div align="center"><?php echo $row['id'];?></div></li> </ol></div></div> </td> <li> </tr> <li> <?php }while($row = mysql_fetch_assoc($result));?> </li> <li> </table> <li> <table width="95% " border="0" align="center" cellpadding="0" cellspacing="0"> <li> <tr> <form name="form1"> <li><td height="27"><div align="center"> <li> <?php <li>echo "共<font color="#ff0000">$totalRows1</font>筆記錄"; </li> <li>echo "<font color="#ff0000"> ".$page."</font>"."/".$totalpages."頁"; </li> <li>//實作 >> 分頁連結</li> <li>$pre = $page - 1;//上一頁</li> <li>$next = $page + 1;//下一頁</li> <li>$maxpages = 4;//處理分頁時 >>顯示4頁</li> <li>$pagepre = 1;//如果目前頁是4,也要顯示前$pagepre頁,如 >> 把第3頁顯示出來</li> <li>if($page != 1) { echo "<a href="%22.%24_SERVER%5B" php_self> "; <li>echo "<a href="%22.%24_SERVER%5B" php_self> ";} <li>if($maxpages>=$totalpages) //如果總記錄不足以顯示4頁</li> <li>{$ pgstart = 1;$pgend = $totalpages;}//就不所以的頁面列印處理</li> <li>elseif(($page-$pagepre-1+$maxpages)>$totalpages)//好像總頁數是6 ,目前是5,則要把之前的3 4 顯示出來,而不僅僅是4 </li> <li>{$pgstart = $totalpages - $maxpages + 1;$pgend = $totalpages;} </li> <li>else{ </li> <li> $pgstart=(($page >>而不會是0 1 2 3 > >> </li> <li>$pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1)); </li> <li>} </li> <li>for($pg=$pgstart;$pgif($pg == $page) echo "<a href="" .>$pg</a> "; </li> <li>else echo "<a href="" .>$ pg</a> "; </li> <li>} </li> <li>if($page != $totalpages) </li> <li>{echo "<a href="%22.%24_SERVER%5B" php_self>></a> "; </li> <li>echo "<a href="%22.%24_SERVER%5B" php_self>>></a> ";} </li> <li>?> </li> <li> <select name="menu1" onchange="MM_jumpMenu('parent',this,0)"> <li> <option value="">選擇</option> </li> <li> <?php for($pg1=1;$pg1<=$totalpages;$pg1++) { <li>echo "<option value="" .>".$pg1."</option>"; </li> <li>}?> </li> <li></select> </li> <li></a> </li></a> </li> </div></td></li> </form> <li> </tr> </li> <li> </table> </li> <li> <?php } else {//記錄集為空時顯示?> </li> <li> <tr class="tr"> <li> <td height="24"><div align="center">沒有任何紀錄</div></td> </li> <li> </tr> </li> <li> </li> <li> <?php }?> </li> <li>