php用mysql实现分页的原理
/*
PHP控制MYSQL分页显示的类
*/
/*
*数据库分页显示的类
*/
class Page_class {
var $TotalPages, $CurrentPage, $NumPerPage, $QueryStr, $Result, $DBID,$Data;
var $TotalRecords, $StartRecord, $EndRecord, $RowCount ;
function Setup ($NumPerPage) {
$this->NumPerPage = $NumPerPage;
}
function InitPage ($QueryStr,$ID) {
$this->QueryStr = $QueryStr;
$this->DBID = $ID;
$Query = "select COUNT(*) ".strstr($QueryStr,"from");
$this->Result = @mysql_query($Query,$this->DBID) or die("内部错误,请联系管理员");
$this->Data = @mysql_fetch_array($this->Result);
$this->TotalRecords = $this->Data[0];
if ($this->NumPerPage > $this->TotalRecords) {
$this->TotalPages = 1 ;
}
else $this->TotalPages = Ceil($this->TotalRecords/$this->NumPerPage) ;
}
function GetPage ($CurrentPage) {
if ($CurrentPage > $this->TotalPages || $CurrentPage < 1) {
return("错误,没有此页");
}
else {
$this->CurrentPage = $CurrentPage ;
$this->StartRecord = ($this->CurrentPage - 1)*$this->NumPerPage;
$this->EndRecord = $this->CurrentPage * $this->NumPerPage;
$Query = $this->QueryStr." LIMIT ".$this->StartRecord.",".$this->NumPerPage;
$this->Result = @mysql_query($Query,$this->DBID) or die("查询语句出错,请检查语法");
$this->RowCount = @mysql_num_rows($this->Result);
$this->StartRecord += 1;
return($Query);
}
}
function GetData ($Row) {
@mysql_data_seek($this->Result,$Row) or die("没有这一行数据");
$this->Data = @mysql_fetch_array($this->Result) or die("没有这一行数据");
}
}
?>