<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
$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 "
PK ID | ";Field 1 | ";Field 2 | ";Field 3 | ";Row No | ";
// free resources and close connection
OCIFreeStatement($rs);
OCILogoff($OracleDBConn);
?>
// 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";
}
?>
echo "
Total pages: ".$total_pages."
";Number of records: ".$num_rows."
";The SQL Query is: ".$sql."
";?>