一个简单的php分页类代码(转载)

入门级php分页类

原文地址:http://www.xfcodes.com/php/fenye/3608.htm
时间:2015-12-16 20:52:00来源:网络
php分页类。

 

复制代码代码如下:

<?php
/*
* 快速php分页类
*
**/
class Page{
    private $totalpage;
    private $stride;
    private $currentpage;
    //设置总页数
    function setTotalpage($objpage=1){
    $this->totalpage=$objpage;
    }
    //设置当前页
    function setCurrentpage($objpage=1){
    $this->currentpage=$objpage;
    } //先锋教程网 www.xfcodes.com
    //设置跨度
    function setStride($objStride=1){
    $this->stride=$objStride;
    }
//获得总页数
    function getTotalpage(){
    return $this->totalpage;
    }
//获得跨读
    function getStride($objStride=1){
    return $this->stride;
    }
    //获取当前页
    function getCurrentpage($objpage=1){
    return $this->currentpage;
    }
    //打印分页
    public function Pageprint(){
        for($Tmpa=1;$Tmpa<=$this->totalpage;$Tmpa++){
            if($Tmpa+$this->stride<$this->currentpage){//加了跨度还小于当前页的不显示
                continue;
                }
            if($Tmpa+$this->stride==$this->currentpage){//刚好够跨度的页数
                $p=$this->currentpage-1;
                $willprint.="<a href=\"$_SERVER[PHP_SELF]?page=1\"><span>首页</span></a> <a href=\"$_SERVER[PHP_SELF]?page=$p\"><span>上一页</span></a> ";
                }
            if($Tmpa>$this->currentpage+$this->stride){//大于当前页+跨度的页面
                break;
            }
            $willprint.="[<span><a href=\"$_SERVER[PHP_SELF]?page=$Tmpa\">$Tmpa</a></span>]";
            if($Tmpa==$this->currentpage+$this->stride){//刚好够跨度的页数
                $p=$this->currentpage+$this->stride+1;
                $willprint.="<a href=\"$_SERVER[PHP_SELF]?page=$p\"><span>下一页</span></a> <a href=\"$_SERVER[PHP_SELF]?page=$this->totalpage\"><span>末页</span></a>";
                }
            }
        echo $willprint;
    }
}

class cut_page extends Page
{
    var    $filename;
    var $showtotal;
    function __construct($filename){
    $this->cachefile    =    $filename;
    $this->showtotal    =    "yes";
    }
    public function filename(){
    if (file_exists($this->cachefile)){
        return 1;
        }
    else
        return 0;
    }
    private $type;
    public function typelist($typeid){
        $this->type    =    $typeid;
        $fp    =    fopen($this->cachefile,"rb");
        $str=    fread($fp,filesize($this->cachefile));
        //echo $this->type."<br>"; //分类ID
        $arr=    explode("::",$str);

        for ($i=0;$i<count($arr);$i++){
            $arr2    =    explode("=",$arr[$i]);
            if($arr2[0]==$this->type){
                //return $arr2[1];
                if(isset($_GET[page])){
                    $page=$_GET[page];
                }else{
                    $page=1;
                    }
                Page::setTotalpage($arr2[1]);
                Page::setCurrentpage($page);
                Page::setStride(3);
                Page::Pageprint();
                if ($this->showtotal=="yes"){
                    echo "<span style=\"border:1px solid #ccc;margin-left:40px;padding:1px 1px;\">".$page."/".$arr2[1]."页</span>";
                    }
                }
            }
        fclose($fp);
    }
    public function updatelist($typeid,$value){
        $this->type    =    $typeid;
        $this->typevalue=$value;
        if (file_exists($this->cachefile)){        //若文件存在
            $fp    =    fopen($this->cachefile,"rb+");
            $str=    fread($fp,filesize($this->cachefile));
            if(ereg("::".$this->type."=[0-9]+",$str)){
                //正则替换
                $str2=    ereg_replace("::".$this->type."=[0-9]+","::".$this->type."=".$this->typevalue,$str);
                fseek($fp,0); //从文件头开始
                fwrite($fp,$str2);        //写入文件新数据
                $seek    =    ftell($fp);        //返回文件指针长度
                ftruncate($fp,$seek);        //截断文件
                }
            else{
                $str2    =    "::".$this->type."=".$this->typevalue;
                fwrite($fp,$str2);    //若分类ID不存在,则续写文件
                }
            }
        else{        //若文件不存在
            $fp    =    fopen($this->cachefile,"xb");
            $str="::".$this->type."=".$this->typevalue;
            fwrite($fp,$str);
            }
        fclose($fp);
    }
}

/*使用方法,参数为缓存文件名,可自定义*/
$listpage    =    new cut_page("pages_cache.txt");
/*列出分页 参数为分类的ID*/
$listpage->typelist("2");
$listpage->showtotal="yes";    //是否显示总数yes /no   默认为yes

echo "<br>";
/*
修改分页文件或者创建分页文件,参数分别为分类ID,分页数量,此方法使用在创建文章后遍历数据库获得分类的ID及分页的总量
分页=总记录/每页显示数目 ^0^这个自己算。updatelist(type,num)
//$listpage->updatelist("6","900");
  cut_page为主类。可进行分页的创建,修改,显示
*/
?>

posted @ 2015-12-28 11:56  等风来  阅读(327)  评论(0编辑  收藏  举报