在网上看到一个PHP分页类,拿下来改了改,可总是有问题,提示也看不出个所以然来,请大家帮忙看看!
提示function Pager($conn, $sql, $pageNo, $pagesize){这一行有错误
代码如下:
<?php
// +----------------------------------------------------------------------
// | 分页类pager.class.php
// +----------------------------------------------------------------------// 禁止直接访问该页面
if (basename($HTTP_SERVER_VARS['PHP_SELF']) == "pager.class.php") {
header("HTTP/1.0 404 Not Found");
}class Pager{
var $infoCount=0;//总信息数
var $pageCount;//总页数
var $pagesize;//每页显示条数
var $pageNo;//当前页码
var $startPos;//查询的起始位置Position
var $nextPageNo;//下一页页码
var $prevPageNo;//上一页页码
var $sql;//要执行的SQL语句
var $conn;//连接对象
function Pager($conn, $sql, $pageNo, $pagesize){
$this->infoCount=$this->getTotalNum();
$this->pagesize=$pagesize;
$this->pageNo=$pageNo;
$this->sql=$sql;
$this->conn=$conn;
$this->pageCount=$this->GetPageCount();
$this->AdjustPageNo();
$this->startPos=$this->GetStartPos();
$this->querySQL();
$this->ShowBar();
}
function getTotalNum(){
$rs=$this->conn->Execute($this->sql);
return $rs->RecordCount();
}
function getArray(){
$this->sql.= " limit ".$this->getLimitStart().",".$this->pagesize;
$rs=$this->conn->Execute($this->sql);
$arr=$rs->GetArray();
return $arr;
}
function getLimitStart(){//SQL里 LIMIT start
return ($this->pageNo-1)*$this->pagesize;
} function AdjustPageNo(){//保证当前页码的有效性
if($this->pageNo == '' || $this->pageNo < 1)
$this->pageNo = 1;
if ($this->pageNo > $this->pageCount)
$this->pageNo = $this->pageCount;
}
function GoToNextPage(){//得到下一页
$nextPageNo=$this->pageNo+1;
if ($nextPageNo>$this->pageCount){
$this->nextPageNo=$this->pageCount;
return false;
}
$this->nextPageNo=$nextPageNo;
return true;
}
function GotoPrevPage(){//得到上一页
$prevPageNo=$this->pageNo-1;
if ($prevPageNo<1){
$this->prevPageNo=1;
return false;
}
$this->prevPageNo=$prevPageNo;
return true;
}
function GetPageCount(){//得到总页数
return ceil($this->infoCount/$this->pagesize);
}
function GetStartPos(){//计算起始记录
return ($this->pageNo-1)*$this->pagesize;
}
function ShowBar(){
if($this->pageCount>0 && $this->pageCount<10){
$this->pageStart=1;
$this->pageEnd=$this->pageCount;
}else{
$this->pageStart=$this->pageNo-5;
$this->pageEnd=$this->pageNo+4;
if($this->pageStart<=0){
$this->pageStart=1;
$this->pageEnd=10;
}
if($this->pageEnd>=$this->pageCount){
$this->pageStart=$this->pageCount-10;
$this->pageEnd=$this->pageCount;
}
}
for($i=$this->pageStart;$i<=$this->pageEnd;$i++){
if($i==$this->pageNo){
$this->pageTmp .= " <font color='#999999'>". $i ."</font> ";
}else{
$this->pageTmp .= " <a href='". $this->pageUrl . $i ."'>". $i ."</a> ";
}
}
$barHtmls = "";
$barHtmls .= "<TABLE width='98%' align='right' cellpadding='0'>";
$barHtmls .= "<TBODY>";
$barHtmls .= "<TR>";
$barHtmls .= "<TD width='250' align='right'>共 ".$this->pageCount."页 ".$this->infoCount."条记录 当前第".$this->pageNo."页 </TD>";
$barHtmls .= "<TD align='right'><a href='".$this->pageUrl."1'>首页</a>";
if($this->pageNo>1){
$barHtmls .= " <a href='".$this->pageUrl . $this->pageNo-1 ."'>上一页</a>";
}else{
$barHtmls .= " <font color='#999999'>上一页</font>";
}
$barHtmls .= $this->pageTmp;
if($this->pageNo<$this->pageCount){
$barHtmls .= " <a href='".$this->pageUrl . $this->pageNo+1 ."'>下一页</a>";
}else{
$barHtmls .= " <font color='#999999'>下一页</font>";
}
$barHtmls .= " <a href='".$this->pageUrl . $this->pageCount ."'>尾页</a> </TD>";
$barHtmls .= "<TD align=right width=75><input name='pageNow' type='text' id='pageNow' size='3' maxlength='4' value='".$this->pageNo."' style='width:25px;text-align: center;' onKeyDown=if(event.keyCode=='13'){location='". $this->pageUrl ."'+document.getElementById('pageNow').value}><input type='button' name='Submit' value='GO' onClick=location='". $this->pageUrl ."'+document.getElementById('pageNow').value></TD>";
$barHtmls .= "</TR>";
$barHtmls .= "</TBODY>";
$barHtmls .= "</TABLE>";
return $barHtmls;
}
}
?>
提示function Pager($conn, $sql, $pageNo, $pagesize){这一行有错误
代码如下:
<?php
// +----------------------------------------------------------------------
// | 分页类pager.class.php
// +----------------------------------------------------------------------// 禁止直接访问该页面
if (basename($HTTP_SERVER_VARS['PHP_SELF']) == "pager.class.php") {
header("HTTP/1.0 404 Not Found");
}class Pager{
var $infoCount=0;//总信息数
var $pageCount;//总页数
var $pagesize;//每页显示条数
var $pageNo;//当前页码
var $startPos;//查询的起始位置Position
var $nextPageNo;//下一页页码
var $prevPageNo;//上一页页码
var $sql;//要执行的SQL语句
var $conn;//连接对象
function Pager($conn, $sql, $pageNo, $pagesize){
$this->infoCount=$this->getTotalNum();
$this->pagesize=$pagesize;
$this->pageNo=$pageNo;
$this->sql=$sql;
$this->conn=$conn;
$this->pageCount=$this->GetPageCount();
$this->AdjustPageNo();
$this->startPos=$this->GetStartPos();
$this->querySQL();
$this->ShowBar();
}
function getTotalNum(){
$rs=$this->conn->Execute($this->sql);
return $rs->RecordCount();
}
function getArray(){
$this->sql.= " limit ".$this->getLimitStart().",".$this->pagesize;
$rs=$this->conn->Execute($this->sql);
$arr=$rs->GetArray();
return $arr;
}
function getLimitStart(){//SQL里 LIMIT start
return ($this->pageNo-1)*$this->pagesize;
} function AdjustPageNo(){//保证当前页码的有效性
if($this->pageNo == '' || $this->pageNo < 1)
$this->pageNo = 1;
if ($this->pageNo > $this->pageCount)
$this->pageNo = $this->pageCount;
}
function GoToNextPage(){//得到下一页
$nextPageNo=$this->pageNo+1;
if ($nextPageNo>$this->pageCount){
$this->nextPageNo=$this->pageCount;
return false;
}
$this->nextPageNo=$nextPageNo;
return true;
}
function GotoPrevPage(){//得到上一页
$prevPageNo=$this->pageNo-1;
if ($prevPageNo<1){
$this->prevPageNo=1;
return false;
}
$this->prevPageNo=$prevPageNo;
return true;
}
function GetPageCount(){//得到总页数
return ceil($this->infoCount/$this->pagesize);
}
function GetStartPos(){//计算起始记录
return ($this->pageNo-1)*$this->pagesize;
}
function ShowBar(){
if($this->pageCount>0 && $this->pageCount<10){
$this->pageStart=1;
$this->pageEnd=$this->pageCount;
}else{
$this->pageStart=$this->pageNo-5;
$this->pageEnd=$this->pageNo+4;
if($this->pageStart<=0){
$this->pageStart=1;
$this->pageEnd=10;
}
if($this->pageEnd>=$this->pageCount){
$this->pageStart=$this->pageCount-10;
$this->pageEnd=$this->pageCount;
}
}
for($i=$this->pageStart;$i<=$this->pageEnd;$i++){
if($i==$this->pageNo){
$this->pageTmp .= " <font color='#999999'>". $i ."</font> ";
}else{
$this->pageTmp .= " <a href='". $this->pageUrl . $i ."'>". $i ."</a> ";
}
}
$barHtmls = "";
$barHtmls .= "<TABLE width='98%' align='right' cellpadding='0'>";
$barHtmls .= "<TBODY>";
$barHtmls .= "<TR>";
$barHtmls .= "<TD width='250' align='right'>共 ".$this->pageCount."页 ".$this->infoCount."条记录 当前第".$this->pageNo."页 </TD>";
$barHtmls .= "<TD align='right'><a href='".$this->pageUrl."1'>首页</a>";
if($this->pageNo>1){
$barHtmls .= " <a href='".$this->pageUrl . $this->pageNo-1 ."'>上一页</a>";
}else{
$barHtmls .= " <font color='#999999'>上一页</font>";
}
$barHtmls .= $this->pageTmp;
if($this->pageNo<$this->pageCount){
$barHtmls .= " <a href='".$this->pageUrl . $this->pageNo+1 ."'>下一页</a>";
}else{
$barHtmls .= " <font color='#999999'>下一页</font>";
}
$barHtmls .= " <a href='".$this->pageUrl . $this->pageCount ."'>尾页</a> </TD>";
$barHtmls .= "<TD align=right width=75><input name='pageNow' type='text' id='pageNow' size='3' maxlength='4' value='".$this->pageNo."' style='width:25px;text-align: center;' onKeyDown=if(event.keyCode=='13'){location='". $this->pageUrl ."'+document.getElementById('pageNow').value}><input type='button' name='Submit' value='GO' onClick=location='". $this->pageUrl ."'+document.getElementById('pageNow').value></TD>";
$barHtmls .= "</TR>";
$barHtmls .= "</TBODY>";
$barHtmls .= "</TABLE>";
return $barHtmls;
}
}
?>
检查输入变量
就是构造函数,在new Pager()时要带上4个参数.
另外,我看了一下。这个类有很多错误,首先构造函数
function Pager($conn, $sql, $pageNo, $pagesize){
$this->infoCount=$this->getTotalNum();
$this->pagesize=$pagesize;
$this->pageNo=$pageNo;
$this->sql=$sql;
$this->conn=$conn;
$this->pageCount=$this->GetPageCount();
$this->AdjustPageNo();
$this->startPos=$this->GetStartPos();
$this->querySQL();
$this->ShowBar();
}
有很大问题。需要修改成
function Pager($conn, $sql, $pageNo, $pagesize){
$this->pagesize=$pagesize;
$this->pageNo=$pageNo;
$this->sql=$sql;
$this->conn=$conn;
$this->infoCount=$this->getTotalNum();
$this->pageCount=$this->GetPageCount();
$this->AdjustPageNo();
$this->startPos=$this->GetStartPos();
$this->querySQL();
$this->ShowBar();
}
include("adodb/adodb.inc.php");
include("adodb/tohtml.inc.php");<?phpclass page{
var $conn;
var $rs;
var $sql;
var $numPage;//每一页显示多少个记录
var $currPage;//当前页
function __construct($conn,$sql,$numPage,$currPage){
$this->conn=$conn;
$this->sql=$sql;
$this->numPage=$numPage;
$this->currPage=$currPage;
}
function getRecordSet(){
$this->rs=$this->conn->PageExecute($this->sql, $this->numPage, $this->currPage);
return $this->rs;
}
function showBar(){
$rs=$this->getRecordSet();
$tempStr="";
if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage())) {
if (!$rs->AtFirstPage()) {
$tempStr.="<a href=";
$tempStr.=$_SERVER['PHP_SELF']."?page=";
$tempStr.=$rs->AbsolutePage() - 1 .">Previous page</a>";
}
if (!$rs->AtLastPage()) {
$tempStr.="<a href=";
$tempStr.=$_SERVER['PHP_SELF']."?page=";
$tempStr.=$rs->AbsolutePage() + 1 .">Next page</a>";
} }
return $tempStr; }
}
if (isset($_GET['page'])) $curr_page = ($_GET['page']);
$pageObj=new page(getConn(),"select id,userName from userdb",1,$curr_page);
$rs=$pageObj->getRecordSet();
rs2htmlWithEdit($rs,'border=1 cellpadding=3',array('操作','用户名'),"modUser.php?","doDel.php?tableId=3&");
print $pageObj->showBar();
?>