require_once"DB.php"; class listPage { var $_table; //表名 var $_maxLine; //每页显示行数 var $_db; var $_offset; //记录偏移量 var $total = 0; //记录总数 var $number; //本页读取的记录数 var $result; //读出的结果
var $totalPgs; //总页数 var $currentPgs; //当前页数 var $condition; //显示条件 如:where id='$id' order by id desc var $pageQuery; //分页显示要传递的参数
var $pageLink1;//分页显示链接 var $pageLink2;//分页显示链接
//******构造函数************* //参数:表名、最大行数、偏移量、db对象 //分页参考字段请用一个自动编号字段 function ListPage($tableName,$PgNum,$off=0,$db){ $this->_table=$tableName; $this->_maxLine=$PgNum; $this->_offset=$off; $this->_db=$db; $this->condition=" "; $this->_db->setFetchMode(DB_FETCHMODE_ASSOC); }
//********设置显示条件********* //如:where id='$id' order by id desc //要求是字串string,符合SQL语法(本字串将加在SQL语句后) function setCondition($s){ $this->condition = $s; return true; }
//******设置传递参数************ // key参数名 value参数值 // 如:setpagequery("id",$id);如有多个参数要传递,可多次调用本函数。 function setPageQuery($key,$value){ $tmp['k']=$key; $tmp['value']=$value; $this->pageQuery[]= $tmp; //printr($this->pageQuery); }
//********读取记录*************** // 主要工作函数,根据所给的条件从表中读取相应的记录 // 返回值是一个二维数组,Result[记录号][字段名] function readList() { $SQL = "select * from ".$this->_table." ".$this->condition; $result = $this->_db->query($SQL); $this->total = $result->numRows();//计算总记录数 if($this->total>0) { //根据条件 Condition //起始 $SQL="SELECT * FROM ".$this->_table." ".$this->condition; //if(($this->_offset * $this->_maxLine) > $this->total ) $this->_offset = 0; $result = $this->_db->limitQuery($SQL,$this->_offset,$this->_maxLine); $k=0; $rows = array(); while($rows = $result->fetchRow()){ $this->result[] = $rows; $k++; } $this->number=$k; } return $this->result; }
//**********显示页数************* //显示当前页及总页数 function setPgs() { $this->totalPgs = ceil($this->total / $this->_maxLine); $this->currentPgs = ceil($this->_offset / $this->_maxLine)+1; }
//**********显示翻页按钮************* //此函数要在ThePage()函数之后调用!!! //显示首页、下页、上页、未页,并加上要传递的参数 function setPage() { $this->setPgs(); $first=1; $next=$this->currentPgs+1; $prev=$this->currentPgs-1; $last=$this->totalPgs; $k=count($this->pageQuery); $strQuery=""; //生成一个要传递参数字串 for($i=0;$i < $k;$i++){ $strQuery.="&".$this->pageQuery[$i]['k']."=".$this->pageQuery[$i]['value']; }
$this->pageLink1 = ""; if($this->currentPgs>1 && $this->currentPgs < $this->totalPgs){ $this->pageLink1 .="| <a href=".$_SERVER['PHP_SELF']."?currentpage=".$first.$strQuery.">首页</A> "; $this->pageLink1 .= "<a href=".$_SERVER['PHP_SELF']."?currentpage=".$prev.$strQuery.">上一页 </a> "; $this->pageLink1 .= "| <A href=".$_SERVER['PHP_SELF']."?currentpage=".$next.$strQuery.">下一页</A> "; $this->pageLink1 .= "<A href=".$_SERVER['PHP_SELF']."?currentpage=".$last.$strQuery.">末页</A> |"; } else if($this->currentPgs==1 && $this->currentPgs < $this->totalPgs) { $this->pageLink1 .="| 首页 上一页 | "; $this->pageLink1 .="<A href=".$_SERVER['PHP_SELF']."?currentpage=".$next.$strQuery.">下一页</A> "; $this->pageLink1 .="<A href=".$_SERVER['PHP_SELF']."?currentpage=".$last.$strQuery.">末页</A> |"; } else if($this->currentPgs==$this->totalPgs && $this->totalPgs>1) { $this->pageLink1 .="| <A href=".$_SERVER['PHP_SELF']."?currentpage=".$first.$strQuery.">首页</A> "; $this->pageLink1 .="<a href=".$_SERVER['PHP_SELF']."?currentpage=".$prev.$strQuery.">上一页 </a> "; $this->pageLink1 .="| 下一页 末页 |"; }
for($i=1;$i<=$this->totalPgs;$i++) { $this->pageLink2 .= "<a href=".$_SERVER['PHP_SELF']."?currentpage=".$i.$strQuery.">".$i."</a> "; }
}
} //******end class --------------

|