分享两个php分页类

 php分页类算是php网站设计中最最常用的一个类了,我找到了两个,在这里和大家分享一下。都是经过本人测试的,没有任何问题。直接上代码了。

1.第一个php分页类

<?php
class Pager
{
    var $_perPage; //每一页记录数

    var $_totalItems; //总记录数

    var $_totalPages; //总共页数

    var $_currentPage; // 当前页序号

    var $_listStart; //当前头条记录ID

    var $_listEnd; //当前最后条记录ID

    var $_pageStart; //第一页

    var $_pageEnd; //最后页

    var $_listPage; //循环显示当前分页列表

    var $_pageData; //当前要显示的记录

    var $_linkData; //页面传递的变量

    var $_startId; //从数据库读出的启始id

 

    function pager($_totalItems, $_perPage, $_linkData)
    {
        $this->_currentPage = max((int)@$_GET[‘pageId’], 1); //当前的页序号

        $this->_perPage = $_perPage; //每一页记录数

        $this->_totalItems = $_totalItems; //总记录数

        $this->_totalPages = ceil($this->_totalItems / $this->_perPage); //总页数

        $this->_linkData = $_linkData; //传递的变量

        $this->_listStart = ($this->_currentPage – 1) * $this->_perPage + 1; //当前头条记录ID

        $this->_listEnd = ($this->_currentPage != $this->_totalPages) ? $this->_currentPage * $this->_perPage : $this->_totalItems; //当前最后条记录ID

        $this->_startId = $this->_listStart -1; //从数据库读出的启始id

    }

 

    function startId() // 从数据库读出的启始id

    {
        return $this->_startId;
    }
    function totalItems() // 总记录数

    {
        return $this->_totalItems;
    }
    function totalPages() // 总页数

    {
        return $this->_totalPages;
    }
    function listStart() // 当前头条记录ID

    {
        return $this->_listStart;

 

    }
    function listEnd() // 当前最后条记录ID

    {
        return $this->_listEnd;
    }
    function currentPage() // 当前页

    {
        return $this->_currentPage;
    }

 

    function pageStart() // 第一页

    {
        return $this->_pageStart = “<a href=” . $_SERVER[‘PHP_SELF’] . ($this->_linkData) . “&pageId=1><FONT title=首页 face=webdings>9</FONT></a>”;
    }
    function pageEnd() // 最后页

    {
        return $this->_pageEnd = “<a href=” . $_SERVER[‘PHP_SELF’] . ($this->_linkData) . “&pageId=” . $this->_totalPages . “><FONT title=尾页 face=webdings>:</FONT></a>”;
    }
    function upPage() // 上一页

 

    {
        if ($this->_currentPage > 1 and $this->_currentPage <= $this->_totalPages)
        {
            $up = $this->_currentPage-1;
            return $this->_upPage = “<a href=” . $_SERVER[‘PHP_SELF’] . ($this->_linkData) . “&pageId=” . $up . “><FONT title=上页 face=webdings>7</FONT></a>”;
        }
    }
    function downPage() // 下一页

    {
        $down = $this->_currentPage + 1;
        if ($this->_currentPage < $this->_totalPages)
            return $this->_downPage = “<a href=” . $_SERVER[‘PHP_SELF’] . ($this->_linkData) . “&pageId=” . $down . “><FONT title=下页 face=webdings>8</FONT></a>”;

 

    }
    function listPage() // 循环显示当前分页列表

    {
        if ($this->_currentPage <= 5 and $this->_currentPage != “”)
        {
            $start = 1;
            if ($this->_totalPages < 10) $end = $this->_totalPages;
            else $end = 10;
        } elseif (($this->_currentPage + 5) > $this->_totalPages)
        {
            $start = $this->_currentPage – 5;

 

            $end = $this->_totalPages;
        }
        else
        {
            $start = $this->_currentPage – 4;
            $end = $this->_currentPage + 5;
        }
        for($i = $start; $i <= $end; $i++)
        {
            $this->_listPage .= “<a href=” . $_SERVER[‘PHP_SELF’] . ($this->_linkData) . “&pageId=” . $i . “>” . $i . “</a> “;
        }
        return $this->_listPage;
    }
    function jumpPage() // 跳转

    {
        $this->_jumpPage = “<table border=0 width=100% class=’fonts’><tr><td>跳转 <select name=’pageId’ style=’height=14px’ onchange=\”location='” . $_SERVER[‘PHP_SELF’] . ($this->_linkData) . “&pa

 

geId=’+this.value;\”>”; //页面跳转

        for($i = 1; $i <= $this->_totalPages; $i++)
        {
            if ($i == $this->_currentPage) $selected = “selected”;
            $this->_jumpPage .= “<option value=’$i’ $selected>$i</option>”;
            unset($selected);
        }
        return $this->_jumpPage .= “</select> 页</td></tr></table>”;
    }
}
?>

应用效果截图:

php分页类
php分页类

 

2.第二个php分页类

<?php

class SepPage{
 var $rs;
 var $pagesize;     //定义每页显示的记录数
 var $nowpage;     //当前页码
 var $array;
 var $conn;
 var $sqlstr;     //执行的SQL语句
 var $total;
 var $pagecount;     //总的记录数
 function ShowDate($sqlstr,$conn,$pagesize,$nowpage){ //定义方法
  $arrays=array();
  $array_title=array();
  $array_content=array();
  if(!isset($nowpage) || $nowpage==”” || $nowpage==0)   //判断当前页变量值是否为空
   $this->nowpage=1;      //定义当前页的值
  else
   $this->nowpage=$nowpage;    //获取当前页的值
  
  $this->pagesize=$pagesize;     //定义每页输出的记录数
  $this->conn=$conn;       //连接数据库返回的标识
  $this->sqlstr=$sqlstr;      //执行的查询语句
  $this->pagecount=$pagecount;    //总的记录数
  $this->total=$total;      //总的记录数
  
  $this->rs=mysql_query($this->sqlstr.”limit “.$this->pagesize*($this->nowpage-1).”,$this->pagesize”,$this->conn);
  $this->total=mysql_num_rows($this->rs);   //获取记录数
    if($this->total==0){         //判断如果查询结果为0,则输出如下内容
   return false;  
    }else{        //否则
      if(($this->total % $this->pagesize)==0){   //判断如果总的记录数除以每页显示的记录数等于0
      $this->pagecount=intval($this->total/$this->pagesize); //则为变量pagecount赋值
   }else if($this->total<=$this->pagesize){
    $this->pagecount=1;//如果查询结果小于等于每页记录数,那么为变量赋值为1 
     }else{
        $this->pagecount=ceil($this->total/$this->pagesize); //否则输出变量值
     }
   while($this->array=mysql_fetch_array($this->rs)){
    array_push($array_title,$this->array[title]);
    array_push($array_content,$this->array[content]);
   }   
   array_push($arrays,$array_title,$array_content);
   return $arrays;
  }
 }
 function ShowPage($contentname,$utits,$anothersearchstr,$class){
  $allrs=mysql_query($this->sqlstr,$this->conn);  //执行查询语句
  $record=mysql_num_rows($allrs);
  $pagecount=ceil($record/$this->pagesize);  //计算共有几页
  $str.=”共有”.$contentname.”&nbsp;”.$record.”&nbsp;”.$utits.”&nbsp;每页显示&nbsp;”.$this->pagesize.”&nbsp;”.$utits.”&nbsp;第&nbsp;”.$this->nowpage.”&nbsp;页/共&nbsp;”.$pagecount.”&nbsp;页”;
  $str.=”&nbsp;&nbsp;&nbsp;&nbsp;”;
  $str.=”<a href=”.$_SERVER[‘PHP_SELF’].”?page=1″.$anothersearchstr.”>首页</a>”;
  $str.=”&nbsp;”;
  if(($this->nowpage-1)<=0){
   $str.=”<a href=”.$_SERVER[‘PHP_SELF’].”?page=1″.$anothersearchstr.”>上一页</a>”;
  }else{
   $str.=”<a href=”.$_SERVER[‘PHP_SELF’].”?page=”.($this->nowpage-1).$anothersearchstr.”>上一页</a>”;
  }
  $str.=”&nbsp;”; 
  if(($this->nowpage+1)>=$pagecount){
   $str.=”<a href=”.$_SERVER[‘PHP_SELF’].”?page=”.$pagecount.$anothersearchstr.”>下一页</a>”;
  }else{
   $str.=”<a href=”.$_SERVER[‘PHP_SELF’].”?page=”.($this->nowpage+1).$anothersearchstr.”>下一页</a>”;
  }
  $str.=”&nbsp;”;
   $str.=”<a href=”.$_SERVER[‘PHP_SELF’].”?page=”.$pagecount.$anothersearchstr.”>尾页</a>”;
  if(count($this->array)==0 || $this->rs==false)   
   return “”;
  else
      return $str; 
 }
}

?>

应用效果截图:

php-分页类

One comment

Leave a Reply