PHP,MySQL,模板分页类

<?php 
class Pages{ 
    //数据源
    var $sql;
    //当前页的页码
    var $currentPageNumber;
    //每页的数据条数
    var $itemsPerPage;
    //调用的地址URL
    var $url;
    //总页数
    var $totalPages;
    //数据总数
    var $totalNums;
   
    //显示模块的前缀代码
    var $model_block_pre;
    //显示模块的后缀代码
    var $model_block_end;
    //显示循环数据的前缀代码
    var $model_line_pre;
    //显示循环数据的后缀代码
    var $model_line_end;
    //显示循环数据的主模版
    var $model_main;
    //模版参数
    var $model_argvs;
   
    //初始化函数
    //$sql,数据源SQL
    //$currentPageNumber,当前页的页码
    //$url,调用的地址URL
    //$model,用于显示的数据模版
    //$itemsPerPage,每页的数据条数
    function Pages($sql,$currentPageNumber,$url,$itemsPerPage = 10){ 
        $this->sql = $sql;
        $this->currentPageNumber = $currentPageNumber;
        $this->itemsPerPage = $itemsPerPage;
        $this->url = $url;
        $this->url.=(stristr($this->url,'?')!=false)?'&':'?';
        $this->get_total();
        $this->get_safe_pages();
    } 
   
    //获取总数和总页数
    function get_total(){ 
        $result = mysql_query($this->sql) or die("SQL Error:" . mysql_error());
        $totalNums = mysql_num_rows($result);
        $totalPages = ceil($totalNums/$this->itemsPerPage);
        $this->totalNums = $totalNums;
        $this->totalPages = $totalPages;
        mysql_free_result($result);
    } 
   
    //将当前页保持在安全的范围
    function get_safe_pages(){ 
        if ($this->currentPageNumber > $this->totalPages){ 
            $this->currentPageNumber = $this->totalPages;
        }elseif ($this->currentPageNumber < 1){ 
            $this->currentPageNumber = 1;
        } 
    } 
   
    //获取当前页的数据起始位置
    function get_limit_start(){ 
        $limit_start = ($this->currentPageNumber - 1) * $this->itemsPerPage;
        return $limit_start;
    } 
   
    //获取当前页的数据数目
    function get_limit_nums(){ 
        return $this->itemsPerPage;
    } 
 
    //获取当前页的数据
    function get_data(){ 
        $SQL = $this->sql . " limit " . $this->get_limit_start() . "," . $this->get_limit_nums();
        $result = mysql_query($SQL) or die("SQL Error:" . mysql_error());
        return $result;
    } 
   
    //检查主模版并且提取参数
    function check_main_model(){ 
        $temparray =split('\[',$this->model_main);
        $returnarray = array();
        for($i=1;$i<count($temparray);$i++){ 
            $tempstr = substr($temparray[$i],0,stripos($temparray[$i],']'));
            $templenth = strlen( $tempstr );
            if ($templenth != 0){ 
                $returnarray[] = $tempstr;
            } 
        } 
        $this->model_argvs = $returnarray;
        if ( count($returnarray) == 0 )return false;else return true;
    } 
   
    //根据模版显示数据
    function show_data($model){ 
        $this->model_block_pre = $model[0];
        $this->model_block_end = $model[1];
        $this->model_line_pre = $model[2];
        $this->model_line_end = $model[3];
        $this->model_main = $model[4];
        if ($this->totalNums < 1){ 
            echo '没有数据';
        }elseif ($this->check_main_model() == false){ 
            echo '模版错误';
        }else{ 
            echo $this->model_block_pre;
            $result = $this->get_data();
            while ( $row = mysql_fetch_assoc($result) ) { 
                $tempstr = $this->model_main;
                echo $this->model_line_pre;
                for($i=0; $i < count($this->model_argvs);$i++){ 
                    $preg = "(.*)(\[". $this->model_argvs[$i] ."\])(.*)";
                    $target = "\\1". $row[$this->model_argvs[$i]] ."\\3";
                    $tempstr = ereg_replace($preg,$target,$tempstr);
                } 
                echo $tempstr;
                echo $this->model_line_end;
            } 
            echo $this->model_block_end;
        } 
    } 
   
    //显示分页项目
    function show_pages(){ 
        if ($this->totalNums < 1){ 
            //没有数据
        }else{ 
            echo "<p>";
            echo "<a href=\"".$this->url."page=1\">首页</a> ";
            echo "<a href=\"".$this->url."page=". ($this->currentPageNumber - 1) ."\">上页</a> ";
            echo "<a href=\"".$this->url."page=". ($this->currentPageNumber + 1) ."\">下页</a> ";
            echo "<a href=\"".$this->url."page=". ($this->totalPages) ."\">尾页</a> ";
            echo ($this->currentPageNumber) ."/". ($this->totalPages) ." ";
            echo "每页". ($this->itemsPerPage) ."条/共". ($this->totalNums) ."条";
            echo "</p>";
        } 
    } 

?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值