In general PHP development, when php code and html code are mixed, it is relatively simple to handle pagination, and it can also be constructed in the form of a function. In recent development, the structure of Pear::DB + Smarty was used, so I considered paginating the template. Since the page cannot be directly manipulated, I considered generating a paging string.
Because it is a three-tier structure, in the form of class library-->PHP call-->template, all data processing is in the class library, so paging control is performed in the PHP call, and the template complexly parses the result of the call. Let’s look directly at the paging code in our PHP call:
--------------------------------------- ----------------------------------------
<?php
/**
* File: Type.php
* Function: Display books under categories
* Author: heiyeluren
**/
//Contains public files, including class libraries, etc.
require_once("include.php");
//Instantiate the operation object
$Type = new CTypes();
//Number of records per page
define("PAGE_SIZE", 10);
//Get the variables submitted by GET
$TypeID = $tid ? $tid : intval($_REQUEST['tid']);
//Total number of books
$BookTotal = $Type->getBookTotal($TypeID);
/* Paging display core*/
//Get the total number of pages
$pageCount = ($BookTotal/PAGE_SIZE);
//Current page number
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;
}
//Generate paging link string
if ($page==1 && $pageCount>1) {
$pageStr = "Previous page | <a href=/Type.php?tid=".$TypeID."&page=".($page+1).">Next page</a>";
} elseif ($page==$pageCount && $pageCount>1) {
$pageStr = "<a href=/Type.php?tid=".$TypeID."&page=".($page-1).">Previous page</a> | Next page";
} elseif ($page>1 && $page<=$pageCount) {
$pageStr = "<a href=/Type.php?tid=".$TypeID."&page=".($page-1).">Previous page</a> |
<a href=/Type.php?tid=".$TypeID."&page=".($page+1).">Next page</a>";
} else {
$pageStr = "Previous page | Next page";
}
//Get the current record according to the number of pages
$allBook = $Type->getBookFromType($TypeID, $start=$startNum, $offset=PAGE_SIZE);
//Smarty variable assignment
$tpl->assign('BookTotal', $BookTotal);
$tpl->assign('allBook', $allBook);
$tpl->assign('pageStr', $pageStr);
$tpl->display('Type.html');
unset($Type);
?>
-------------------------------------------------- ----------------------------------
For a clearer understanding, here is a brief description of the basic contents of the class library: (The code is incomplete)
-------------------------------------------------- ----------------------------------
<?php
/**
* File: Type.class.php
* Function: Type processing class
* www.knowsky.com
* Author: heiyeluren
**/
classType
{
var $mDsn;
var $mTableName;
var $hPearDB;
//Constructor
functionType()
{
//...
}
//Get the handle method of the pear DB class
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;
}
//Get the total number of books
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;
}
//Get all books
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;
}
}
?>
-------------------------------------------------- ----------------------------------
Finally, let us take a look at how this Type.html template is processed:
-------------------------------------------------- ----------------------------------
{*Insert header file*}
{include file="Cendar/head.html"}
<div id="side">
<ul>
<li>Total number of books: {$BookTotal}</li>
</ul>
</div>
<div id="Book">
<h2 class="cata">The specific content of the book</h2>
<ul>
{section name=Book loop=$allBook}
<li><a href="show_Book.php?tid={$allBook[Book].id}">{$allBook[Book].title}</a></li>
{sectionelse}
<li class="warning">There are currently no books</li>
{/section}
</ul>
</div>
{* Pagination string display*}
<div align="right">
{"GBK"|iconv:"utf-8":$pageStr}
</div>
{* 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 processing 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