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

RSS 订阅当前论坛  

$5.95 Web Hosting     

上一主题 下一主题
 62  1/7  1  2  3  4  5  6  7  > 
     
标题: [原创] 分页,一个函数足矣  
 
DJ.
新手上路
Rank: 1



UID 129870
精华 0
积分 42
帖子 38
金钱 42 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-24
状态 离线
[广告]: Enom域名自助付费 自助注册 自助PUSH 主流域名COM等一律57.99元年
分页,一个函数足矣

有点累,暂时先不说太多

<?php
/*
函数: doPage
作者: DJ <[email]DJ@Bluessoft.com[/email]>
作用:通过记录总数,每页显示数取得offset,总页数,前一页与后一页以及分页纯数字的页数数组,不带任何链接
返回值: 数组
$aPDatas['offset']   offset 
$aPDatas['t']           当前页 
$aPDatas['m']         总页数 
$aPDatas['p']         上一页 
$aPDatas['n']         下一页
$aPDatas['bar']      分页数组
$aPDatas['pg']       上一组页码
$aPDatas['ng']       下一组页码
$aPDatas['qs']        QUERY_STRING 

参数说明:
$intTotal   记录总数
$intCol     每页显示数

以下几个参数,请根本实际使用习惯把你最常用的相关值设为默认,这样平时只使用上面两个参数就够了
$intNum     分页数组数量,当为'all'时,显示全部。根据你的习惯,把默认值修改。
$strVar     分页变量名,在地址档中如abc.php?page=123,即是page,默认是'page'
$bGetQS     是否读取Query String, 默认读取
$bEncodeQS  是否对Query String使用htmlspecialchars,默认使用
$intPos     当前页所在分页数组中位置,根据你的习惯,把默认值修改。
*/
function doPage$intTotal $intCol $intNum 10 $intPos $strVar 'page' $bGetQS true $bEncodeQS true )
{
    
$aPDatas = array( ) ;
    if ( isset( 
$_REQUEST$strVar ] ) )
    {
        ( 
$intThisPage $_REQUEST$strVar ] ) > 1  
        
$aPDatas['t'] = $intThisPage 
        
$aPDatas['t'] = $intThisPage ;
    }
    else
    {
        
$aPDatas['t'] = $intThisPage ;
    }

    if ( 
$intTotal || $intCol )
    {
        
$aPDatas['offset'] = ;
        
$aPDatas['t'] = $aPDatas['m'] = $aPDatas['p'] = $aPDatas['n'] = ;
    }
    else
    {
        
$aPDatas['offset']   = $intCol * ( $intThisPage ) ; 
        
$aPDatas['m']  = ceil $intTotal $intCol ) ;
        
$aPDatas['p']  = $intThisPage $intThisPage ;
        
$aPDatas['n'] = $intThisPage == $aPDatas['m'] ? $aPDatas['m']  : $intThisPage ;
    }

    
$intNum $intNum == 'all' $aPDatas['m'] :  intval $intNum ) ;

    if ( 
$intNum 
    {
        
$intSPage = ( $intSsPage $intThisPage $intPos ) < $intSsPage ;
        if ( 
$intSPage $intNum $aPDatas['m'] ) $intSPage abs$aPDatas['m'] - $intNum ) + ;
        
$intEPage = ( $intEsPage $intSPage $intNum ) > $aPDatas['m'
                        ? 
$aPDatas['m'] : $intEsPage 
        
$aPDatas['pg']  = ( $intPGroup $intThisPage $intNum ) > ?  $intPGroup  ;
        
$aPDatas['ng']  = ( $intNGroup $intThisPage $intNum ) < $aPDatas['m'] ? $intNGroup $aPDatas['m'] ;
        
        
$arrPageBar = array ( ) ; 
        for ( 
$i $intSPage $i <= $intEPage $i++ ) 
        {
            
$arrPageBar[] = $i ;
        }
        
$aPDatas['bar']  = $arrPageBar ;
    }
    if ( 
$bGetQS )
    {
        
$strPagepattern '/('.$strVar.'=d{0,})/' ;
        
preg_match_all$strPagepattern$_SERVER["QUERY_STRING"] , $arrResult );
        if ( 
$arrResult[1] )
        {
            
$strQueryString $arrResult[1][0] ? str_replace"&".$arrResult[1][0] , "" $_SERVER["QUERY_STRING"] ) : $_SERVER["QUERY_STRING"];
            
$strQueryString str_replace$arrResult[1][0] , "" $strQueryString ) ; 
        }
        else
        {
            
$strQueryString $_SERVER["QUERY_STRING"] ;
        }
        
         if ( 
$strQueryString 
        {
            
$strQueryString $bEncodeQS htmlspecialchars($strQueryString) . '&' $strQueryString '&' ;
        } 
        
$aPDatas['qs'] = $strQueryString 
    }
    return 
$aPDatas ;
}

error_reporting(ALL) ;


//PHP程序使用例子
$intCount    1000 ;  // 用你的方法取得记录总数, 这里假设是1000 
$intCol  10 ;     // 每页显示5个记录

//根据前面两个变量 就可以使用函数完成余下的工作了
$aPDatas doPage $intCount $intCol  ) ;

$intOffset $aPDatas['offset'] ;

print_r($aPDatas) ;
?>
2008-6-26 10:21 PM#1
查看资料  发短消息  顶部
 
开花石头 (南冥洗)
版主
Rank: 7Rank: 7Rank: 7
努力把内裤穿在外面


UID 7949
精华 12
积分 25632
帖子 4532
金钱 25512 喜悦币
威望 120
人脉 0
阅读权限 100
注册 2001-4-19
状态 离线
[广告]: 代充Paypal帐号美元
这么多变量在算,不累啊。。。。。
分页只干他该干的事情

<?php
    
    
/*
     * @(#)Pagination.class.php 1.0 08/04/15
     *
     * Copyright 2008 [url]www.8y8u.com.cn[/url]
     */


    /**
    * <b>通用翻页类</b><br>
    * 翻页、数字条、跳转显示
    * @author    Kevin
    * @version    1.0 08/04/16
    * @since       PHP 5.2.5 mysql 5.0.51
    */
    
    
class Pagination{
        
        
/**
        * 总条目数
        */
        
private $total;
        
        
/**
        * 每页显示条目数
        */
        
private $pageSize 20;
        
        
/**
        * 总页数
        */
        
private $sumPageNumber;
        
        
/**
        * 当前页数
        */
        
private $currentlyPageNumber;
        
        
/**
        * 结果集开始条目数
        */
        
private $startNumber;
        
        
/**
        * 结果集结束条目数
        */
        
private $endNumber;
        
        
/**
        * 翻页时的标识
        */
        
private $pageNumberFlag "page";
        
        
/**
        * 翻页时需要额外带的参数
        */
        
private $linksParameter;
        
        
/**
        * 数字翻页条的数量
        */
        
private $numberBar 6;
        
        
/**
        * 首页翻页标识
        */
        
private $firstPageFlag "[首页]";
        
        
/**
        * 尾页翻页标识
        */
        
private $lastPageFlag "[尾页]";
        
        
/**
        * 上页翻页标识
        */
        
private $previousPageFlag "[上页]";
        
        
/**
        * 下页翻页标识
        */
        
private $nextPageFlag "[下页]";
        
        
/**
        * 初始化,在SET相关参数后执行,执行后才可以调用GET
        * @name     initialize
        * @return     void
        */
        
function initialize(){
            
            
$this->sumPageNumber ceil$this->total $this->pageSize );
            if( 
== $this->sumPageNumber )$this->sumPageNumber++;
            
            
$this->currentlyPageNumber $_REQUEST[$this->pageNumberFlag];
            if ( ( 
$this->currentlyPageNumber == "" ) || 
                 ( 
$this->currentlyPageNumber <= ) || 
                 ( 
$this->currentlyPageNumber $this->sumPageNumber )
                ){
                    
$this->currentlyPageNumber 1;
                }
                 
            
            
$this->startNumber 
                
$this->currentlyPageNumber $this->pageSize $this->pageSize;
            
$this->endNumber $this->currentlyPageNumber $this->pageSize;
            if( 
$this->total $this->endNumber ){
                
$this->endNumber $this->total;
            }
        }
        
        
/**
        * 得到含有连接的首页翻页标识
        * @return     string
        */
        
function getFirstPage(){
            if( 
== $this->currentlyPageNumber){
                return 
$this->firstPageFlag " n ";    
            }else{
                return 
"<a href=\"?" $this->pageNumberFlag "=1"$this->linksParameter ."\">" $this->firstPageFlag "</a> n";
            }
        }
        
        
/**
        * 得到含有连接的尾页翻页标识
        * @return     string
        */
        
function getLastPage(){
            if( 
$this->currentlyPageNumber != $this->sumPageNumber && != $this->sumPageNumber ){
                 return 
"<a href=\"?" $this->pageNumberFlag "=" $this->sumPageNumber $this->linksParameter "\">" $this->lastPageFlag "</a> n";
            }else{
                 return 
$this->lastPageFlag " n ";    
            }
        }
        
        
/**
        * 得到含有连接的上页翻页标识
        * @return     string
        */
        
function getPreviousPage(){
            if( 
$this->currentlyPageNumber ){
                 return 
"<a href=\"?" $this->pageNumberFlag "=" . ( $this->currentlyPageNumber ) . $this->linksParameter ."\">" $this->previousPageFlag "</a> n";
             }else{
                 return 
$this->previousPageFlag " n";          
             }
        }
        
        
/**
        * 得到含有连接的下页翻页标识
        * @return     string
        */
        
function getNextPage(){
            if( 
$this->currentlyPageNumber $this->sumPageNumber && $this->sumPageNumber != 0){
                 return 
"<a href=\"?" $this->pageNumberFlag "=" . ( $this->currentlyPageNumber ) . $this->linksParameter ."\">" $this->nextPageFlag "</a> n";
             }else{
                 return 
$this->nextPageFlag " n";
             }
        }
        
        
/**
        * 得到翻页的数字条
        * @return     string
        */
        
function getNumberBar(){
            if( 
== ( $this->numberBar ) ){
                
$this->numberBar++;
            }
            
$scope = ( $this->numberBar )/2;
            
$start $this->currentlyPageNumber $scope;
            
$end $this->currentlyPageNumber $scope;
            if( 
$start ){
                
$start 1;
            }
            if( 
$end $this->sumPageNumber ){
                
$end $this->sumPageNumber;
                if( 
$this->sumPageNumber $this->numberBar ){
                    
$start $this->sumPageNumber $this->numberBar 1;
                }
            }
            if( 
== $start && $this->sumPageNumber $this->numberBar ){
                
$end $this->numberBar;
            }

            if( 
$start == $end ){
                return 
"<font color=\"red\">[1]</font>n";
            }
            
            for( 
$i=$start$i<=$end$i++ ){
                if( 
$i == $this->currentlyPageNumber ){
                    
$nb .= "<font color=\"red\">[" $i "]</font>&nbsp;n";
                    continue;
                }
                
$nb .= "<a href=\"?" $this->pageNumberFlag "=" $i $this->linksParameter "\">[" $i "]</a>&nbsp;n";
            }
            
            return 
$nb;
        }
        
        
/**
        * 得到可以跳转的表单框,GET方式传递
        * @return     string
        */
        
function getJumpForm(){
            
$jf .= "<script language=\"javascript\">n";
            
$jf .= "function _checkForm(){n";
            
$jf .= "var a = document.getElementById(\"" $this->pageNumberFlag "\").value;n";
            
$jf .= "if( a > " $this->sumPageNumber " || a <1 || a == " $this->currentlyPageNumber "  ){n";
            
$jf .= "return false;}else{n";
            
$jf .= "window.location.href=\"?" $this->pageNumberFlag "=\"+a+\"" $this->linksParameter "\";";
            
$jf .= "}n}n</script>n";
            
$jf .= "<input class=\"selectButtonCss\" id=\"" $this->pageNumberFlag "\" name=\"" $this->pageNumberFlag "\" tpye=\"text\" size=\"3\" value=\"" $this->currentlyPageNumber "\">n";
            
$jf .= "<input type=\"button\" name=\"button\" value=\"GO\" onClick=\"return _checkForm()\"  class=\"ButtonCss\">n";
            
            return 
$jf;
        }
        
        
/**
        * 得到结果集的开始数
        * @return     int
        */
        
function getStartNumber(){
            return 
$this->startNumber;
        }
        
        
/**
        * 得到结果集的结束数
        * @return     int
        */
        
function getEndNumber(){
            return 
$this->endNumber;
        }
        
        
/**
        * 得到当前页数
        * @return     string
        */
        
function getCurrentlyPageNumber(){
            return 
$this->currentlyPageNumber;
        }
        
        
/**
        * 得到总页数
        * @return     string
        */
        
function getSumPageNumber(){
            return 
$this->sumPageNumber;
        }
        
        
/**
        * 得到翻页时需要的所有参数
        * @return     string
        */
        
function getAllLinksParameter(){
            
$page = empty($_GET[$this->pageNumberFlag])?0:$_GET[$this->pageNumberFlag];
            return 
$this->pageNumberFlag."=".$page.$this->linksParameter;
        }
        
        
/**
        * 设定总的结果集条目数
        * @return     void
        */
        
function setTotal$total ){
            
$this->total $total;
        }
        
        
/**
        * 设定每页需要显示的结果集条目数
        * @return     void
        */
        
function setPageSize$pageSize ){
            
$this->pageSize $pageSize;
        }
        
        
/**
        * 设定翻页时传递页数的变量名称
        * @return     void
        */
        
function setPageNumberFlag$pageNumberFlag ){
            
$this->pageNumberFlag $pageNumberFlag;
        }
        
        
/**
        * 设定翻页时需要额外传递的变量
        * @return     void
        */
        
function setLinksParameter$linksParameter ){
            
$this->linksParameter $linksParameter;
        }
        
        
/**
        * 设定首页翻页标识
        * @return     void
        */
        
function setFirstPageFlag$firstPageFlag ){
            
$this->firstPageFlag $firstPageFlag;
        }
        
        
/**
        * 设定尾页翻页标识
        * @return     void
        */
        
function setLastPageFlag$lastPageFlag ){
            
$this->lastPageFlag $lastPageFlag;
        }
        
        
/**
        * 设定上页翻页标识
        * @return     void
        */
        
function setPreviousPageFlag$previousPageFlag ){
            
$this->previousPageFlag $previousPageFlag;
        }
        
        
/**
        * 设定下页翻页标识
        * @return     void
        */
        
function setNextPageFlag$nextPageFlag ){
            
$this->nextPageFlag $nextPageFlag;
        }
        
        
/**
        * 设定翻页数字条所显示的数量
        * @return     void
        */
        
function setNumberBar$numberBar ){
            
$this->numberBar $numberBar;
        }
        
        function 
__set($name,$val){
            echo 
"不能在Pagination.class.php中SET 属性 <font color=\"red\">" .$name"</font>";
        }
        function 
__get($name){
            echo 
"不能在Pagination.class.php中GET 属性 <font color=\"red\">" .$name"</font>";
        }
        function 
__call($name,$arguments){
            echo 
"Pagination.class.php中不存在 function <font color=\"red\">" .$name"</font>";
        }
        
    }
?>




2008-6-26 11:43 PM#2
查看资料  访问主页  Blog  发短消息  QQ  ICQ 状态  顶部
 
DJ.
新手上路
Rank: 1



UID 129870
精华 0
积分 42
帖子 38
金钱 42 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-24
状态 离线
[推荐阅读] 贴子被删掉了
实在懒得跟你说了
2008-6-26 11:48 PM#3
查看资料  发短消息  顶部
 
DJ.
新手上路
Rank: 1



UID 129870
精华 0
积分 42
帖子 38
金钱 42 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-24
状态 离线
[推荐阅读] 关于php读取pop3邮件


QUOTE:
原帖由 开花石头 于 2008-6-26 11:43 PM 发表
这么多变量在算,不累啊。。。。。
分页只干他该干的事情

<?php
       
        /*
         * @(#)Pagination.class.php 1.0 08/04/15
         *
         * Copyright 2008 www.8y8u.com.cn
         */


        /**
        * <b>通用 ...
我都不知哪招惹您老人家了,处处跟我过不去啊

我发的解释贴子也被删了,不知你看到没有,我根本不像你说的不允许别人不接受我写类的思想什么的

我也不知道为什么就被删了,说的都是很正经的话

另外,我看你贴的这个类不像是你写的吧。我甚至都没细看,一眼扫下去,别的不说,当看到一堆中文在里面就不对了。想想看,当你的站有gb2312编码页面,也有utf-8页面,或其他编码的页面,都要做分页的时候,难道你要把类一份一份复制,保存成不同编码格式?不过后来我想想,你是设为了变量的,这也就没什么太大问题了。还有大问题是每取一样都要调用它的专用函数,谁会记取上一页的函数是什么?谁会记得取下一页的函数又是什么?还有链接呢?难道分页就一定要在当前页打开?还有,当前页就一定要是红色?分页样式就一定要是[number]这样的?跳转表设定ID,难道一个页面就只能用一个跳转表单……可以说,问题非常多
2008-6-27 08:10 AM#4
查看资料  发短消息  顶部
 
diekiss
金牌会员
Rank: 6Rank: 6
高级会员


UID 18243
精华 0
积分 1400
帖子 1376
金钱 1400 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2003-2-3
来自 狮山俊景
状态 离线
[推荐阅读] 还有没有<报仇>那样的小说啊,看上瘾了
可能版主觉得老是硬推你的分页代码,烦了吧



 
 无限风光在险峰
 
2008-6-27 08:15 AM#5
查看资料  访问主页  发短消息  QQ  顶部
 
DJ.
新手上路
Rank: 1



UID 129870
精华 0
积分 42
帖子 38
金钱 42 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-24
状态 离线
[推荐阅读] CMS内容管理系统选型问题
老是硬推我的分页代码?我在这就发了一贴分页类的,不过在类里面那贴他倒没说什么,都是讨论的话
我想问题是出在有一个人发了贴子说让当前页永远居中,我回贴说我5年前就不用这方法了。当然我说的都是事实。当时也没解释,因为要做到那样实在是简单。而且有一个弊端,就是那们只能出来寄数的分页条。另外分页不点着玩的,居中意义不大,应该将更多更大的分页显示在分页条中。不采取居中后我的处理是设一个参数,像这函数里的$intPos,可以设定当前页所在位置。
当然,我丢下一句说5年前就不用了,是有点轻视人的感觉。但我实际没有那样的意思,因为当时很累了,没有精力打这么多字。
石头有可能是替那人抱打不平吧。
这事先错在我。
2008-6-27 08:22 AM#6
查看资料  发短消息  顶部
 
DJ.
新手上路
Rank: 1



UID 129870
精华 0
积分 42
帖子 38
金钱 42 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-24
状态 离线
[推荐阅读] PHP 调用存储过程
有点累,暂时先不说太多

<?php
/********************************  以下是使用例子 ***********************************/
error_reporting(ALL) ;

//PHP程序使用例子
$intCount    1000 ;  // 用你的方法取得记录总数, 这里假设是1000 

//取得mysql的记录数,我可以提供一个函数
function myGetCount$strQuery $pDBC )
{
    
$resResult = @mysql_query $strQuery $pDBC ) ;
    while ( 
$arrRow = @mysql_fetch_row $resResult ) ) 
    {
        
$intCount $arrRow[0] ; 
    }
    @
mysql_free_result$resResult ) ;
    return 
intval$intCount ) ;
}

//两者结合使用的话
//连接数据库
$dbconn mysql_connect 'localhost' 'dbname' 'password' ) ;
mysql_select_db'yourdb' $dbconn  ) ;
//取记录数查询
$strQuery 'SELECT COUNT(`id`) FROM TABLE WHERE 1' ;

$intCount myGetCount$strQuery $dbconn ) ; //取得了记录数

$intCol  10 ;     // 每页显示5个记录

//根据前面两个变量 就可以使用函数完成余下的工作了
$aPDatas doPage $intCount $intCol  ) ;

$intOffset $aPDatas['offset'] ;

print_r($aPDatas) ;
?>
另外,如果你喜欢采用类的话,你可以看这里http://www.bluessoft.com/project/bluepage/
2008-6-27 08:30 AM#7
查看资料  发短消息  顶部
 
void
注册会员
Rank: 2



UID 93558
精华 0
积分 70
帖子 78
金钱 70 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2007-5-24
状态 离线
[推荐阅读] 今天你python了没
别人领悟不到你面向对象精神之所在
对你强烈不满,就把你给XX了
2008-6-27 08:56 AM#8
查看资料  发短消息  顶部
 
void
注册会员
Rank: 2



UID 93558
精华 0
积分 70
帖子 78
金钱 70 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2007-5-24
状态 离线
[推荐阅读] 我想对某些文件保密,不让用户看到该文件的URL,该怎么做
搞PHP的没有几个能真真领悟到OO思想
非常缺乏对问题的抽象能力,很多PHPER都是走极端
2008-6-27 08:58 AM#9
查看资料  发短消息  顶部
 
sanders_yao
版主
Rank: 7Rank: 7Rank: 7
or2 =333


UID 30286
精华 0
积分 2573
帖子 4778
金钱 2570 喜悦币
威望 0
人脉 3
阅读权限 100
注册 2004-7-23
来自 北京 菜户营
状态 离线
[推荐阅读] 代码好写,文档头痛,推广更头痛
至于删帖子的事情 可能是村长养的河蟹干的 你说你在这里时间长 那就应该知道河蟹的事情
常来这里的人一般都有被河蟹贪污帖子的情况 所有的仇请记在村长帐上
欢迎继续交流程序




最近写的一些代码都会在以下地址发布,有兴趣的来批:
http://www.phpx.com/happy/thread-131368-1-1.html
2008-6-27 10:18 AM#10
查看资料  Blog  发短消息  顶部
 62  1/7  1  2  3  4  5  6  7  > 
     


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