一个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树的代码,可以嵌套任意层
Oct 09 PHP
php基础知识:类与对象(1)
Dec 13 PHP
PHP 面向对象详解
Sep 13 PHP
仿Aspnetpager的一个PHP分页类代码 附源码下载
Oct 08 PHP
php smarty模板引擎的6个小技巧
Apr 24 PHP
使用PHP实现阻止用户上传成人照片或者裸照
Dec 25 PHP
PHP-FPM之Chroot执行环境详解
Aug 03 PHP
php 升级到 5.3+ 后出现的一些错误,如 ereg(); ereg_replace(); 函数报错
Dec 07 PHP
PHP Cookei记录用户历史浏览信息的代码
Feb 03 PHP
PHPCMS V9 添加二级导航的思路详解
Oct 20 PHP
使用Zookeeper分布式部署PHP应用程序
Mar 15 PHP
ThinkPHP3.2.3框架实现执行原生SQL语句的方法示例
Apr 03 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实现通用的信用卡验证类
2015/03/24 PHP
PHP程序员简单的开展服务治理架构操作详解(二)
2020/05/14 PHP
javascript的函数、创建对象、封装、属性和方法、继承
2011/03/10 Javascript
JS事件Event元素(兼容IE,Firefox,Chorme)
2012/11/01 Javascript
Jquery创建一个层当鼠标移动到层上面不消失效果
2013/12/12 Javascript
基于JavaScript如何制作遮罩层对话框
2016/01/26 Javascript
javascript中的 object 和 function小结
2016/08/14 Javascript
如何实现json数据可视化详解
2016/11/24 Javascript
jsp 自动编译机制详细介绍
2016/12/01 Javascript
纯JS实现只能输入数字的简单代码
2017/06/21 Javascript
说说node中的可读流和可写流的区别
2018/06/01 Javascript
es6中Promise 对象基本功能与用法实例分析
2020/02/23 Javascript
vue+vuex+axios从后台获取数据存入vuex,组件之间共享数据操作
2020/07/31 Javascript
Vue通过provide inject实现组件通信
2020/09/03 Javascript
python对指定目录下文件进行批量重命名的方法
2015/04/18 Python
Python面向对象编程中关于类和方法的学习笔记
2016/06/30 Python
Python遍历目录中的所有文件的方法
2016/07/08 Python
Flask模板引擎之Jinja2语法介绍
2019/06/26 Python
用Python写一个自动木马程序
2019/09/17 Python
Python 迭代,for...in遍历,迭代原理与应用示例
2019/10/12 Python
Python基于os.environ从windows获取环境变量
2020/06/09 Python
python中四舍五入的正确打开方式
2021/01/18 Python
如何用Python进行时间序列分解和预测
2021/03/01 Python
css3和jquery实现自定义checkbox和radiobox组件
2014/04/22 HTML / CSS
如何避免常见的6种HTML5错误用法
2017/11/06 HTML / CSS
荷兰最大的儿童服装店:The Kids Republic
2019/04/13 全球购物
英国鲜花递送:Blossoming Gifts
2020/07/10 全球购物
销售副总经理岗位职责
2013/12/11 职场文书
树转促学习心得体会
2014/09/10 职场文书
颐和园导游词
2015/01/30 职场文书
经理助理岗位职责
2015/02/02 职场文书
看雷锋电影观后感
2015/06/10 职场文书
PostgreSQL事务回卷实战案例详析
2022/03/25 PostgreSQL
Springboot-cli 开发脚手架,权限认证,附demo演示
2022/04/28 Java/Android
Nginx限流和黑名单配置
2022/05/20 Servers
Android移动应用开发指南之六种布局详解
2022/09/23 Java/Android