<?php
class page{
/*
类功能:分页
创建时间:2005-03-12
作者:多菜鸟
EMAIL:kingerq AT sohu.com
来源:http://blog.csdn.net/kingerq
实例:
//省略一些连接信息及mysql类库等包含文件
$sql = "select count(*) from t_publiccode";
$db->query($sql);
$db->next_record();
$recordcount = $db->f(0);//记录总数
$p = new page($recordcount, 20, 15);//(总记录数,每页记录数,每面页码个数)
$p->ar = array("«首页", "‹前页", "后页›", "末页»");//可以设置成图片HTML代码
$trunpage = $p->show_page();//得到分页信息
$sql = "select * from t_publiccode".$p->limit();
//echo $trunpage."<br>".$sql;
$db->query($sql);
//...
类似效果:
共[52600]条记录/共[2630]页 <<首页 <前页 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 后页> 末页>>
*/
var $pagecount; //总页数
var $limit = ""; //分页时用到的limit参数
var $pagearg = "page"; //分页参数名称
var $pagesize = 20; //每页显示记录数
var $recordcount; //总记录数
var $pagenum; //当前页码
var $argstr = ""; //GET参数字符串
var $perpage = 5; //每次显示页码个数
var $ar = array("[«]", "[‹]", "[›]", "[»]");
/*
* 功能:构造函数
* 参数 $recordcount 为记录总数
* 参数 $pagesize 为每页显示记录数,默认为20
* 参数 $perpage 为每次显示页码个数,默认为5
*/
function page($recordcount, $pagesize = 20, $perpage = 5){
$this->pagesize = $pagesize;
$this->recordcount = $recordcount;
$this->perpage = $perpage;
$this->pagecount = ceil($recordcount/$pagesize);//总页数
$this->pagenum = $this->currentpage();//当前页码
$this->argstr = $this->newarg();//GET参数字符串
$this->limit = " LIMIT ".($this->pagenum-1)*$pagesize.",".$pagesize;//分页时用到的limit参数
}
/*
* 功能:取得当前页码函数
*/
function currentpage(){
if(isset($_GET[$this->pagearg])){
if($_GET[$this->pagearg] <= 0) {
$page = 1;
}else if($_GET[$this->pagearg] > $this->pagecount){
$page = $this->pagecount;
}else{
$page = $_GET[$this->pagearg];
}
}else {
$page = 1;
}
return $page;
}
/*
* 功能:重新整理GET参数
*/
function newarg(){
$str = "";
$urlar = $_GET;
unset($urlar[$this->pagearg]);
if($urlar){
foreach($urlar as $key=>$val){
if($str == "") {
$str = "?$key=$val";
}else {
$str .= "&$key=$val";
}
}
$str .= "&$this->pagearg=";
}else{
$str = "?$this->pagearg=";
}
return $str;
}
/*
* 功能:返回MYSQL语句的limit部分代码
*/
function limit(){
return $this->limit;
}
/*
* 功能:分页字符输出函数
*/
function show_page(){
$trunpage = " 共[".$this->recordcount."]条记录/共[".$this->pagecount."]页 ";
$page = $this->pagenum;
//向前翻
if($page == 1){
$trunpage .= $this->ar[0]." ".$this->ar[1]." ";
}else {
$trunpage .= '<a href="'.$this->argstr.'1">'.$this->ar[0].'</a> <a _fcksavedurl=""'.$this->argstr.'1">'.$this->ar[0].'</a> <a" href="'.$this->argstr.($page-1).'">'.$this->ar[1].'</a> ';
}
//中间数字翻页
$numpage = 1;
while($page - $this->perpage * $numpage > 0) $numpage++;
$startpage = ($numpage - 1) * $this->perpage;
$endpage = $this->pagecount < $numpage * $this->perpage ? $this->pagecount : $numpage * $this->perpage;
for($i = $startpage + 1; $i <= $endpage; $i++){
$trunpage .= $i != $page ? "<a href=/"".$this->argstr.$i."/">$i</a> " : "<strong>".$i."</strong> ";
}
//向后翻
if($page == $this->pagecount || $this->pagecount == 0){
$trunpage .= $this->ar[2]." ".$this->ar[3]." ";
}else {
$trunpage .= '<a href="'.$this->argstr.($page+1).'">'.$this->ar[2].'</a> <a href="'.$this->argstr.$this->pagecount.'">'.$this->ar[3].'</a> ';
}
//跳转
$select = "<select οnchange='location.href=this.options[this.selectedIndex].value'>/n";
for($i = 1; $i <= $this->pagecount; $i++){
$select .= "<option value=/"".$this->argstr.$i."/">$i</option>/n";
}
$select .= "</select>";
return $trunpage.$select;
}
}
?>
<?php
/**********************
* 通用分页类
* 作者: 多菜鸟
* 邮箱: kingerq AT sohu DOT com
* 时间: 2006-04-17
* 来源:http://blog.csdn.net/kingerq
* 实例:
$p = new Page( 60, 10 );
$p->trunStr = array( "First", "Prev", "Next", "End" );//设置分页显示字符
echo $p->printPage();//输出分页字符串,也可以trunUp(),trunNum(),trunDown()和jumpTo()分开来输出显示
//echo $p->limitStr();//显示MYSQL中LIMIT部分字符串
//echo $p->pageCount();//总页数
**********************
*/
class Page {
var $perPage = 5; //每页显示的页码数
var $paramStr = ""; //分页参数字符串变量,连接地址
var $currentPage = 1; //当前页码
var $paramName = "pageNum"; //翻页参数名称
var $limitStr = ""; //MYSQL中LIMIT参数字符串
var $pageSize = 20; //每页记录数
var $trunStr = array( "首页", "上页", "下页", "末页" ); //翻页字符
/*
* 构造函数
* $recordCount 为记录总数量
* $pageSize 为每页显示记录数
*/
function Page( $recordCount = 0, $pageSize = 0 ) {
if( ! is_numeric( $recordCount ) || ! is_numeric( $pageSize ) ) return;
//记录总数
if( intval( $recordCount ) == 0 )
$recordCount = $this->recordCount;
else
$this->recordCount = $recordCount;
//每页显示记录数
if( intval( $pageSize ) == 0 )
$pageSize = $this->pageSize;
else
$this->pageSize = $pageSize;
$this->currentPage();//当前页码处理
$this->paramStr();//当前页地址参数
//为当前页定界
if( $this->currentPage > $this->pageCount() )
$this->currentPage = $this->pageCount() < 1 ? 1 : $this->pageCount();
}
function printPage() {
$pageStr = $this->trunUp(); //向上
$pageStr .= $this->trunNum(); //数字页码
$pageStr .= $this->trunDown(); //向下
$pageStr .= $this->jumpTo(); //跳转
return $pageStr;
}
//向上翻处理
function trunUp() {
$pageStr = "";
if( $this->currentPage == 1 ) {
$pageStr .= $this->trunStr[0] . " " . $this->trunStr[1] . " ";
}else {
$pageStr .= "<a href=/"".$this->paramStr."1/" title=/"".$this->trunStr[0]."/">".$this->trunStr[0]."</a> ";
$pageStr .= "<a href=/"".$this->paramStr.($this->currentPage - 1)."/" title=/"".$this->trunStr[1]."/">".$this->trunStr[1]."</a> ";
}
return $pageStr;
}
//每页显示的页码连接
function trunNum() {
$pageStr = "";
$num = 1;
while( $this->currentPage - $this->perPage * $num > 0 ) $num++;
$start = ( $num - 1 ) * $this->perPage;
$end = $this->pageCount() < $this->perPage * $num ? $this->pageCount() : $this->perPage * $num;
for( $i = $start + 1; $i <= $end; $i++ ) {
$pageStr .= $i != $this->currentPage ? "<a href=/"".$this->paramStr."$i/">$i</a> " : "<strong>$i</strong> ";
}
return $pageStr;
}
//向下翻处理
function trunDown() {
$pageStr = "";
if( $this->pageCount() == $this->currentPage || $this->pageCount() == 0 ) {
$pageStr .= $this->trunStr[2] . " " . $this->trunStr[3] . " ";
} else {
$pageStr .= "<a href=/"".$this->paramStr.($this->currentPage + 1)."/" title=/"".$this->trunStr[2]."/">".$this->trunStr[2]."</a> ";
$pageStr .= "<a href=/"".$this->paramStr.$this->pageCount()."/" title=/"".$this->trunStr[3]."/">".$this->trunStr[3]."</a> ";
}
return $pageStr;
}
//跳转
function jumpTo() {
$pageStr = "<select οnchange=/"location.href=this.options[this.selectedIndex].value/">/n";
for($i = 1; $i <= $this->pageCount(); $i++){
if($this->currentPage == $i) $selected = " selected";
else $selected = "";
$pageStr .= "<option value='".$this->paramStr.$i."'$selected>$i</option>/n";
}
$pageStr .= "</select>";
return $pageStr;
}
//当前页参数
function paramStr() {
unset( $_GET[$this->paramName] );//去掉翻页参数
if( count( $_GET ) ) {
foreach( $_GET AS $param => $value ) {
$this->paramStr .= $this->paramStr ? "&" : "?";
$this->paramStr .= $param . "=" . $value;
}
$this->paramStr .= "&".$this->paramName."=";
} else {
$this->paramStr = "?$this->paramName=";
}
}
//取得当前页码
function currentPage() {
if( isset( $_GET[$this->paramName] ) && intval( $_GET[$this->paramName] ) > 0 ) {
$this->currentPage = intval( $_GET[$this->paramName] );
}
return $this->currentPage;
}
//总页数
function pageCount() {
return ceil( $this->recordCount / $this->pageSize );//总页数
}
//MYSQL中LIMIT部分字符串
function limitStr() {
return " LIMIT " . ( ( $this->currentPage - 1 ) * $this->pageSize ) . ", " . $this->pageSize;
}
//设置每页显示页码数,页码数至少大于1
function setPerPage($num = 5) {
if( intval($num) > 1 )
$this->perPage = intval($num);
}
}
?>