一个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
IStream与TStream之间的相互转换
Aug 01 PHP
php中json_encode处理gbk与gb2312中文乱码问题的解决方法
Jul 10 PHP
php中将一段数据存到一个txt文件中并显示其内容
Aug 15 PHP
PHP中浮点数计算比较及取整不准确的解决方法
Jan 09 PHP
php中instanceof 与 is_a()区别分析
Mar 03 PHP
php超快高效率统计大文件行数
Jul 05 PHP
详解PHP中的Traits
Jul 29 PHP
浅谈关于PHP解决图片无损压缩的问题
Sep 01 PHP
laravel获取不到session的三种解决办法【推荐】
Sep 16 PHP
PHP进阶学习之类的自动加载机制原理分析
Jun 18 PHP
php实现获取近几日、月时间示例
Jul 06 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 class类的用法详细总结
2013/10/17 PHP
ThinkPHP模板比较标签用法详解
2014/06/30 PHP
prototype 源码中文说明之 prototype.js
2006/09/22 Javascript
js操作textarea 常用方法总结
2012/12/03 Javascript
SOSO地图API使用(一)在地图上画圆实现思路与代码
2013/01/15 Javascript
利用js实现在浏览器状态栏显示访问者在本页停留的时间
2013/12/29 Javascript
使用forever管理nodejs应用教程
2014/06/03 NodeJs
JavaScript实现简单图片滚动附源码下载
2014/06/17 Javascript
javascript面向对象之对象的深入理解
2015/01/13 Javascript
javascript实现网站加入收藏功能
2015/12/16 Javascript
jquery实现具有收缩功能的垂直导航菜单
2016/02/16 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
vue.js 微信支付前端代码分享
2018/02/10 Javascript
JS原型与继承操作示例
2019/05/09 Javascript
vue中英文切换实例代码
2020/01/21 Javascript
Vue实现小购物车功能
2020/12/21 Vue.js
Python使用matplotlib和pandas实现的画图操作【经典示例】
2018/06/13 Python
对python内置map和six.moves.map的区别详解
2018/12/19 Python
Python函数装饰器实现方法详解
2018/12/22 Python
python+opencv实现阈值分割
2018/12/26 Python
python scp 批量同步文件的实现方法
2019/01/03 Python
Python-Flask:动态创建表的示例详解
2019/11/22 Python
python正则过滤字母、中文、数字及特殊字符方法详解
2020/02/11 Python
tensorflow基于CNN实战mnist手写识别(小白必看)
2020/07/20 Python
css3一个简易的 LED 数字时钟实现方法
2020/01/15 HTML / CSS
香港网上花店:FlowerAdvisor香港
2019/05/30 全球购物
应届大学毕业生找工作的求职信范文
2013/11/29 职场文书
毕业生就业自荐书
2013/12/15 职场文书
高中地理教学反思
2014/01/29 职场文书
民事授权委托书范文
2014/08/02 职场文书
奥巴马上海演讲稿
2014/09/10 职场文书
收入证明范本
2015/06/12 职场文书
金榜题名主持词
2015/07/02 职场文书
《称赞》教学反思
2016/02/17 职场文书
2016小学优秀教师先进事迹材料
2016/02/26 职场文书
Python捕获、播放和保存摄像头视频并提高视频清晰度和对比度
2022/04/14 Python