PHP common development in the case of PHP code and HTML code inclusions in the situation of processing paging is relatively simple, can also be built into the form of a function. Recently developed using Pear::D B + Smarty structure, so consider if the template is paged, because the page cannot be directly manipulated, so consider the form of a raw page string.
Because it is a three-tier structure, the class library-->php call-to-template form, all data processing is in the class library, then the paging control is in the PHP call, the template is the result of complex parsing calls. Just look at the paging code in our PHP call:
--------------------------------------------------------------------------------
/**
* File: type.php
* Function: Display the books under the category
* Author: Heiyeluren
**/
Contains public files, including class libraries, etc.
Require_once ("include.php");
Instantiating an Action object
$Type = new cTYPES ();
Number of records per page
Define ("Page_size", 10);
Get the variables for a get commit
$TypeID = $tid? $tid: Intval ($_request[tid]);
Total Books
$BookTotal = $Type->getbooktotal ($TypeID);
/* Pagination Display core */
Get Total Pages
$pageCount = ($BookTotal/page_size);
Current page
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;
}
Raw Component Page Link string
if ($page ==1 && $pageCount >1) {
$PAGESTR = "Previous Page | Next Page ";
} elseif ($page = = $pageCount && $pageCount >1) {
$PAGESTR = "Previous Page | Next Page ";
} elseif ($page >1 && $page <= $pageCount) {
$PAGESTR = "Previous Page |
Next Page ";
} else {
$PAGESTR = "Previous Page | Next Page ";
}
Get the current record by page count
$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 clearer understanding, the following is a simple description of the basic content in the class Library: (Incomplete code)
--------------------------------------------------------------------------------
/**
* File: Type.class.php
* Function: Type processing class
* www.knowsky.com
* Author: Heiyeluren
**/
Class Type
{
var $mDsn;
var $mTableName;
var $hPearDB;
constructor function
function Type ()
{
//...
}
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's look at how this type.html template is handled:
--------------------------------------------------------------------------------
{* Insert header file *}
{include file= "cendar/head.html"}
- Total books: {$BookTotal}
Specific content of the book
{section Name=book loop= $allBook}
- {$allBook [Book].title}
{Sectionelse}
- There are no books at present
{/section}
{* Pagination string display *}
{"GBK" |iconv: "Utf-8": $pageStr}
{* Insert bottom file *}
{include file= "cendar/foot.html"}
--------------------------------------------------------------------------------
So let's get to the point where we know that we can control $pagstr from the PHP program, which is our paging string, and it will be replaced in the template file to achieve the effect.
Basically here to understand how to do pagination in the template, of course, you can also write the paging function, or encapsulated into a class, it is convenient to call around. hehe ~ ~ ~
(Do not reprint the above code without authorization)
Author:heiyeluren
Date:2005-8-2
http://www.bkjia.com/PHPjc/508379.html www.bkjia.com true http://www.bkjia.com/PHPjc/508379.html techarticle PHP Common development in the case of PHP code and HTML code inclusions in the situation of processing paging is relatively simple, can also be built into the form of a function. Recently developed using Pear::D B + Smarty structure, in ...