一个ORACLE分页程序,挺实用的.


Posted in PHP onOctober 09, 2006

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Paging Test</TITLE>
<META NAME="Generator" CONTENT="TextPad 4.0">
<META NAME="Author" CONTENT="?">
<META NAME="Keywords" CONTENT="?">
<META NAME="Description" CONTENT="?">
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<?php

// How to split the result into pages, like 'limits' in MySQL?
// ===========================================================
// Tutorial by Neil Craig (neilc@netactive.co.za)
// Date: 2001-06-05
// With this example, I will explain paging of database queries where the
// result is more than the developer want to print to the page, but wish to
// split the result into seperate pages.
// The table "SAMPLE_TABLE" accessed in this tutorial has 4 fields:
// PK_ID, FIELD1, FIELD2 and FIELD3. The types don't matter but you should
// define a primary key on the PK_ID field.

$display_rows = 5;     // The rows that should be display at a time. You can
                       // modify this if you like.

// Connect to the Oracle database
putenv("ORACLE_SID=purk");
putenv("ORACLE_HOME=/export/oracle8i");
putenv("TNS_ADMIN=$ORACLE_HOME/network/admin");
$OracleDBConn = OCILogon("purk","purk","lengana.world");

// This query counts the records
$sql_count = "SELECT COUNT(*) FROM SAMPLE_TABLE";

// Parse the SQL string & execute it
$row_count=OCIParse($OracleDBConn, $sql_count);       
OCIExecute($row_count);

// From the parsed & executed query, we get the amount of records found.
// I'm not storing this result into a session variable because it allows for
// new records to be shown as it is entered by another user while the result
// is printed.
if (OCIFetch($row_count)) {
    $num_rows = OCIResult($row_count,1);
} else {
    $num_rows = 0;        // If no record was found
}

// Free the resources that were used for this query
OCIFreeStatement($row_count);

// We need to prepare the query that will print the results as a page. I will
// explain the query to you in detail.

// If no page was specified in the url (ex. http://mysite.com/result.php?page=2),
// set it to page 1.
if (empty($page) || $page == 0) {
    $page = 1;
}

// The start range from where the results should be printed
$start_range = (($page - 1) * $display_rows) + 1;

// The end range to where the results should be printed
$end_range = $page * $display_rows;

// The main query. It consists of 3 "SELECT" statements nested into each
// other. The center query is the query you would normally use to return the
// records you want. Do you ordering and "WHERE" clauses in this statement.
// We select the rows to limit our results but because the row numbers are
// assigned to the rows before any ordering is done, lets the code print the
// result unsorted.
// The second nested "SELECTED" assigns the new row numbers to the result
// for us to select from.

$sql = "SELECT PK_ID, FIELD1, FIELD2, FIELD3, ROW_NO FROM (SELECT PK_ID, ";
$sql .= "FIELD1, FIELD2, FIELD3, ROWNUM ROW_NO FROM (SELECT PK_ID, FIELD1, ";
$sql .= "FIELD2, FIELD3 FROM SAMPLE_TABLE ORDER BY FIELD3)) WHERE ROW_NO BETWEEN ";
$sql .= $start_range." AND ".$end_range;

// start results formatting
echo "<table width='95%' border='1' cellspacing='1' cellpadding='2' align='center'>";
echo "<tr bgcolor='#666666'>";
echo "<td><b><font color='#FFFFFF'>PK ID</font></b></td>";
echo "<td><b><font color='#FFFFFF'>Field 1</font></b></td>";
echo "<td><b><font color='#FFFFFF'>Field 2</font></b></td>";
echo "<td><b><font color='#FFFFFF'>Field 3</font></b></td>";
echo "<td><b><font color='#FFFFFF'>Row No</font></b></td>";
echo "</tr>";

if ($num_rows != 0) {

    // Parse the SQL string & execute it
    $rs=OCIParse($OracleDBConn, $sql);       
    OCIExecute($rs);

    // get number of columns for use later
    $num_columns = OCINumCols($rs);

    while (OCIFetch($rs)){
        echo "<tr>";
        for ($i = 1; $i < ($num_columns + 1); $i++) {
            $column_value = OCIResult($rs,$i);
            echo "<TD>$column_value</TD>";
        }
        echo "</tr>";
    }

} else {

    // Print a message stating that no records was found
    echo "<tr><td align=center>Sorry! No records was found</td></tr>";
}

// Close the table
echo "</TABLE>";

// free resources and close connection
OCIFreeStatement($rs);
OCILogoff($OracleDBConn);

?>
<div align=center>
<?php

// Here we will print the links to the other pages

// Calculating the amount of pages
if ($num_rows % $display_rows == 0) {
    $total_pages = $num_rows / $display_rows;
} else {
    $total_pages = ($num_rows / $display_rows) + 1;
    settype($total_pages, integer); // Rounding the variable
}

// If this is not the first page print a link to the previous page
if ($page != 1) {
    echo "<a href='".$PHP_SELF."?page=".($page - 1)."'>Previous</a>";
}

// Now we can print the links to the other pages
for ($i = 1; $i <= $total_pages;  $i++) {
    if ($page == $i){
        // Don't print the link to the current page
        echo " ".$i;
    } else {
        //Print the links to the other pages
        echo " <a href='".$PHP_SELF."?page=".$i."'>".$i."</a>";
    }
}

// If this is not the last page print a link to the next page
if ($page < $total_pages) {
    echo " <a href='".$PHP_SELF."?page=".($page + 1)."'>Next</a>";
}

?>
</div>
<?php

// I'm just adding this section to print some of the variables for extra info
// and some debugging

echo "<p><b>Total pages: </b>".$total_pages."</p>";
echo "<p><b>Number of records: </b>".$num_rows."</p>";
echo "<p><b>The SQL Query is:</b> ".$sql."</p>";

?>
</BODY>
</HTML>

PHP 相关文章推荐
PHP语法速查表
Dec 06 PHP
php 保留小数点
Apr 21 PHP
PHP正确配置mysql(apache环境)
Aug 28 PHP
JSON在PHP中的应用介绍
Sep 08 PHP
PHP使用JSON和将json还原成数组
Feb 12 PHP
php实现的一个简单json rpc框架实例
Mar 30 PHP
php验证码生成代码
Nov 11 PHP
优化WordPress中文章与评论的时间显示
Jan 12 PHP
基于PHP的加载类操作以及其他两种魔术方法的应用实例
Aug 28 PHP
PHP多维数组排序array详解
Nov 21 PHP
Thinkphp 5.0实现微信企业付款到零钱
Sep 30 PHP
laravel5.1框架基础之Blade模板继承简单使用方法分析
Sep 05 PHP
通过ICQ网关发送手机短信的PHP源程序
Oct 09 #PHP
搜索引擎技术核心揭密
Oct 09 #PHP
输出控制类
Oct 09 #PHP
提取HTML标签
Oct 09 #PHP
如何把PHP转成EXE文件
Oct 09 #PHP
一个查看session内容的函数
Oct 09 #PHP
一个显示天气预报的程序
Oct 09 #PHP
You might like
PHP数字和字符串ID互转函数(类似优酷ID)
2014/06/30 PHP
Thinkphp调用Image类生成缩略图的方法
2015/03/07 PHP
PHP创建单例后台进程的方法示例
2017/05/23 PHP
PHP实现创建一个RPC服务操作示例
2020/02/23 PHP
ExtJS 2.0实用简明教程 之ExtJS版的Hello
2009/04/29 Javascript
js跨域问题之跨域iframe自适应大小实现代码
2010/07/17 Javascript
输入自动提示搜索提示功能的使用说明:sugggestion.txt
2013/09/02 Javascript
jquery实现焦点图片随机切换效果的方法
2015/03/12 Javascript
jquery实现简单手风琴菜单效果实例
2015/06/13 Javascript
WEB前端开发都应知道的jquery小技巧及jquery三个简写
2015/11/15 Javascript
jquery捕捉回车键及获取checkbox值与异步请求的方法
2015/12/24 Javascript
判断是否存在子节点的实现代码
2016/05/18 Javascript
bootstrap警告框示例代码分享
2017/05/17 Javascript
SpringMVC+bootstrap table实例详解
2017/06/02 Javascript
node koa2实现上传图片并且同步上传到七牛云存储
2017/07/31 Javascript
详解如何使用webpack+es6开发angular1.x
2017/08/16 Javascript
vue滚动固定顶部及修改样式的实例代码
2019/05/30 Javascript
vue 框架下自定义滚动条(easyscroll)实现方法
2019/08/29 Javascript
vue+ts下对axios的封装实现
2020/02/18 Javascript
解决vue单页面多个组件嵌套监听浏览器窗口变化问题
2020/07/30 Javascript
Python优先队列实现方法示例
2017/09/21 Python
单链表反转python实现代码示例
2018/02/08 Python
python 根据网易云歌曲的ID 直接下载歌曲的实例
2019/08/24 Python
使用django和vue进行数据交互的方法步骤
2019/11/11 Python
基于pandas向csv添加新的行和列
2020/05/25 Python
Python库安装速度过慢解决方案
2020/07/14 Python
cosme官方海外旗舰店:日本最大化妆品和美容产品的综合口碑网站
2017/01/18 全球购物
澳大利亚首屈一指的鞋类品牌:Tony Bianco
2018/03/13 全球购物
波兰电子产品购物网站:Vobis
2019/05/26 全球购物
大学生在校学习的自我评价
2014/02/18 职场文书
互联网电子商务专业毕业生求职信
2014/03/18 职场文书
报关报检委托书
2014/04/08 职场文书
工伤事故证明
2014/10/20 职场文书
2015秋季开学典礼演讲稿
2015/07/16 职场文书
2016国培研修心得体会
2016/01/08 职场文书
Python 处理表格进行成绩排序的操作代码
2021/07/26 Python