<title>PHP+ajax分頁示範</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script language="javascript" src="ajaxpage.js"></script> <div id="result"> <?php $terry=mysql_connect("localhost","root","")or die("串連資料庫失敗:".mysql_error()); mysql_select_db("ajaxtest",$terry); mysql_query("set NAMES 'utf8'"); $result=mysql_query("select * from tb_user"); $total=mysql_num_rows($result) or die(mysql_error()); $page=isset($_GET['page'])?intval($_GET['page']):1; $page_size=3; $url='index.php'; $pagenum=ceil($total/$page_size); $page=min($pagenum,$page); $prepage=$page-1; $nextpage=($page==$pagenum?0:$page+1); $pageset=($page-1)*$page_size; $pagenav=''; $pagenav.="顯示第<font color='red'>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>記錄 共<b><font color='yellow'>".$total."</font></b>條記錄 現在是第 <b><font color='blue'>".$page."</font></b> 頁 "; if($page<=1) $pagenav.="<a style=cursor:not-allowed;>首頁</a> "; else $pagenav.="<a onclick=javascript:dopage('result','$url?page=1') style=cursor:pointer;>首頁</a> "; if($prepage) $pagenav.="<a onclick=javascript:dopage('result','$url?page=$prepage') style=cursor:pointer;>上一頁</a> "; else $pagenav.="<a style=cursor:not-allowed;>上一頁</a> "; if($nextpage) $pagenav.="<a onclick=javascript:dopage('result','$url?page=$nextpage') style=cursor:pointer;>下一頁</a> "; else $pagenav.="<a style=cursor:not-allowed;>下一頁</a> "; if($pagenum) $pagenav.="<a onclick=javascript:dopage('result','$url?page=$pagenum') style=cursor:pointer;>尾頁</a> "; else $pagenav.="<a style=cursor:not-allowed;>尾頁</a> "; $pagenav.="共".$pagenum."頁"; if($page>$pagenum){ echo "error:沒有此頁".$page; exit(); } ?> <table align="center" border="2" width="300"> <tr bgcolor="#cccccc" align="center"> <td>使用者名稱</td> <td>使用者密碼</td> </tr> <?php $info=mysql_query("select * from tb_user order by id desc limit $pageset,$page_size"); while($array=mysql_fetch_array($info)){ ?> <tr align="center"> <td><?php echo $array['id'];?></td> <td><?php echo $array['username'];?></td> </tr> <?php } ?> </table> <?php echo "<p align=center>$pagenav</p>"; ?> </div> |