PHP经典分页class
来源:
互联网
日期:2006-6-12
/** * 获取分页方法 * $p = new Page(); * $set = $p -> Set(10,@$_GET['page']); * $page = $set[1]; * $sql = MySQL::Select('bluepoint_admin','*','','id','1',$set[2],$set[0]); * while ($rs = mysql_fetch_object($sql)) { * echo $rs -> username.' '.$rs -> password.'<br>'; * } * echo $p -> ShowPage($p -> RsNum('bluepoint_admin',''),$set[0]); */ class Page{ var $psize = 10; //每页显示记录类 var $pernum = 5; //页码偏移量 function get($number,$psize){ $i = 0; foreach($_GET as $k => $v) { $i++; $str = ($i==1) ? '?' : '&'; @$this -> varstr = ($k <> 'page') ? @$this -> varstr.$str.$k.'='.$v : @$this -> varstr; } @$this -> varstr = @$this -> varstr ? @$this -> varstr.'&' : '?'; $totalpage = ceil($number / $psize); $this -> page = (isset($_GET['page'])&&($_GET['page'] <= $totalpage))?intval($_GET['page']):1; } function total($number){ $this -> tpage = ceil($number / $this -> psize); } function RsNum($Table_Name,$Condition){ $Fields = array('id'); $result = MySQL::Select($Table_Name,$Fields,$Condition,'id','0','0','0'); $num = mysql_num_rows($result); return $num; } function Set($psize,$getpage){ $page = intval($getpage) ? intval($getpage) : 1; $start = ($page - 1) * $psize; $str = array($psize,$page,$start); return $str; } function ShowPage($number = 0,$psize = 0){ $this -> psize = $psize ? $psize:$this -> psize; $this -> get($number,$psize); $this -> total($number); $setpage = $this -> page ? ceil($this -> page / $this -> pernum) : 1; $pagenum = ($this -> tpage > $this -> pernum) ? $this -> pernum : $this -> tpage; $text = ""; if ($this -> page > 1) { $text .= "<a title=\"第一页\" href=\"".@$this -> varstr."page=1\">[1]</a>..."; } if ($setpage > 1) { $lastsetid = ($setpage - 1) * $this -> pernum; $text .= "<a title=\"上一页\" href=\"".@$this -> varstr."page=".$lastsetid."\">[<<]</a>"; } if ($this -> page > 1) { $pre = $this -> page - 1; $text .= '<a title="上一页" href="'.@$this -> varstr.'page='.$pre.'">[<]</a>'; } $i = ($setpage - 1) * $this -> pernum; for($j = $i; $j < ($i + $pagenum) && $j < $this -> tpage; $j++) { $newpage = $j + 1; if ($this -> page == $j + 1) { $text .= '<b>['.($j + 1).']</b>'; } else { $text .= '<a href="'.@$this -> varstr.'page='.$newpage.'">['.($j + 1).']</a>'; } } if ($this -> page < $this -> tpage){ $next = $this -> page + 1; $text .= '<a title="下一页" href="'.@$this -> varstr.'page='.$next.'">[>]</a>'; } if ($setpage < @$this -> pers) { $nextpre = $setpage*($this -> pernum + 1); $text .= '<a title="下一页" href="'.@$this -> varstr.'page='.$nextpre.'">[>>]</a>'; [转自:飞腾设计网 http://www.feitec.com]
} if ($this -> page < $this -> tpage) { $text .= '...<a title="最后一页" href="'.@$this -> varstr.'page='.$this -> tpage.'">['.$this -> tpage.']</a>'; } $text = '共有'.$number.'条记录,每页'.$psize.'条记录 '.$text; return $text; } } [转自:飞腾设计网 http://www.feitec.com]
需要说明的是: $sql = MySQL::Select('bluepoint_admin','*','','id','1',$set[2],$set[0]);是本人写的一个生成SQL语句的方法,请使用时换成相应该的SQL语句。例如:$sql = "select * from bluepoint_admin order by id desc limit $set[2],$set[0]";
$p -> RsNum('bluepoint_admin',''); 是用于计算result的数量。使用时应在相对应该的 RsNum方法中修改 $result = MySQL::Select($Table_Name,$Fields,$Condition,'id','0','0','0'); 为自己的SQL语句
更多的PHP经典分页class请到论坛查看: http://BBS.TC711.COM
【 双击滚屏 】 【 评论 】 【 收藏 】 【 打印 】 【 关闭 】
来源:
互联网
日期:2006-6-12
|
|
|