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

<!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>

时间: 2024-10-03 20:05:44

一个ORACLE分页程序,挺实用的.的相关文章

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

<!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="?&qu

一个ORACLE分页程序

oracle|程序|分页 <!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" CONTE

blog的魅力,解决我一个oracle的小问题:)

oracle|解决|问题 今天遇到一个oracle的问题,总怀疑是hibernate的查询语句有错,因为提示connot execute query.并且同时出现ORA-00600错误,网上找到:http://blogsite.3322.org:8080/jspwiki/Wiki.jsp?page=Main&weblog.days=30&weblog.startDate=310503oracle9i的驱动与8i有区别 原有的class12.zip不能继续使用在9i当中. 会产生 java.

update-这是一个oracle计税公式,运行时出错missing right parenthesis

问题描述 这是一个oracle计税公式,运行时出错missing right parenthesis update 表名 t set 扣税= case (t.计税工资) when ((t.计税工资 <= 1500) and (t.计税工资 > 0)) then t.计税工资*0.03 when ((t.计税工资 <= 4500) and (t.计税工资 > 1500)) then t.计税工资*0.1-105 when ((t.计税工资 <= 9000) and (t.计税工

语句-关于一个oracle 的sql 查询

问题描述 关于一个oracle 的sql 查询 哪位大神能帮我看看这个语句怎么写 有区间[i1,i2]与bgpprefix 表中的nstartip 和nstopip 比对,查询能使(i1>=nstartip ,且i2<=nstopip),若存在多个,取nstopip-nstartip 最小的那一项,要求必须是查完要求的区间,在从区间中用min()函数找到nstopip-nstartip的那一项 要求效率要高,各位大神帮个忙啊 解决方案 SELECT TOP 1 nstartip, nstopi

迁移 oracle10g rac-您好,请教一个ORACLE跨OS数据迁移问题

问题描述 您好,请教一个ORACLE跨OS数据迁移问题 PLATFORM_ID PLATFORM_NAME ENDIAN_FORMAT 6 AIX-Based Systems (64-bit) Big 18 IBM Power Based Linux Big 2 Solaris[tm] OE (64-bit) Big 4 HP-UX IA (64-bit) Big 16 Apple Mac OS Big 1 Solaris[tm] OE (32-bit) Big 9 IBM zSeries Ba

oracle 连接-请教一个oracle数据库连接的问题

问题描述 请教一个oracle数据库连接的问题 本菜从来没有接触过oracle,最近需要连接oracle数据库,于是从orale官网下载了带有jdk版本的sqldeveloper : Windows 64-bit - zip file includes the JDK 7. 下载后直接运行:sqldeveloper.exe 打开了连接界面,但是连不上数据库. 请问各位大神,我是不是需要作什么配置或者还需要安装其它东西才可以使用sqldeveloper? 解决方案 起码数据库ip端口,用户名,密码

oracle拉格朗日-哪位大神给写一个oracle中关于拉格朗日插值法的函数??大神 求指教啊

问题描述 哪位大神给写一个oracle中关于拉格朗日插值法的函数??大神 求指教啊 哪位大神给写一个oracle中关于拉格朗日插值法的函数??大神 求指教啊

sql oracle 数据库-一个ORACLE字段包含多个ID如何解决

问题描述 一个ORACLE字段包含多个ID如何解决 表1: +------------+--------------+-----------------+ | product_id | product_name | product_type_id | +------------+--------------+-----------------+ | 1 | 产品A | 1,2 | | 2 | 产品B | 2,3 | +------------+--------------+-----------