Home >php教程 >php手册 >分页显示Oracle数据库记录的PHP类

分页显示Oracle数据库记录的PHP类

WBOY
WBOYOriginal
2016-06-21 08:59:42882browse

  <?php

/*********************************************
TOracleViewPage v 2.0
日期:2000-9-23

分页显示Oracle数据库记录的类


更新日期:2000-10-19
增加显示TopRecord的功能,允许第一页显示的记录数与其它页不同。

作者:sharetop
email:ycshowtop@21cn.com

***********************************************/
class TOracleViewPage {

var $Table; //表名
var $MaxLine; //每页显示行数

var $LinkId; //数据库连接号
var $Id; //排序参考字段

var $Offset; //记录偏移量
var $Total; //记录总数
var $Number; //本页读取的记录数
var $TopNumber;//读新记录时实际取出的记录数
var $Result; //读出的结果
var $TopResult;//读新记录时的结果

var $TheFirstPage;//特殊指定第一页的链接
var $StartRec; //指定第二页的起始记录号

var $TPages; //总页数
var $CPages; //当前页数

var $TGroup;
var $PGroup; //每页显示的页号个数
var $CGroup;

var $Condition; //显示条件 如:where id='$id' order by id desc
var $PageQuery; //分页显示要传递的参数

//-------------------------------------
// 以下构造函数、析构函数及初始化函数
//-------------------------------------

//构造函数
//参数:表名、最大行数、分页参考的字段、每页显示的页号数

function TOracleViewPage($TB,$ML,$id){
global $offset;

$this->Table=$TB;
$this->MaxLine=$ML;
$this->Id=$id;

$this->StartRec=0;
if(isset($offset)) $this->Offset=$offset;
else $this->Offset=0;

$this->Condition="";
$this->TheFirstPage=NULL;
$this->PageQury=NULL;
}

//初始化
//参数:用户名、密码、数据库
function InitDB($user,$password,$db){
if (PHP_OS == "WINNT") $dllid=dl("php3_oci80.dll");
$this->LinkId = OCILogon($user,$password,$db);
}

//断开
function Destroy(){
OCILogoff($this->LinkId);
}

//-------------------------
// Set 函数
//-------------------------

//设置显示条件
//如:where id='$id' order by id desc
//要求是字串,符合SQL语法(本字串将加在SQL语句后)

function SetCondition($s){
$this->Condition=$s;
}

//设置每组的显示个数
function SetNumGroup($pg){
$this->PGroup=$pg;
}
//设置首页,如无则为NULL
function SetFirstPage($fn){
$this->TheFirstPage=$fn;
}
//设置起始记录,如无则取默认0
function SetStartRecord($org){
$this->StartRec=$org;
}

//设置传递参数
// key参数名 value参数值
// 如:setpagequery("id",$id);如有多个参数要传递,可多次调用本函数。

function SetPageQuery($key,$value){
$tmp[key]=$key; $tmp[value]=$value;
$this->PageQuery[]=$tmp;
}

//--------------------------------
// Get 函数
//--------------------------------

//取记录总数
function GetTotalRec(){

$SQL="SELECT Count(*) AS total FROM ".$this->Table." ".$this->Condition;

$stmt = OCIParse($this->LinkId,$SQL);
$bool = OCIExecute($stmt);
if (!$bool) {
echo "连接失败!";
OCILogoff($this->LinkId);
exit;
}
else {
OCIFetch($stmt);
$this->Total=OCIResult($stmt,1);
}
OCIFreeStatement($stmt);
}

//取总页数、当前页
function GetPage(){
$this->TPages=ceil($this->Total/$this->MaxLine);
$this->CPages=ceil($this->Offset/$this->MaxLine)+1;
}

//取总组数、当前组
function GetGroup() {
$this->TGroup=ceil($this->TPages/$this->PGroup);
$this->CGroup=ceil($this->CPages/$this->PGroup);
}

//--------------------------------
// 工作函数
//--------------------------------

//读取记录
// 主要工作函数,根据所给的条件从表中读取相应的记录
// 返回值是一个二维数组,Result[记录号][字段名]

function ReadList() {

$SQL="SELECT * FROM ".$this->Table." ".$this->Condition." ORDER BY ".$this->Id." DESC";

$stmt = OCIParse($this->LinkId,$SQL);
$bool = OCIExecute($stmt);
if (!$bool) {
echo "连接失败!";
OCILogoff($this->LinkId);
exit;
}
else {
$ncols = OCINumCols($stmt);
for ( $i = 1; $i <= $ncols; $i++ )
$column_name[$i] = OCIColumnName($stmt,$i);
$k=0;

for($j=0;$j<$this->StartRec+$this->Offset;$j++) OCIFetch($stmt);
for($j=0;$j<$this->MaxLine;$j++){
if(OCIFetch($stmt)){
$k++;
for($i=1;$i<=$ncols;$i++)
$temp[$column_name[$i]]=OCIResult($stmt,$i);
$this->Result[]=$temp;
}
else break;
}
$this->Number=$k;

}
OCIFreeStatement($stmt);
return $this->Result;
}

//读最新的记录
//topnum指定要读出的记录数

function ReadTopList($topnum){

$SQL="SELECT * FROM ".$this->Table." ".$this->Condition." ORDER BY ".$this->Id." DESC";

$stmt = OCIParse($this->LinkId,$SQL);
$bool = OCIExecute($stmt);
if (!$bool) {
echo "连接失败!";
OCILogoff($this->LinkId);
exit;
}
else {
$ncols = OCINumCols($stmt);
for ( $i = 1; $i <= $ncols; $i++ )
$column_name[$i] = OCIColumnName($stmt,$i);
$k=0;

for($j=0;$j<$topnum;$j++){
if(OCIFetch($stmt)){
$k++;
for($i=1;$i<=$ncols;$i++)
$temp[$column_name[$i]]=OCIResult($stmt,$i);
$this->TopResult[]=$temp;
}
else break;
}
$this->TopNumber=$k;

}
OCIFreeStatement($stmt);
return $this->TopResult;
}

//---------------------------
// 分页相关
//---------------------------

//显示当前页及总页数
//本函数在GetPage()后调用。
function ThePage() {
echo "第".$this->CPages."页/共".$this->TPages."页";
}

//显示翻页按钮
//此函数要在GetPage()函数之后调用
//显示下页、上页,并加上要传递的参数

function Page() {
$k=count($this->PageQuery);
$strQuery=""; //生成一个要传递参数字串
for($i=0;$i<$k;$i++){
$strQuery.="&".$this->PageQuery[$i][key]."=".$this->PageQuery[$i][value];
}

return $strQuery;
}

function PrePage($strQuery){
$prev=$this->Offset-$this->MaxLine;
if($prev>=0)
echo "<A href=$PHP_SELF?offset=".$prev.$strQuery." class=newslink>上一页</A>";
else if($this->TheFirstPage!=NULL)
echo "<A href=".$this->TheFirstPage." class=newslink>上一页</A>";
else echo "上一页";
}

function NexPage($strQuery){
$next=$this->Offset+$this->MaxLine;
$k=$this->Total-$this->StartRec;
if($next<$k)
echo "<A href=$PHP_SELF?offset=".$next.$strQuery." class=newslink>下一页</A>";
else
echo "下一页";
}

//------------------------------------
// 记录分组
//----------------------------------
//显示分组
function NumPage() {
$first=($this->CGroup-1)*($this->PGroup)+1;
$last=($first+$this->PGroup > $this->TPages)? ($this->TPages+1):($first+$this->PGroup);
$pr=($this->CGroup-2>=0)?( ($this->CGroup-2)*($this->PGroup)+1 ):(-1);
$prev=($pr!=-1)?( ($pr-1)*$this->MaxLine):(0);
$ne=($this->CGroup*$this->PGroup+1<=$this->TPages)?($this->CGroup*$this->PGroup+1):(-1);
$next=($ne!=-1)?( ($ne-1)*$this->MaxLine):(0);

$k=count($this->PageQuery);
$strQuery=""; //生成一个要传递参数字串
for($i=0;$i<$k;$i++){
$strQuery.="&".$this->PageQuery[$i][key]."=".$this->PageQuery[$i][value];
}

if($first!=1)
echo "<A href=$PHP_SELF?offset=".$prev.$strQuery." > << </a>";
for($i=$first;$i<$last;$i++) {
if($this->CPages!=$i){
$current=($i-1)*$this->MaxLine;
echo "<A href=$PHP_SELF?offset=".$current.$strQuery." >".$i."</a> ";
}
else echo "<font color=#e00729>".$i."</font> ";
}
if($ne!=-1)
echo "<A href=$PHP_SELF?offset=".$next.$strQuery." > >> </a>";
}

//******end class
}
?>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn