Home >Backend Development >PHP Tutorial >Processing of template pagination in PHP_PHP tutorial
PHP普通开发中php代码和html代码夹杂的情况中处理分页是比较简单的,也可以构建成函数的形式。最近开发中使用 Pear::DB + Smarty 的结构,于是考虑如果对模板进行分页,因为不能直接操作页面,所以就考虑生成分页字符串的形式。 因为是三层结构,类库-->PHP调用-->模板的形式,所有的数据处理是在类库里的,那么分页控制就在PHP调用中进行的,模板就复杂解析调用的结果。先直接看我们PHP调用中的分页代码: -------------------------------------------------------------------------------- //包含公共文件,包括类库等 //实例化操作对象 //每页记录数 //获取GET提交的变量 //书籍总数 /* 分页显示核心 */ //按照页数获取当前记录 //Smarty变量赋值 $tpl->display(Type.html); unset($Type); ?> 为了更清晰的认识,下面简单的描述一下类库中的基本内容:(代码不完整) -------------------------------------------------------------------------------- class Type //构造函数 //获取书籍总数 //获取所有书籍 } -------------------------------------------------- ----------------------------------
/**
* 文件:Type.php
* 功能:显示类别下的书籍
* 作者:heiyeluren
**/
require_once("include.php");
$Type = new CTypes();
define("PAGE_SIZE", 10);
$TypeID = $tid ? $tid : intval($_REQUEST[tid]);
$BookTotal = $Type->getBookTotal($TypeID);
//获取总页数
$pageCount = ($BookTotal/PAGE_SIZE);
//当前页数
if (isset($_GET[page]) && !empty($_GET[page])) {
$page = intval($_GET[page]);
} else {
$page = 1;
}
if ($page==1) {
$startNum = 0;
} else {
$startNum = ($page-1) * PAGE_SIZE;
}
//生成分页链接字符串
if ($page==1 && $pageCount>1) {
$pageStr = "上一页 | 下一页";
} elseif ($page==$pageCount && $pageCount>1) {
$pageStr = "上一页 | 下一页";
} elseif ($page>1 && $page<=$pageCount) {
$pageStr = "上一页 |
下一页";
} else {
$pageStr = "上一页 | 下一页";
}
$allBook = $Type->getBookFromType($TypeID, $start=$startNum, $offset=PAGE_SIZE);
$tpl->assign(BookTotal, $BookTotal);
$tpl->assign(allBook, $allBook);
$tpl->assign(pageStr, $pageStr);
--------------------------------------------------------------------------------
/**
* 文件:Type.class.php
* 功能:Type处理类
* www.knowsky.com
* 作者:heiyeluren
**/
{
var $mDsn;
var $mTableName;
var $hPearDB;
function Type()
{
//...
}
//获得pear DB类的句柄方法
function _getDBClass($fetchMode = DB_FETCHMODE_ASSOC)
{
if(!is_object($this->hPearDB)){
$this->hPearDB = DB::connect($this->mDsn);
$this->hPearDB->query("set names utf8");
$this->hPearDB->setFetchMode($fetchMode);
if(DB::IsError($this->hPearDB)){
return false;
}
}
return $this->hPearDB;
}
function getBookTotal($TypeId)
{
$db = $this->_getDBClass();
$sql = "SELECT COUNT(*) AS total FROM ...";
$rs = $db->getOne($sql);
if (DB::isError($rs))
return $rs->getMessage();
else
return $rs;
}
function getBookFromType($TypeId, $start, $offset)
{
$db = $this->_getDBClass();
$sql = "SELECT * FROM ... LIMIT $start,$offset";
$rs = $db->getAll($sql);
if (DB::isError($rs))
return $rs->getMessage();
else
return $rs;
}
?>
--------------------------------------------------------------------------------
最后再让我们看一下这个Type.html模板是如何处理的:
{* Insert header file*}
{include file ="Cendar/head.html"}
Book specific content
{section name=Book loop=$allBook}
{sectionelse}
{/ section}
{* paging string display*}
{"GBK"| iconv:"utf-8":$pageStr}
{* Insert bottom file*}
{include file="Cendar/foot.html"}
--- -------------------------------------------------- --------------------------
Then we grasp the key point and know that we can control $pagStr from the PHP program to be our paging string, and finally it will be replaced in the template file to achieve the effect.
At this point, you basically understand how to perform paging in the template. Of course, you can also write the paging function as a function, or encapsulate it into a class, so that it can be called everywhere. Haha~~~
(Please do not reprint the above code without authorization)
Author: heiyeluren
Date: 2005-8-2