一个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网站在线人数统计
Apr 09 PHP
PHP 图片上传实现代码 带详细注释
Apr 29 PHP
php 截取字符串并以零补齐str_pad() 函数
May 07 PHP
基于ubuntu下nginx+php+mysql安装配置的具体操作步骤
Apr 28 PHP
php-cli简介(不会Shell语言一样用Shell)
Jun 03 PHP
领悟php接口中interface存在的意义
Jun 27 PHP
CI框架中集成CKEditor编辑器的教程
Jun 09 PHP
php实现parent调用父类的构造方法与被覆写的方法
Feb 11 PHP
php二维数组合并及去重复的方法
Mar 04 PHP
PHP面向对象五大原则之接口隔离原则(ISP)详解
Apr 04 PHP
基于laravel where的高级使用方法
Oct 10 PHP
Thinkphp 框架扩展之数据库驱动常用方法小结
Apr 23 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网页游戏学习之Xnova(ogame)源码解读(三)
2014/06/23 PHP
PHP利用header跳转失效的解决方法
2014/10/24 PHP
php使用GD创建保持宽高比缩略图的方法
2015/04/17 PHP
PHP.ini安全配置检测工具pcc简单介绍
2015/07/02 PHP
PHP中$_SERVER使用说明
2015/07/05 PHP
PHP基于SimpleXML生成和解析xml的方法示例
2017/07/17 PHP
通过AJAX的JS、JQuery两种方式解析XML示例介绍
2013/09/23 Javascript
jQuery javaScript捕获回车事件(示例代码)
2013/11/07 Javascript
JavaScript异步回调的Promise模式封装实例
2014/06/07 Javascript
JavaScript解析json格式数据简单示例
2014/12/09 Javascript
JS实现图片产生波纹一样flash效果的方法
2015/02/27 Javascript
JS实现滑动菜单效果代码(包括Tab,选项卡,横向等效果)
2015/09/24 Javascript
JavaScript中用let语句声明作用域的用法讲解
2016/05/20 Javascript
node网页分段渲染详解
2016/09/05 Javascript
jQueryUI Datepicker组件设置日期高亮
2016/10/13 Javascript
JS实现的验证身份证及获取地区功能示例
2017/01/16 Javascript
vue中如何引入jQuery和Bootstrap
2017/04/10 jQuery
详解vue.js之props传递参数
2017/12/12 Javascript
详解webpack+express多页站点开发
2017/12/22 Javascript
node作为中间服务层如何发送请求(发送请求的实现方法详解)
2018/01/02 Javascript
mac上配置Android环境变量的方法
2018/07/08 Javascript
Angular ElementRef简介及其使用
2018/10/01 Javascript
浅谈在Vue.js中如何实现时间转换指令
2019/01/06 Javascript
nodejs简单抓包工具使用详解
2019/08/23 NodeJs
JavaScript 面向对象程序设计详解【类的创建、实例对象、构造函数、原型等】
2020/05/12 Javascript
js实现全选和全不选功能
2020/07/28 Javascript
[47:42]Fnatic vs Liquid 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python是怎么被发明的
2020/06/15 Python
python合并多个excel文件的示例
2020/09/23 Python
美国50岁以上单身人士约会平台:SilverSingles
2018/06/29 全球购物
介绍一下MYSQL常用的优化技巧
2012/10/25 面试题
人力资源主管的岗位职责
2014/03/15 职场文书
学习计划书怎么写
2014/09/15 职场文书
2014年党员自我评议总结
2014/09/23 职场文书
JMeter对MySQL数据库进行压力测试的实现步骤
2022/01/22 MySQL
Flutter集成高德地图并添加自定义Maker的实践
2022/04/07 Java/Android