pagination | templates
PHP common development in the case of PHP code and HTML code mixed with the situation in the process of paging is relatively simple, can also be built into a functional form. Using Pear::D B + Smarty structure in recent development, it is considered that if you page through a template because you cannot manipulate the page directly, consider the form of a raw page string.
Because it is a three-tier structure, class library-->php call the form of--> template, all the data processing is in the class library, then paging control in the PHP call, the template on the complex resolution of the result of the call. First look directly at the paging code in our PHP call:
--------------------------------------------------------------------------------
<?php
/**
* 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 object of action
$Type = new cTYPES ();
Number of records per page
Define ("Page_size", 10);
Get the variable submitted by getting
$TypeID = $tid? $tid: Intval ($_request[' tid '));
//Books total
$BookTotal = $Type->getbooktotal ($TypeID);
/* pagination display core */
//Get total page number
$pageCount = ($BookTotal/page_size);
Current pages
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 ingredient page 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). " > Prev </a> | Next Page ";
} elseif ($page >1 && $page <= $pageCount) {
$PAGESTR = "<a href=/type.php?tid=". $TypeID. " &page= ". ($page-1). " > Prev </a> |
<a href=/type.php?tid= ". $TypeID." &page= ". ($page + 1). " > next page </a> ";
} else {
$PAGESTR = [Previous] | Next Page ";
}
Get current record by page
$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's a simple description of the basics in the class Library: (Code incomplete)
--------------------------------------------------------------------------------
<?php
/**
* File: Type.class.php
* Function: Type processing class
* Author: Heiyeluren
**/
Class Type
{
var $mDsn;
var $mTableName;
var $hPearDB;
Constructors
function Type ()
{
//...
}
Get the handle method for 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 the 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 take a look at how this type.html template is handled:
--------------------------------------------------------------------------------
{* Insert header file *}
{include file= "cendar/head.html"}
<div id= "Side" >
<ul>
<li> Total Books: {$BookTotal}</li>
</ul>
</div>
<div id= "book" >
<H2 class= "Cata" > Book Specific 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" > There are 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 have to grasp the point that we can control $pagstr from the PHP program is our paging string, and finally it will be replaced in the template file to achieve the effect.
Basically here to understand how to do paging in the template, of course, you can also write the paging function, or packaged into a class, so it is convenient to call around. Oh ~ ~ ~ ~
(without authorization, please do not reprint the above code)
Author:heiyeluren
Date:2005-8-2