喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

招聘PHP开发人员(北京)

上一主题 下一主题
     
标题: [原创] 我写的分页类(支持POST)  
 
songlv (超级版主)
金牌会员
Rank: 6Rank: 6


UID 73828
精华 0
积分 2933
帖子 689
金钱 2929 喜悦币
威望 0
人脉 4
阅读权限 70
注册 2006-5-22
状态 离线
[广告]: Enom域名自助付费 自助注册 自助PUSH 主流域名COM等一律57.99元年
我写的分页类(支持POST)

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

<?php
<?php
class issPage {

                     
//重写与否
    
var $rewritefalse ;

    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]) ? 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,-1);//去掉末尾&
        
}
    }

}
?>




不要跑  
2008-3-9 08:19 PM#1
查看资料  访问主页  Blog  发短消息  顶部
 
bollwarm (bollwarm)
新手上路
Rank: 1
初级会员



UID 26753
精华 0
积分 17
帖子 17
金钱 17 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2004-3-8
状态 离线
[广告]: Enom域名自助付费 自助注册 自助PUSH 主流域名COM等一律57.99元年
应该写一个实例来,不然看代码很麻烦
2008-3-12 10:52 AM#2
查看资料  发短消息  顶部
 
zw870307
中级会员
Rank: 3Rank: 3


UID 92902
精华 0
积分 282
帖子 379
金钱 282 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2007-5-12
状态 离线
[推荐阅读] 最流行的网络挣钱方式
是哦..有个实例就好啦
2008-3-16 11:56 AM#3
查看资料  Blog  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.077480 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP