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


分页是网站常用的一项功能,如果有一个良好的分页类(函数),可以帮开发者节省不少开发时间。

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:

<?php
include ( "lib/BluePage.class.php" ) ;
$pBP = new BluePage ;
$intCount = 1000 ; // 假设记录总数为1000
$intShowNum = 10 ; // 每页显示10
$aPDatas = $pBP->get( $intCount , $intShowNum ) ;
$strHtml = $pBP->getHTML( $aPDatas ) ; //在适当位置输出或赋值给一个模板变量
?>

效果图:

使用例子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 ); //取得分页数据
?>

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

[/php]

主页地址:http://www.bluessoft.com/project/bluepage/

下载地址:http://www.bluessoft.com/project/bluepage/BluePage.tar.gz

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3