当前位置: 首页 > 图文教程 > 网络编程 > PHP > BluePage通用分页类助开发者提高开发效率

PHP
PHP 开发环境的选择、建立及使用(5)
PHP 开发环境的选择、建立及使用(6)
PHP 开发环境的选择、建立及使用(7)
PHP 开发环境的选择、建立及使用(8)
PHP 开发环境的选择、建立及使用(9)
Win2003下APACHE PHP5 MYSQL4 PHPMYADMIN 的简易安装配置
PHP新手上路(八) 文件上传
PHP新手上路(九) 投票系统
PHP新手上路(十) 简易banner动态更替
PHP新手上路(十一) 数据库链接
PHP新手上路(十二)使用PHP来操作Oracle数据库
PHP新手上路(十三)PHP资源
PHP新手上路(十四) 其他杂项
session全教程(一)
session全教程(二)
session全教程(三)
PHP编码规范
第十五节--Zend引擎的发展 -- Classes and Objects in PHP5 [15]
第十四节--命名空间 -- Classes and Objects in PHP5 [14]
第十二节--类的自动加载 -- Classes and Objects in PHP5 [12]

PHP 中的 BluePage通用分页类助开发者提高开发效率


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 197 ::
收藏到网摘: n/a

1 分页要与SQL无关。为什么分页必须与SQL无关呢?很显然,有一定开发经验的朋友,至少都在使用数据库代理类。我们不应该在分页类中global或传递数据库类,至于直接把数据库连接放进去,那更要不得。另外请看第5点。

2 分页要与html及样式分离。与html分离是显而易见的需要的,首先,就算是同一个网站,也会需要不同风格的分页,甚至有些用的还是图片。其次,可以应付不同编码格式。另外,与html和样式分离,即相当于PHP的模板技术一处道理,美工可以修改出自己想要的效果。


3 要考虑变量值问题,并非所有分页都是数字的。比如有些人分页是page=pn123,甚至是page=p123nfadfafdaf。分页类要提供这方面的处理。

4 web协议不仅仅限于http。很多分页类内部就把协议定死了http了,这是不可取的。

仅仅上面最基本的四点,现在所能看到的所谓分页类中,能做到其中两个的,基本上没有。

好吧。我们开始。

5 分页不仅限于数据库分页。有些是对内容的分页,比如把一篇长达上万字的内容进行分页,这样如果把SQL写到类里面的,就更要不得了,一点用也没有。

BluePage是一个通用的分页类,它能帮助你更快地完成分页任务。

先看使用例子: http://www.bluessoft.com/project/bluepage/example.php

使用例子1:

 
使用例子2:

以下为引用的内容:
<?php
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;
$intCount = 1000 ; // 假设记录总数为1000
$intShowNum = 10 ; // 每页显示10
$aPDatas = $pBP->get( $intCount , $intShowNum ) ;
//print_r($aPDatas); //如不记得返回,打印出来看看
?>

 
使用例子3:

以下为引用的内容:
<?php
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;
$intCount = 1000 ; // 假设记录总数为1000
$intShowNum = 10 ; // 每页显示10
$aPDatas = $pBP->get( $intCount , $intShowNum ) ;
//第二个参数指定配置文件
$strHtml = $pBP->getHTML( $aPDatas , "myBPDiy.inc.php") ;
?>

输出控制:

以下为引用的内容:

<?php
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;
$intCount = 1000 ; // 假设记录总数为1000
$intShowNum = 10 ; // 每页显示10
$aPDatas = $pBP->get( $intCount , $intShowNum ) ;

// f 首页
// pg 上一组页码
// p 上一页
// bar 分页条
// ng 下一组页码
// n 下一页
// m 总页数
// sl 下拉选页
// i Input表单
$pBP->_order = 'm|sl' ; //只输出总页数与下拉选页
$strHtml = $pBP->getHTML( $aPDatas ) ;
?>

 
使用例子5

以下为引用的内容:
<?php
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;
$intCount = 1000 ; // 假设记录总数为1000
$intShowNum = 10 ; // 每页显示10
$pBP->_getlink = false ; // 取消取得链接
$pBP->_getqs = false ; // 取消取得Query String
//返回分页数字(省资源)
$aPDatas = $pBP->get( $intCount, $intShowNum );
//print_r($aPDatas); //打印出来看看

 

//只要最大页,上一页,与下一页和当前页以及offset返回(最省资源)
$aPDatas = $pBP->get( $intCount, $intShowNum , 0 );
//print_r($aPDatas); //打印出来看看
?>

 
取得offset

以下为引用的内容:

<?php
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;
$intCount = 1000 ; // 假设记录总数为1000
$intShowNum = 10 ; // 每页显示10
$aPDatas = $pBP->get( $intCount, $intShowNum );
$offset = $aPDatas['offset'] ;
?>

 
非数据库分页:

比如有一篇文章长度是10000字节,要想每2000字节分为一页,那怎么办呢?

以下为引用的内容:

<?php
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;
$strLen = strlen($strSubContent); //假设内容总长度,这个自己计算取得
$strSubLen = 2000 ; // 每页数据长度
$aPDatas = $pBP->get( $strLen, $strSubLen );
$offset = $aPDatas["offset"] ;
//取得当前页的内容
$strSubContent = fn_substr( $strSubContent, $offset , $strSubLen ) ; //截取函数自己写
?>

一些属性:

8.1 你使用的变量不是page,而是其他,比如是 pn :

以下为引用的内容:

<?php
$pBP->_var = 'pn' ;
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>

8.2 $this->_prefix有什么作用?

当你的分页是类似于page=pp123这样的数字前面有字符的时候,$this->_prefix就有用了 

以下为引用的内容:
<?php
$pBP->_prefix = 'pp' ; // 如page=pp123的 pp
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>

8.3 $this->_postfix有什么作用? :

当你的分页是类似于page=123p这样的数字后面有字符的时候,$this->_postfix就有用了

以下为引用的内容:

<?php
$pBP->_postfix = 'p' ; // 如page=123p的 p
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>


8.4 $this->_prefix和$this->_postfix能否同时使用? :

当然可以。当你的分页是类似于page=pn123ccc 这样的数字后面有字符的时候,就两个一起用

以下为引用的内容:

<?php
$pBP->_prefix = 'pn' ;
$pBP->_postfix = 'ccc' ;
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>

8.5 $this->_pos有什么用? :

它的作用是 当前页在分页条中的位置设定,比如设为3,当前页是8,那么数字8就分处在分页条的第三位即: 6 7 8 9 10 11 12 13 14 15

以下为引用的内容:

<?php
$pBP->_pos = 5 ; //把当前页放到第五位
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>


8.6 $this->_symbol有什么用? :

连接符

以下为引用的内容:

<?php
$pBP->_symbol= '&' ; //使用&为链接符
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>

8.7 $this->_getqs有什么用? :

是否取得Query String。默认取得,为false则不取得。可节省资源,但如果要取得链接与html的时候,它会为true

以下为引用的内容:

<?php
$pBP->_getqs = false ;
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>


8.8 $this->_getlink有什么作用? :

this->_getlink默认为true,即表示取得分页的链接,为false时,有关*ln键名的变量,都不会有值它的作用在于,1 适用于手工设置链接的人 2 节省资源

以下为引用的内容:

<?php
$pBP->_getlink = false ;
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>


8.9 $this->_encode有什么作用? :

$this->_encode默认为true,即表示使用htmlspecialchars对Query String过滤

以下为引用的内容:

<?php
$pBP->_encode= false ;//不过滤query string
$aPDatas = $pBP->get( $intCount, $intShowNum );
?>


最后:

关于BluePage.default.inc.php配置文件

这个是默认的配置文件。你可以将面的内容拷贝一份,保存为另一个配置。比如命名为page.abc.inc.php 假设当前访问的是list.php文件,在list.php同级目录下有一目录保存config,如./config目录,而你将page.abc.inc.php保存在./config目录了。

以下为引用的内容:

<?php
$pBP->_encode= false ;//不过滤query string
$aPDatas = $pBP->get( $intCount, $intShowNum );
$strHtml = $pBP->getHTML( $aPDatas, './config/page.abc.inc.php' ); //路径要正确
?>


 
请根据你的页面输出编码,保存相应编码格式。就像你做模板一样。

如果你的页面是utf-8格式的,请保存配置文件为utf-8格式。注意,只是改page.abc.inc.php编码,类文件的编码请不要改动。

补充一点:

如果觉得没有取记录总数的函数不方便,你可以自已在类里面加上取总数的函数,或者使用外部函数

我们在实际应用中,取记录数的方法是跟随项目对象的,所以一般不加在分页类里面.

如果你没有自己取记录数的方法,你可以在分页类中加上,或者加到外部

[php]

程序示例:

以下为引用的内容:

<?php
//这是mysql的函数,你可以加一个名为msGetCount的函数支持mssql
//加到类里面,或作为外部函数
function myGetCount( $strQuery , $pDBC )
{
$resResult = @mysql_query ( $strQuery , $pDBC ) ;
while ( $arrRow = @mysql_fetch_row ( $resResult ) )
{
$intCount = intval($arrRow[0]);
}
@mysql_free_result( $resResult ) ;
return $intCount ;
}

//这是SQLserver的函数
//加到类里面,或作为外部函数
function msGetCount( $strQuery , $pDBC )
{
$resResult = @mssql_query ( $strQuery , $pDBC ) ;
while ( $arrRow = @mssql_fetch_row ( $resResult ) )
{
$intCount = $arrRow[0];
}
@mssql_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' ;

include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;

//作为外部函数时
$intCount = myGetCount( $strQuery , $dbconn ) ; //取得了记录数
//如果是SQLserver
$intCount = msGetCount( $strQuery , $dbconn ) ; //取得了记录数

//作为类的方法时
$intCount = $pBP->myGetCount( $strQuery , $dbconn ) ;//取得了记录数
//如果是SQLserver
$intCount = $pBP->msGetCount( $strQuery , $dbconn ) ;//取得了记录数

$pBP->get( $intCount, 10 ); //取得分页数据
?>

当然,我们并不鼓励将数据库操作放入分页类中 。