前一篇講了”上一頁、下一頁”的分頁方式,
這次來玩玩看直接以數字指定的分頁方式 🙂
*數字分頁
開頭應該這樣子寫:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("../config.php"); | |
$result = mysql_query("SELECT * FROM member"); | |
$total = mysql_num_rows($result); | |
if(!isset($page)) (int)$page=1; //若無設定$page, 則預設為1 | |
$page_num = 2; //每頁設定顯示筆數 | |
$front_back = 2; | |
$begin = ($page-1) * $page_num; | |
$totalpage = ceil($total / $page_num); | |
$query = "SELECT * FROM member ORDER BY num DESC LIMIT $begin,$page_num"; | |
$result = mysql_query($query); | |
$row = mysql_fetch_row($result); | |
?> |
中間夾的就是DataGrid的部份
然後在表格底下加入分頁:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table width="400" align="center"> | |
<tr> | |
<td><?echo "共".$totalpage."頁,現在是第".$page."頁";?> | |
</td> | |
<td> | |
<a href="<?=$PHP_SELF?>?page=1"title="最首頁">最首頁</a> | |
<? | |
if($page-$front_back<=1){ | |
$front=1; | |
}else{ | |
echo "..."; | |
$front=$page-$front_back; | |
} | |
for($i=$front;$i<$page;$i++){ | |
echo "<a href=\"$PHP_SELF?page=".$i."\">".$i."</a>"; | |
} | |
?> | |
<? | |
echo "<b>$page</b>"; | |
?> | |
<? | |
if($page+$front_back>=$totalpage){ | |
$back=$totalpage; | |
}else{ | |
$back=$page+$front_back; | |
} | |
for($i=$page+1;$i<=$back;$i++){ | |
echo "<a href=\"$PHP_SELF?page=".$i."\">".$i."</a>"; | |
} | |
if($back!=$totalpage) echo "..."; | |
?> | |
<a href="<?=$PHP_SELF?>?page=<?=$totalpage?>"title="最末頁">最末頁</a> | |
</td> | |
</tr> | |
</table> |
*參考資料
PHP Official Site
http://linux.tnc.edu.tw/techdoc/adodb/book1.html