喜悦国际村 » 喜悦原创 » 我写的分页类(支持POST)

页: [1]
songlv2008-3-9 12:19 PM
我写的分页类(支持POST)

目前网上的分页类都需要指定页面和参数,我这个分页类可以自动获得页面和GET,POST参数,免去 特别适合那些高级搜索结果列表页面

[php]
<?php
class issPage {

                     //重写与否
        var $rewrite= false ;

        var $pvar;

    //------分页设置-----------
    function issPage($total,$pagesize=15,$current=false,$pvar = "page") {

                $this->pvar=$pvar;
                $this->total=$total;
                $this->pageMax = ceil($total/$pagesize);//总页数
                if (!$current) $current = empty($_GET[$this->pvar]) ? 1 : intval($_GET[$this->pvar]) ;
                if ($current > $this->pageMax) $current = $this->pageMax;
                if ($current < 1) $current = 1;

                $this->curr  = $current;//当前页面
                $this->psize = $pagesize;//页面大小

                $this->varstr='';
                $this->output='';
        }

    //-----设置除$_GET外的追加变量---------
        function setVar($data) {
                foreach ($data as $k=>$v) {
                        $_GET[$k]=$v;
                }
        }

        function output() {

                $start        = $this->curr-1;
                if ($start<1) $start=1;
                $end        = $start+2;
                if ($end > $this->pageMax)$end=$this->pageMax;
                $pnext=($this->curr+1> $this->pageMax)? $this->pageMax:$this->curr+1;
                if($this->pageMax>1){
                $this->output='<form method="GET" class="issPage" onsubmit="subpage();return false">';
                $this->output.='        <table border="0" cellpadding="0" cellspacing="0">';
                $this->output.='                <tr>';
                $this->output .='        <td class="page_count">'.$this->curr.'/'.$this->pageMax.'</td>';
                $this->output .= '        <td  class="page_content">';
                $this->output .= '                        <a id="page_prev"  href='.$this->FormatUrl($this->curr-1).'>前一页</a>';
                for ($i=$start; $i<=$end; $i++) {
                        if($this->curr == $i)$this->output .= '<span>'.$i.'</span>';
                        else $this->output .= '        <a href='.$this->FormatUrl($i).'>'.$i.'</a>';
                }
                $this->output .= '                        <a id="page_next" href='.$this->FormatUrl($pnext).'>后一页</a>';

                $this->output.='        </td>';
                $this->output.='        <td class="page_cbg">';
                $this->output.='                        <input type="text" name="'.$this->pvar.'" id="'.$this->pvar.'" class="page_query"/>';
                $this->output.='        </td>';
                $this->output.='        <td class="page_cbg">';
                $this->output.='                        <input type="submit" value="GO"  class="page_submit" />';

                $this->output.='        </td>';
                $this->output.='        <td class="page_end">';
                $this->output.='        </td>';
                $this->output.='        </tr>';
                $this->output.='</table>';

                $this->output.="</form>";
                $this->output.='<script>function subpage(page){self.location="'.$this->FormatUrl().'&page="+document.getElementById("'.$this->pvar.'").value;}</script>';

                return $this->output;
                }else{
                        return null;
                }

        }

    //生成Limit语句
        function limit(){
                return ' LIMIT '.(($this->curr - 1) * $this->psize).', '.$this->psize.' ';
        }

        function between(){
                return ' BETWEEN '.($this->total - $this->curr * $this->psize + 1).' AND '.($this->total - ($this->curr - 1) * $this->psize).' ';
        }


        //获得合法的URL地址@Private
        function FormatUrl($num=0){//格式化URL@Private

                if(empty($_SERVER['REDIRECT_URL']) || substr($_SERVER['REDIRECT_URL'],-4)=='.php') $this->rewrite=false;
                //是否重写(注意: 要求文件为.html结尾且重写规则中页码需要在最后。setVar无效,直接替换地址栏url)
               
                $wholeurl="http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?";
                $_GET[$this->pvar]=$num;

                if($this->rewrite){//重写的用REQUEST_URI进行判断
                        $pos = strpos($_SERVER['REQUEST_URI'], '-'.$this->pvar."_".$this->curr.'.html');
                        $pos2 = strpos($_SERVER['REQUEST_URI'], '-'.$this->pvar."_".$this->curr.'-') ;

                        if( $pos === false && $pos2 === false) {
                                Return "http://".$_SERVER['SERVER_NAME'].str_replace('.html',"-".$this->pvar."_$num.html",$_SERVER['REQUEST_URI']);
                        }else {
                                if($pos){
                                        $str=str_replace('-'.$this->pvar."_".$this->curr.'.html',"-".$this->pvar."_$num.html",$_SERVER['REQUEST_URI']);
                                }else{
                                        $str=str_replace('-'.$this->pvar."_".$this->curr.'-', "-".$this->pvar."_$num-", $_SERVER['REQUEST_URI']);
                                }
                                Return "http://".$_SERVER['SERVER_NAME'].$str;
                        }
                        Return $wholeurl;

                }else{//未重写的用PHP_SELF+QUERY_STRING进行判断
                        $_GET=array_merge($_GET, $_POST);
                        foreach($_GET as $k =>$v){
                                $wholeurl.=$k."=".urlencode($v)."&";
                        }
                        Return substr($wholeurl, 0 ,-1);//去掉末尾&
                }
        }

}



[/php]

bollwarm2008-3-12 02:52 AM
应该写一个实例来,不然看代码很麻烦

zw8703072008-3-16 03:56 AM
是哦..有个实例就好啦


查看完整版本: 我写的分页类(支持POST)


Powered by Discuz! Archiver 6.1.0  © 2001-2006 Comsenz Inc.
Processed in 0.006073 second(s), 2 queries