CurrentPage类<?php
class CurrentPage{
//basic element
var $totalRecords;
var $currentPage;
var $pageRecords;
var $totalPages;
//extension element
var $currentConn;
var $qSQL;
public function __construct($conn,$pSQL,$pageSize,$pagenumber){
if(is_object($conn)){
$this->currentConn=$conn;
}
$this->qSQL=$pSQL;
$this->pageRecords=$pageSize;
$this->currentPage=$pagenumber;
}
private function getTotalRecords(){
try{
$result=mssql_query($this->qSQL);
if(($currTotal=mssql_num_rows($result))>0){
$this->totalRecords=$currTotal;
}else{
die("connection fail");
exit;
}
}catch(Exception $e){
print $e->getMessage();
}
}
public function getTotalPages(){
if(isset($this->totalRecords) && isset($this->pageRecords)){
if($this->totalRecords % $this->pageRecords ==0){
$this->totalPages=$this->totalRecords / $this->pageRecords;
}else{
$this->totalPages=intval($this->totalRecords / $this->pageRecords +1);
}
}else{
$this->getTotalRecords();
}
return $this->TotalPages;
}
private function getCurrentPage(){
if($this->currentPage<=1){
$this->currentPage=1;
}
if($this->currentPage >= $this->getTotalPages()){
$this->currentPage=$this->getTotalPages();
}
if(!isset($this->currentPage)){
$this->currentPage=1;
}
return $this->currentPage;
} public function showRecordSet(){
$strStart=($this->getCurrentPage()-1) * $this->pageRecords;
$strEnd=$this->getCurrentPage() * $this->pageRecords;
$currArray=array();
$tempResult=mssql_query($this->qSQL);
if(mssql_data_seek($tempResult,$strStart)){
for($i=$strStart;$i<$strEnd;$i++){
$row=mssql_fetch_object($tempResult);
array_push($currArray,$row);
}
return $currArray;
}
}
public function getPageInfo(){
//print_r("connection info: ".$this->currentConn."<br>");
//print_r("call sql statement: ".$this->qSQL."<br>");
print_r("define records: ".$this->pageRecords."<br>");
print_r("construcctor current page: " .$this->currentPage."<br>");
print_r("get current page: " .$this->getCurrentPage()."<br>");
print "total records:{$this->totalRecords}<br>";
print "total pages:{$this->totalPages}<br>";
}
}
?>调用代码: $tSQL="SELECT DISTINCT e1.AccountName AS accountName, e2.AccountID
FROM t_Character e2 INNER JOIN
t_User e1 ON e2.AccountID = e1.AccountID";
$pageRecord=new CurrentPage($connSQLSERVER,$tSQL,$strPageSize,$pageNumber);
print $pageRecord->getPageInfo();
$arrTemp=$pageRecord->showRecordSet();
print $pageRecord->getPageInfo();
for($i=0;$i<count($arrTemp);$i++){
print $arrTemp[$i]->accountName."<br>";
}输出:
connection info:
define records: 100
construcctor current page: 2
get current page: 1
total records:111623
total pages:1117
connection info:
define records: 100
construcctor current page: 1
get current page: 1
total records:111623
total pages:1117
class CurrentPage{
//basic element
var $totalRecords;
var $currentPage;
var $pageRecords;
var $totalPages;
//extension element
var $currentConn;
var $qSQL;
public function __construct($conn,$pSQL,$pageSize,$pagenumber){
if(is_object($conn)){
$this->currentConn=$conn;
}
$this->qSQL=$pSQL;
$this->pageRecords=$pageSize;
$this->currentPage=$pagenumber;
}
private function getTotalRecords(){
try{
$result=mssql_query($this->qSQL);
if(($currTotal=mssql_num_rows($result))>0){
$this->totalRecords=$currTotal;
}else{
die("connection fail");
exit;
}
}catch(Exception $e){
print $e->getMessage();
}
}
public function getTotalPages(){
if(isset($this->totalRecords) && isset($this->pageRecords)){
if($this->totalRecords % $this->pageRecords ==0){
$this->totalPages=$this->totalRecords / $this->pageRecords;
}else{
$this->totalPages=intval($this->totalRecords / $this->pageRecords +1);
}
}else{
$this->getTotalRecords();
}
return $this->TotalPages;
}
private function getCurrentPage(){
if($this->currentPage<=1){
$this->currentPage=1;
}
if($this->currentPage >= $this->getTotalPages()){
$this->currentPage=$this->getTotalPages();
}
if(!isset($this->currentPage)){
$this->currentPage=1;
}
return $this->currentPage;
} public function showRecordSet(){
$strStart=($this->getCurrentPage()-1) * $this->pageRecords;
$strEnd=$this->getCurrentPage() * $this->pageRecords;
$currArray=array();
$tempResult=mssql_query($this->qSQL);
if(mssql_data_seek($tempResult,$strStart)){
for($i=$strStart;$i<$strEnd;$i++){
$row=mssql_fetch_object($tempResult);
array_push($currArray,$row);
}
return $currArray;
}
}
public function getPageInfo(){
//print_r("connection info: ".$this->currentConn."<br>");
//print_r("call sql statement: ".$this->qSQL."<br>");
print_r("define records: ".$this->pageRecords."<br>");
print_r("construcctor current page: " .$this->currentPage."<br>");
print_r("get current page: " .$this->getCurrentPage()."<br>");
print "total records:{$this->totalRecords}<br>";
print "total pages:{$this->totalPages}<br>";
}
}
?>调用代码: $tSQL="SELECT DISTINCT e1.AccountName AS accountName, e2.AccountID
FROM t_Character e2 INNER JOIN
t_User e1 ON e2.AccountID = e1.AccountID";
$pageRecord=new CurrentPage($connSQLSERVER,$tSQL,$strPageSize,$pageNumber);
print $pageRecord->getPageInfo();
$arrTemp=$pageRecord->showRecordSet();
print $pageRecord->getPageInfo();
for($i=0;$i<count($arrTemp);$i++){
print $arrTemp[$i]->accountName."<br>";
}输出:
connection info:
define records: 100
construcctor current page: 2
get current page: 1
total records:111623
total pages:1117
connection info:
define records: 100
construcctor current page: 1
get current page: 1
total records:111623
total pages:1117
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货