分页是网站常用的一项
功能,如果有
一个良好的
分页类(
函数),可以帮开发者节省不少开发时间. 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 ) ; //在适当位置
输出或赋值给
一个模板变量 ?>
效果图: <a href="http://www.bluessoft.com/project/bluepage/e1.jpg" title="
效果图"></a> 使用例子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同级目录下有一目录保存con
fig,如./con
fig目录,而你将 page.abc.inc.
PHP保存在./con
fig目录了。 <?
PHP $pBP->_encode= false ;//不过滤query string $aPDatas = $pBP->get( $intCount,$intShowNum ); $strHtml = $pBP->getHTML( $aPDatas,'./con
fig/page.abc.inc.
PHP' ); //路径要正确 ?> 请根据你的
页面输出编码,保存相应编码格式。就像你做模板一样。 如果你的
页面是utf-8格式的,请保存
配置文件为utf-8格式。注意,只是改page.abc.inc.
PHP编码,类
文件的编码请不要改动。 补充一点: 如果觉得没有取记录总数的
函数不方便,你可以自已在类里面
加上取总数的
函数,或者使用外部
函数 我们在实际应用中,取记录数的
方法是跟随项目对象的,所以一般不加在
分页类里面. 如果你没有自己取记录数的
方法,你可以在
分页类中
加上,或者加到外部 [
PHP] 程序示例: <?
PHP //这是
MysqL的
函数,你可以加
一个名为msGetCount的
函数支持ms
sql //加到类里面,或作为外部
函数 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 = @ms
sql_query ( $strQuery,$pDBC ) ; while ( $arrRow = @ms
sql_fetch_row ( $resResult ) ) { $intCount = $arrRow[0]; } @ms
sql_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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。