一个浅显易懂的分页类

类别:编程语言 点击:0 评论:0 推荐:

一个浅显易懂的分页类

<?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("&laquo;首页", "&#8249;前页", "后页&#8250;", "末页&raquo;");//可以设置成图片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("&laquo;首页", "&#8249;前页", "后页&#8250;", "末页&raquo;");
 /*
  * 功能:分页函数
  * 参数 $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]."&nbsp;".$this->ar[1]."&nbsp;";
  }else {
   $trunpage .= '<a href="'.$str.'1">'.$this->ar[0].'</a> <a href="'.$str.($page-1).'">'.$this->ar[1].'/a> ';
  }
  $trunpage .= "&nbsp;共[$recordcount]条记录/共[$this->pagecount]页&nbsp;";
  if($page == $this->pagecount){
   $trunpage .= $this->ar[2]."&nbsp;".$this->ar[3]."&nbsp;";
  }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;
 }
 
}
?>

本文地址:http://com.8s8s.com/it/it27026.htm