php分页基于oracle,一个很好的分页例子(php+oracle)

环境:

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 = "SELECTempno, 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 "empno";

echo "ename";

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

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."";

?>

运行输出如下效果:

f806def7bb5786ba404e9182f33af5f2.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值