脚踏实地、勇往直前!
全部博文(1005)
分类: Python/Ruby
2013-01-10 19:59:00
环境:
OS:ORACLE 10GR2
PHP5
在网上到处搜索到了一个比较好的分页例子,适当做了修改.
$page=isset($_GET['page'])?intval($_GET['page']):1;
$display_rows = 5;
$OracleDBConn = OCILogon("hxl","hxl","hxl");
$sql_count = "select count(*) from scott.emp";
$row_count=OCIParse($OracleDBConn, $sql_count);
OCIExecute($row_count);
if (OCIFetch($row_count)) {
$num_rows = OCIResult($row_count,1);
}
else {
$num_rows = 0;
}
OCIFreeStatement($row_count);
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;
$sql = "SELECT empno, ename, ROW_NO FROM (SELECT ";
$sql .= "empno, ename, ROWNUM ROW_NO FROM (SELECT ";
$sql .= "empno,ename FROM scott.emp ORDER BY empno)) WHERE ROW_NO BETWEEN ";
$sql .= $start_range." AND ".$end_range;
// start results formatting
echo " echo " echo " echo " echo " 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 " } echo " } } else { // Print a message stating that no records was found echo " } // Close the table echo "";
";";
";empno ";
ename ";
row_no ";
";
";$column_value ";
";Sorry! No records was found
// 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"; 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."
?>