一个浅显易懂的分页类
<?php
class page{
/*
实例:
$sql = "select count(*) from t_article";
$db->query($sql);
$db->next_record();
$recordcount = $db->f(0);//记录总数
$ofun = new page;
$ofun->pagearg = "pagenum";//设置分页参数名,默认为page
$ofun->ar = array("«首页", "‹前页", "后页›", "末页»");//可以设置成图片HTML代码
$trunpage = $ofun->show_page($recordcount, 20);//得到分页信息
$sql = "select * from t_article order by tdate desc ".$ofun->limit;
$db->query($sql);
*/
var $pagecount; //总页数
var $limit = ""; //分页时用到的limit参数
var $pagearg = "page"; //分页参数名称
var $ar = array("«首页", "‹前页", "后页›", "末页»");
/*
* 功能:分页函数
* 参数 $recordcount 为记录总数
* 参数 $pagesize 为每页显示记录数,默认为20
*/
function show_page($recordcount, $pagesize = 20){
$this->pagecount = ceil($recordcount/$pagesize);//总页数
//取得页码,默认为1
if(isset($_GET[$this->pagearg])){
if($_GET[$this->pagearg] <= 0) $page = 1;
else $page = $_GET[$this->pagearg];
}else {
$page = 1;
}
$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=";
}
if($page > $this->pagecount) $page = $this->pagecount;
$this->limit = "limit ".($page-1)*$pagesize.",".$pagesize;
$trunpage = "";
if($page == 1){
$trunpage .= $this->ar[0]." ".$this->ar[1]." ";
}else {
$trunpage .= '<a href="'.$str.'1">'.$this->ar[0].'</a> <a href="'.$str.($page-1).'">'.$this->ar[1].'/a> ';
}
$trunpage .= " 共[$recordcount]条记录/共[$this->pagecount]页 ";
if($page == $this->pagecount){
$trunpage .= $this->ar[2]." ".$this->ar[3]." ";
}else {
$trunpage .= ' <a href="'.$str.($page+1).'">'.$this->ar[2].'</a> <a href="'.$str.$this->pagecount.'">'.$this->ar[3].'</a>';
}
$select = "<select onchange='location.href=this.options[this.selectedIndex].value'>\n";
for($i = 1; $i <= $this->pagecount; $i++){
$select .= "<option value=\"".$str.$i."\">$i</option>\n";
}
$select .= "</select>";
return $trunpage.$select;
}
}
?>