PHPSA中的实用类之分页篇 (phpsa系列教程之六)
来源:互联网 发布:水浒传媒 优化方案答案 编辑:程序博客网 时间:2024/06/06 03:54
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>语句, 为访问者设定执行权限.
分页表现在页面中主要是提供了一个可供用户选择页面的分页字串, 先来看看模板中的内容:
[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的这两个方法:
来总结一下分页在PHPSA中的使用一般方法:
1. 取得当前页号
2. 取得总记录数
3. 设置每面显示的记录数
4. 调用Paget::getPageStr()方法生成分页字串
5. 调用业务逻辑类中的查询当前页内容的方法
好, 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()方法:<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>
?>
[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);
}
?>
/**
* 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方法的使用, 大家可以查看相应的说明./**
* 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);
}
?>
来总结一下分页在PHPSA中的使用一般方法:
1. 取得当前页号
2. 取得总记录数
3. 设置每面显示的记录数
4. 调用Paget::getPageStr()方法生成分页字串
5. 调用业务逻辑类中的查询当前页内容的方法
好, PHPSA的角色管理部分就讲到这里, 学习与使用过程中有任何问题,欢迎发邮件与我共同讨论.
- PHPSA中的实用类之分页篇 (phpsa系列教程之六)
- PHPSA中实现缓存方案 (phpsa系列教程之五)
- 在PHPSA框架中处理用户角色(phpsa系列教程之四)
- 使用Form类进行表单字段验证 (phpsa系列教程之二)
- 使用PHPSA搭建你的应用程序(phpsa系列教程之一)
- 编写你的业务逻辑与实现逻辑验证 (phpsa系列教程之三)
- Phpsa Mvc框架使用系列教程(包含示例代码)
- PHPSA 文档
- simpletest for phpsa
- phpsa文档查看已经差不多了....
- 使用PHPSA做静态分析:PHP Smart Analyzer
- Rails3教程系列之六:从Rails2到Rails3
- Rails3教程系列之六:从Rails2到Rails3
- 网站服务管理系统wdcp系列教程之六
- OpenGL系列教程之六:OpenGL顶点数组
- 超越之MongDB系列教程(六) MongDB的查询
- Linux Shell系列教程之(六)Shell数组
- OpenGL系列教程之六:OpenGL顶点数组
- 编写你的业务逻辑与实现逻辑验证 (phpsa系列教程之三)
- 项目经验-林欣灏
- Hearst Castle
- 在PHPSA框架中处理用户角色(phpsa系列教程之四)
- PHPSA中实现缓存方案 (phpsa系列教程之五)
- PHPSA中的实用类之分页篇 (phpsa系列教程之六)
- 纽约穷游TIPS
- 创新性应用-构建移动应用系统-林欣灏
- 忙了一天在asp中写了几个Class,保存起来,不过不知道这样用好不好!!
- WEB测试资料
- Google AdSense
- 资料
- 回忆拉斯维加斯
- 解决问题 提示信息乱码问题