本文章來介紹一下關於php和orace數據庫實現數據庫的分頁功能,下面有需要的同學可以看看吧。 代碼如下 復制代碼
<html>
<body>
<?
include "/maya/inc/dbconn.php";
$sql="select max(rownum) from xqhtest where id<50";
$stmt=ociparse($gConn,$sql);
ociexecute($stmt);
ocifetch($stmt);
$rowcount=ociresult($stmt,1);
ocifreestatement($stmt);
echo("共有".$rowcount."條記錄<br>n");
$recordperpage=15; //每頁顯示多少條記錄
$pages=ceil($rowcount/$recordperpage); //總頁數
echo("共有".$pages."頁<br>n");
?>
<table border=0><tr><td>id</td><td>name</td></tr>
<?
//判斷偏移量參數是否傳遞給了腳本,如果沒有就使用默認值0
if (empty($offset))
{
$offset=1;
}
$currentpage=ceil($offset/$recordperpage); //顯示當前頁
echo("當前頁:".$currentpage."<br>n");
$endset=$offset+$recordperpage;
$stmt2=ociparse($gConn,"SELECT rownum,id,name FROM xqhtest WHERE id<50 and rownum<".$endset." minus select rownum,id,name from xqhtest where id<50 and rownum<".$offset);
//echo "SELECT id,name FROM xqhtest WHERE rownum<".$endset." minus select id,name from xqhtest where rownum<".$offset."<br>n";
ociexecute($stmt2);
//可以是任何sql語句,但select後面一定要有rownum,這是oracle特有的!
while (ocifetch($stmt2))
{
echo("<tr><td>".ociresult($stmt2,"ID")."</td><td>".ociresult($stmt2,"NAME")."</td></tr>n");
//換成你用於顯示返回記錄的代碼
}
//要寫出到所有頁面的鏈接
print "</table><br><br>";
for ($i=1; $i <= $pages; $i++)
{
$newoffset=($recordperpage*($i-1))+1;
print "<a href="$PHP_SELF?offset=$newoffset">$i</a> n";
}
print "<br>";
$nextoffset=$recordperpage*$currentpage+1;
$prevoffset=$recordperpage*($currentpage-2)+1;
//判斷是否需要上一頁連接
if (($currentpage>1) && ($currentpage<=$pages))
{
print "<a href="$PHP_SELF?offset=$prevoffset">上一頁</a> n";
}