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

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

更新时间:2006年10月09日 00:00:00   作者:

Paging Test

// 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 "

echo "

";

echo "

PK ID";

echo "

Field 1";

echo "

Field 2";

echo "

Field 3";

echo "

Row No";

echo "

";

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 "

";

for ($i = 1; $i < ($num_columns + 1); $i++) {

$column_value = OCIResult($rs,$i);

echo "

$column_value";

}

echo "

";

}

} else {

// Print a message stating that no records was found

echo "

Sorry! No records was found";

}

// Close the table

echo "

";

// free resources and close connection

OCIFreeStatement($rs);

OCILogoff($OracleDBConn);

?>

// 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 "Previous";

}

// 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 " ".$i."";

}

}

// If this is not the last page print a link to the next page

if ($page < $total_pages) {

echo " Next";

}

?>

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

// and some debugging

echo "

Total pages:".$total_pages."

";

echo "

Number of records:".$num_rows."

";

echo "

The SQL Query is: ".$sql."

";

?>

相关文章

1a1b05c64693fbf380aa1344a7812747.png

第十二节 类的自动加载 [12]...2006-10-10

4f55910a645b073bc4fc65dc10dc14bd.png

我开始系统的学习php啦,不是改别人的php程序了...学好基础先...

这里推荐大家看中原大学的php视频教程,看完后再根据php手册学习...2008-06-06

0ea3c7666119d5615e582f823fb3fad6.png

今天小编就为大家分享一篇关于PHP的PDO事务与自动提交,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-01-01

4f96a78db829b1556ff16de21e013c7a.png

PHP_MySQL教程-第三天 基本函数...2007-03-03

8cc1031babc6aff2319f1c6af8544aa0.png

如果一行代码搞不定你要的功能,该怎么办?2010-11-11

0c932a99bb7b6f23c937db507070cc7b.png

做一个有下拉功能的留言版...2006-10-10

cca732bf65a93ed2ec0ac80c638460fe.png

PHP面向对象的使用教程 简单数据库连接...2006-11-11

2d9f31f2af7b675a3d153d2b7f1035a7.png

PHP:风雨欲来 路在何方?...2006-10-10

b452cee8ec5cd9e58ab98eba17281e59.png

VML绘图板②脚本--VMLgraph.js、XMLtool.js...2006-10-10

f4838ec7e2d4da28e0b57d4e852dadd4.png

PHP入门速成(1)...2006-10-10

最新评论