php分页类,可传自定义参数,调用方便

php分页类,用起来还算方便吧。

演示图片:php分页.png

 

网上流传的 php分页类 大部分不支持自定义参数,比如参数是不定的,不能很好的实现,我分享一个我一直使用的分页,原理用在java,.net,现在写出php版本的,希望对大家有帮助吧。
php分页类PageUtil

<?php
  class PageUtil{
          var $pageUrl;
          var $totalPage;
          var $prePageIndex;
          var $nextPageIndex;
          var $currPageIndex;
          var $firstPageIndex;
          var $lastPageIndex;
          var $totalRec;
          var $pageSize;
          var $sql;
          var $pk;
          var $desc;
          function PageUtil($currPageIndex,$pageSize,$pageUrl,$sql,$pk,$desc){
            $this->currPageIndex = $currPageIndex;
            $this->pageSize = $pageSize;
            $this->pageUrl = $pageUrl;
            $this->sql = $sql;
            $this->pk = $pk;
            $this->desc = $desc;
            $this->init();
    }
    function init(){
            $this->totalRec = $this->computeTotalRec();
            $this->totalPage = floor($this->totalRec/$this->pageSize);
            if ($this->totalRec % $this->pageSize > 0){
                $this->totalPage = $this->totalPage + 1;
            }
    }
    function computeTotalRec(){
            $query = " select count(*) c from";
            $query.= " (";
            $query.= " ".$this->sql."";
            $query.= " )count_table";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['c'];
    }
    function pageDataSet(){
                $n1 = ($this->currPageIndex- 1) * $this->pageSize ;
            $query = " select * from(";
            $query.= $this->sql;
            $query.= " )dt order by "; 
            $query.= "dt.".$this->pk;
            $query.= " ".$this->desc;
            $query.= " limit ".$n1.", ".$this->pageSize;
            $result = mysql_query($query);
            if(!$result){
                     echo mysql_error();
            }
            return $result;            
    }
   function  createPageFooter()
        {
            $this->prePageIndex = $this->currPageIndex - 1;
            $this->nextPageIndex = $this->currPageIndex + 1;
            $str="";
            if ($this->currPageIndex > 1)
            {
                $str.="<a href='".$this->pageUrl."&page=1&pageSize=".$this->pageSize."'>首页</a> ";
            }
            else
            {
                $str.="首页 ";
            }
            if ($this->currPageIndex > 1)
            {
                $str.="<a href='".$this->pageUrl."&page=".$this->prePageIndex."&pageSize=".$this->pageSize."'>上页</a> ";
            }
            else
            {
                $str.="上页 ";
            }
            $str.=" 当前".$this->currPageIndex."页 ";
            if ($this->currPageIndex < $this->totalPage)
            {
                $str.="<a href='".$this->pageUrl."&page=".$this->nextPageIndex."&pageSize=".$this->pageSize."'>下页</a> ";
            }
            else
            {
                 $str.="下页";
            }
            if ($this->totalPage > 1 && $this->currPageIndex != $this->totalPage)
            {
                 $str.="<a href='".$this->pageUrl."&page=".$this->totalPage."&pageSize=".$this->pageSize."'>末页</a>  ";
            }
            else
            {
                $str.="末页";
            }
            $str.=" 共".$this->totalRec."条记录";
            $str.="  每页<SELECT size=1 name=pagesize onchange='window.location.href=this.value'>";

            if ($this->pageSize == 3)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=3 selected>3</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=3>3</OPTION>";
            }

            if ($this->pageSize == 10)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=10 selected>10</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=10>10</OPTION>";
            }
            if ($this->pageSize == 20)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=20 selected>20</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=20>20</OPTION>";
            }
            if ($this->pageSize == 50)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=50 selected>50</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=50>50</OPTION>";
            }
            if ($this->pageSize == 100)
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=100 selected>100</OPTION>";
            }
            else
            {
                $str.="<OPTION value=".$this->pageUrl."&pageSize=100>100</OPTION>";
            }
            $str.="</SELECT>";
            $str.="条 分".$this->totalPage."页显示 转到";
            $str.="<SELECT size=1 name=Pagelist onchange='window.location.href=this.value'>";
            for ($i = 1; $i < $this->totalPage + 1; $i++)
            {
                if ($i == $this->currPageIndex)
                {
                    $str.="<OPTION value=".$this->pageUrl."&page=".$i."&pageSize=".$this->pageSize." selected>".$i."</OPTION>";
                }         
                else         
                {
                    $str.="<OPTION value=".$this->pageUrl."&page=".$i."&pageSize=".$this->pageSize.">".$i."</OPTION>";
                }
            }
            $str.="</SELECT>页";

            return $str;
        }
  }
?>

看看如何调用这个php分页类吧,这里我只写主要代码

<?php
$currPageIndex = $_GET["page"]; //当前页
$pageSize = $_GET["pageSize"];//每页多少条
$currPageIndex = $currPageIndex == null ? "1" : $currPageIndex;//取url传过来的当前页
$pageSize = $pageSize == null ? "20" : $pageSize;//取url传过来的页大小
$query =" select t.id tid,t.username,tt.mp,ttt.jftype,t.jifen,t.info,t.addtime from wd_jifen_rec t";//这里是sql
$query.=" join wd_jifen_money_package tt on t.mpid = tt.id ";
$pageUrl = "jifen_rec.php?1=1";
if($_GET['jftype']!='-1'&&$_GET['jftype']){//jftype 自定义参数
$query.=" and t.jftype=".$_GET['jftype'];
$pageUrl.="&jftype=".$_GET['jftype'];
}
if($_GET['mpid']!='-1'&&$_GET['mpid']){//mpid自定义参数
$query.=" and t.mpid=".$_GET['mpid'];
$pageUrl .= "&mpid=".$_GET['mpid'];
}
if($_GET['username']){//自定义参数
$query.=" and t.username='".$_GET['username']."'";
$pageUrl .= "&username=".$_GET['username']."";
}
$query.=" join wd_jifen_type ttt on t.jftype = ttt.id";
//把参数传给分页类,其他就不用管了,参数依次是:当前页,页大小,当前页面url,sql遇见,按照addtim排序,倒下排列
$page = new PageUtil($currPageIndex,$pageSize,$pageUrl,$query,"addtime","desc");
$result = $page->pageDataSet();//分页页面结果集
$pagebar = $page->createPageFooter();//生成页脚分页导航
while ($row=mysql_fetch_array($result)){
?>
<tr align="center">
<td height="25"><?php echo $row['tid'];?></td>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['jftype'];?></td>
<td><?php echo $row['mp']."";?></td>
<td><?php echo $row['jifen'];?></td>
<td><?php echo $row['info'];?></td>
<td><?php echo $row['addtime'];?></td>
</tr>
<?php
}
?>
<tr align="center">
<td height="25" colspan="5" align="right"><?php echo $pagebar;?></td>//页脚分页导航
</tr>

尽管这个 php分页类 比较简陋,但应付基本的需要,没什么问题了。