前端

img erldy

分享我第一次写的PHP+MYSQL分页类

发表于2004/9/28 13:43:00  604人阅读

        之前看过几个分页类代码都比较长和复杂,愧于自己水平低下,没能看懂那些代码,加上发现当要在复合条件查询并分页显示数据的时候,那就很不好用了,于是想自己写一个。使用PHP五个月,对于类的概念和使用也才刚刚似懂非懂,这是本人第一次写的类——分页导航类,希望对新手,还有像我这样的PHPer有帮助!!

/*
_________________________________
 CLASS NAME:SHOWPAGE
_________________________________
 Copyright(c): ERLDY(稻草人)
 Author: Arvan  [E-mail:erldy@126.com QQ:103808353]
 Create date:   2004-09-27
 ________________________________
*/
 /*****
实例:
  $sql = "select count(*)as count from $tbl";
  $db->query($sql);
  $db->next_record();
  $count = $db->f(count);//记录总数
  
  $page = new ShowPage;
   $page->PageSize = 20;
  $page->Total = $count;
  $page->LinkAry = array(); //使用array("id"=>$id)这样的数组传递url变量
  $sql = "select * from $tbl order by sortid desc limit  ".$page->OffSet();
  $db->query($sql);
  $showpage = $page->ShowLink();

  
*****/
 class ShowPage {
  
 var $PageSize;     //每页显示的记录数
 
 var $Total;        //记录总数
 
 var $LinkAry;      //Url参数数组,对于复合条件查询分页显示情况非常好用
 
//取得总页数
 function PageCount() {
  $TotalPage = ($this->Total % $this->PageSize == 0) ? floor($this->Total / $this->PageSize) :  floor($this->Total / $this->PageSize)+1;
  return $TotalPage;
  }
//取得当前页
 function PageNum() {
  $page =  (isset( $_GET['page'])!="") ? $_GET['page'] :  $page = 1;
  return $page;
 }
//查询语句定位指针  
 function OffSet() {
  if ($this->PageNum() > $this->PageCount()) {
         //$this->PageNum = $this->PageCount();
         $pagemin = max(0,$this->Total - $this->PageSize - 1);
        }else if ($this->PageNum() == 1){
          $pagemin = 0;
         }else {
          $pagemin = min($this->Total - 1,$this->PageSize * ($this->PageNum() - 1));
           }
  return $pagemin . "," . $this->PageSize;
     }
//定位首页  
 function FristPage() {
  $Frist = ($this->PageNum() <= 1) ? "首页  " : "<a href=/"?page=1".$this->Url($this->LinkAry)."/">首页</a> ";
  return $Frist;
 }
//定位上一页
 function PrePage() {
  $prepage=$this->PageNum() - 1;
  $Previous = ($this->PageNum() >= 2) ? " <a href=/"?page=".$prepage.$this->Url($this->LinkAry)."/">上一页</a> " : "上一页 ";
  return $Previous;
 }
//定位下一页
 function NextPage() {
  $nextpage = $this->PageNum() + 1;
  $Next = ($this->PageNum() <= $this->PageCount()-1) ? " <a href=/"?page=".$nextpage.$this->Url($this->LinkAry)."/">下一页</a> " : "下一页 ";
  return $Next;
 }
//定位最后一页
 function LastPage() {
  $Last = ($this->PageNum() >= $this->PageCount()) ? "尾页  " : " <a href=/"?page=".$this->PageCount().$this->Url($this->LinkAry)."/">尾页</a> ";
  return $Last;
 }
//下拉跳转页面
 function JumpPage() {
  $Jump = " 当前第 <b>".$this->PageNum()."</b> 页  共 <b>".$this->PageCount()."</b> 页 跳到 <select name=page onchange=/"javascript:location=this.options[this.selectedIndex].value;/">";
  for ($i=1; $i<=$this->PageCount(); $i++) {
  if ($i==$this->PageNum())
   $Jump .= "<option value=/"?page=".$i.$this->Url($this->LinkAry)."/" selected>$i</option>";
  else
   $Jump .="<option value=/"?page=".$i.$this->Url($this->LinkAry)."/">$i</option> "; 
  }
     $Jump .= "</select> 页   <b>[".$this->PageSize."条/页]</b>";
  return $Jump;
 }
//URL参数处理
 function Url($ary) {
  $Linkstr = "";
  if (count($ary) > 0) {
   foreach ($ary as $key => $val) {
   $Linkstr .= "&".$key."=".$val;
   }
  }
  return $Linkstr;
 }
//生成导航条
 function ShowLink() {
  return $this->FristPage().$this->PrePage().$this->NextPage().$this->LastPage().$this->JumpPage();
 } 
 }

        这个类实现了分页的一般但比较全的功能,自我感觉结构非常简单,代码也很容易看懂,并且也比较好用!!

阅读全文
0 0

相关文章推荐

img
取 消
img