php与oracle11g经典分页

10 篇文章 0 订阅

数据表,sequence,触发器创建:http://blog.csdn.net/e421083458/article/details/8944164


<?php
$t1 = xdebug_time_index();
$conn = oci_connect("SCOTT","TIGER","192.168.1.244:1521/orcl");
if(!$conn){
	$e = oci_error();
	print htmlentities($e['message']);
	exit();
}

//总记录数
$sql = "select ROWNUM rn from EXAMPLE";
$par = oci_parse($conn, $sql);
oci_execute($par);
$nRecords = ocifetchstatement($par, $rs);


//取得页码
$page = $_GET['page'];
//定义每页显示信息条数
$page_size = 20;
//当页码参数为空时,将页码设为1
if ($page == "")
{
    $page = 1;
}
//当页码大于1时,每页的开始记录是 (页码-1) * 每页记录数 +1
$startRow = ($page - 1) * $page_size + 1;
$endRow = $page * $page_size;

//方法一:
$cmdstr = "select *
from
(
    select ROWNUM rn ,temp.*
    from (select * from EXAMPLE) temp
    where ROWNUM <= $endRow
)
where rn >= $startRow";

//方法二:
//$cmdstr = "select * from example where rowid in(select rid from(select rownum rn,rid from(select rowid rid,id from EXAMPLE order by id desc) where rownum<".$endRow.")where rn>".$startRow.")order by id desc";
echo $cmdstr;

//执行查询SQL
$parsed = ociparse($conn, $cmdstr);
ociexecute($parsed);
$nrows = ocifetchstatement($parsed, $results);

echo "<html><head><title>Oracle PHP Test</title></head><body>";
echo "<center><h2>Oracle PHP Test</h2>\n";

//表字段名获取
$arrName = array_keys($results);
echo "<table border=1 cellspacing='0' width='70%'>\n<tr>\n";
for ($i = 0; $i < count($arrName); $i++)
{
    echo "<td>" . $arrName[$i] . "</td>\n";
}
echo "</tr>\n";
//循环输出记录
for ($i = 0; $i < $nrows; $i++)
{
    echo "<tr>\n";
    foreach ($results as $data)
    {
        echo "<td>$data[$i]</td>\n";
    }
    echo "</tr>\n";
}
echo "<tr><td colspan='" . count($arrName) . "'> Number of Rows:".$nRecords."</td></tr></table>\n<br>";
//显示分页
//Pages: First Prev   1 2 3 4 5 6> Next Last
//总页数
$totalPage = ceil($nRecords / $page_size);
//上一页链接
$Prev = $page - 1;
if ($Prev < 1)
{
    $Prev = 1;
}
//下一页链接
$Next = $page + 1;
if ($Next > $totalPage)
{
    $Next = $totalPage;
}
//输出上一页链接
if ($page <> 1)
{
    echo '<span><a href="?page=1">First</a></span>';
    echo '<span><a href="?page=' . $Prev . '">Prev</a></span>';
}
else
{
    echo '<span>First</span>';
    echo '<span>Prev</span>';
}
//页码数字链接
//显示的数字个数
$pageNumber = 5;
//页码数算法
$pagebegin = $page - $pageNumber;
if ($page == 1)
{
    $pageend = $pageNumber;
}
else
{
    $pageend = $page + $pageNumber;
}
if ($pagebegin <= 0)
{
    $pagebegin = 1;
}
if ($pageend > $totalPage)
{
    $pageend = $totalPage;
}
//一次向前翻$pageNumber行
if ($page > $pageNumber)
{
    echo '<span><a href="?page=' . ($page - $pageNumber) . '"><<</a></span>';
}
//输出动态生成的页码链接
for ($i = $pagebegin; $i <= $pageend; $i++)
{
    if ($i == $page)
    {
        echo '<span style="background:#FFCC99">' . $i . '</span>';
    }
    else
    {
        echo '<span><a href="?page=' . $i . '">' . $i . '</a></span>';
    }
}
//一次向后翻$pageNumber行
if (($totalPage - $page) > 5)
{
    echo '<span><a href="?page=' . ($page + $pageNumber) . '">>></a></span>';
}
//输出下一页链接
if ($page <> $totalPage)
{
    echo '<span><a href="?page=' . $Next . '">Next</a></span>';
    echo '<span><a href="?page=' . $totalPage . '">Last</a></span>';
}
else
{
    echo '<span>Next</span>';
    echo '<span>Last</span>';
}
oci_close($conn);
$t2 = xdebug_time_index();
echo "<br/><br/>execute time:";
echo $t2-$t1;
echo "s";
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值