欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-14 12:02:02
<...
<!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>