Home  >  Article  >  Backend Development  >  Processing of template pagination in PHP_PHP tutorial

Processing of template pagination in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:01933browse

PHP普通开发中php代码和html代码夹杂的情况中处理分页是比较简单的,也可以构建成函数的形式。最近开发中使用 Pear::DB + Smarty 的结构,于是考虑如果对模板进行分页,因为不能直接操作页面,所以就考虑生成分页字符串的形式。

因为是三层结构,类库-->PHP调用-->模板的形式,所有的数据处理是在类库里的,那么分页控制就在PHP调用中进行的,模板就复杂解析调用的结果。先直接看我们PHP调用中的分页代码:

--------------------------------------------------------------------------------
/**
* 文件:Type.php
* 功能:显示类别下的书籍
* 作者:heiyeluren
**/

//包含公共文件,包括类库等
require_once("include.php");

//实例化操作对象
$Type = new CTypes();

//每页记录数
define("PAGE_SIZE", 10);

//获取GET提交的变量
$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);

//Smarty变量赋值
$tpl->assign(BookTotal, $BookTotal);
$tpl->assign(allBook, $allBook);
$tpl->assign(pageStr, $pageStr);

$tpl->display(Type.html);

unset($Type);

?>


--------------------------------------------------------------------------------

 

为了更清晰的认识,下面简单的描述一下类库中的基本内容:(代码不完整)

 

--------------------------------------------------------------------------------
/**
* 文件:Type.class.php
* 功能:Type处理类
* www.knowsky.com
* 作者:heiyeluren
**/

class Type
{
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"}



  • Total number of books: {$BookTotal}




Book specific content




{* 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508379.htmlTechArticleIn normal PHP development, it is relatively simple to handle paging when php code and html code are mixed. It can also be constructed as The form of the function. The structure of Pear::DB + Smarty was used in recent development, in...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn