When PHP code and html code are mixed in normal php development, it is relatively simple to process pages. You can also build them into functions. Recently, the Pear: DB + Smarty structure is used in development. Therefore, if you want to paging the template, you should consider generating a paging string because you cannot directly operate the page.
Because it is a three-tier structure, the class library --> PHP call --> template form, all the data processing is in the class library, so paging control is carried out in the PHP call, the template parses the call results in a complex way. Let's look at the paging code in PHP calls:
--------------------------------------------------------------------------------
<? Php
/**
* File: Type. php
* Function: displays books under a category.
* Author: heiyeluren
**/
// Contains public files, including class libraries
Require_once ("include. php ");
// Instantiate the operation object
$ Type = new CTypes ();
// Number of records per page
Define ("PAGE_SIZE", 10 );
// GET the variable submitted by GET
$ TypeID = $ tid? $ Tid: intval ($ _ REQUEST [tid]);
// Total number of books
$ BookTotal = $ Type-> getBookTotal ($ TypeID );
/* Display the core by page */
// Obtain 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 a 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 ";
}
// Obtain the current record by page number
$ AllBook = $ Type-> getBookFromType ($ TypeID, $ start = $ startNum, $ offset = PAGE_SIZE );
// Assign values to the Smarty variable
$ Tpl-> assign (BookTotal, $ BookTotal );
$ Tpl-> assign (allBook, $ allBook );
$ Tpl-> assign (pageStr, $ pageStr );
$ Tpl-> display(Type.html );
Unset ($ Type );
?>
--------------------------------------------------------------------------------
For a clearer understanding, the following describes the basic content in the class library: (the code is incomplete)
--------------------------------------------------------------------------------
<? Php
/**
* File: Type. class. php
* Function: Type processing class
* Www.knowsky.com
* Author: heiyeluren
**/
Class Type
{
Var $ mDsn;
Var $ mTableName;
Var $ hPearDB;
// Constructor
Function Type ()
{
//...
}
// Obtain 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;
}
// Obtain 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;
}
// Obtain 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 a 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"> book content
<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"> 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 "}
--------------------------------------------------------------------------------
So we can grasp the key point and know that we can control $ pagStr from the PHP program, which is our paging string. Finally, it will be replaced with the template file to achieve the effect.
Basically, you can 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 it is convenient to call it everywhere. Haha ~~~
(Do not reprint the above Code without authorization)
Author: heiyeluren
Date: 2005-8-2