当前位置:Linux教程 - Oracle - 用php+oracle实现分页的示例程序

用php+oracle实现分页的示例程序

<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=http://www.sheup.com/?\"$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=http://www.sheup.com/?\"$PHP_SELF?offset=$prevoffset\">上一页</a> \n";
}

//判断是否需要下一页连接
if (($pages!=0) && ($currentpage<$pages))
{
  print "<a href=http://www.sheup.com/?\"$PHP_SELF?offset=$nextoffset\">下一页</a> \n";
}

ocifreestatement($stmt2);
ocilogoff($gCon