<?php
//本例子摘自phpbuilder.com
//稍加翻譯
//<[email protected]>
$limit=20; // 每頁顯示的行數
$numresults=mysql_query("select * from TABLE where YOUR CONDITIONAL HERE order by WHATEVER");//換成你所需要的sql語句
$numrows=mysql_num_rows($numresults);
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=1;
}
// 得到查詢結果
$result=mysql_query("select id,name,phone ".
"from TABLE where YOUR CONDITIONAL HERE ".
"order by WHATEVER limit $offset,$limit");
// 現在顯示查詢結果
while ($data=mysql_fetch_array($result)) {
// 在這裡插入您要顯示的結果以及樣式
}
// 顯示按鈕
if ($offset!=1) { // bypass PREV link if offset is 1
$prevoffset=$offset-20;
print "<a href=\"$PHP_SELF?offset=$prevoffset\">上一頁</a> \n";
}
// 計算頁面數
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // 顯示頁數
$newoffset=$limit*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$newoffset\">下一頁</a><p>\n";
}
?>