Define (' Base_path ', $_server[' document_root ');
Define (' Smarty_path ', ' \smarttest\smarty\\ ');
Require Base_path. Smarty_path. ' Smarty.class.php ';
/* $dir 2 of this path shown to the performance page is the same as the following string, causing Smarty to find the templates path */
$dir 2 = "... /smarty/tempplates/";
Class Smartyproject extends smarty{
function Smartyproject () {
/* Must add this parent::__construct (), otherwise, Smarty will not be constructed, truncate, etc. cannot be used,
* But it doesn't seem to make sense, but in fact,
* This does solve the problem that truncate cannot use */
Parent::__construct ();
$this->template_dir = Base_path. Smarty_path. ' /templates/';
$this->compile_dir = Base_path. Smarty_path. ' /templates_c/';
$this->config_dir = Base_path. Smarty_path. ' /configs/';
$this->cache_dir = Base_path. Smarty_path. ' /cache/';
}
}
Class conndb{
var $dbtype;
var $host;
var $user;
var $pwd;
var $dbname;
var $debug;
var $conn;
function Conndb ($dbtype, $host, $user, $pwd, $dbname, $debug =false) {//constructor
$this->dbtype= $dbtype;
$this->host= $host;
$this->user= $user;
$this->pwd= $pwd;
$this->dbname= $dbname;
$this->debug= $debug;
}
function Getconnid () {
Require Base_path. ' /smarttest/adodb5/adodb.inc.php ';//Refer the ADODB Master file in
if ($this->dbtype = = = ' MySQL ') {
$this->conn = newadoconnection (' mysql ');
$this->conn->connect ($this->host, $this->user, $this->pwd, $this->dbname);
}else if ($this->dbtype = = ' mssql ') {
$this->conn = newadoconnection (' mssql ');
$this->conn->connect ($this->host, $this->user, $this->pwd, $this->dbname);
}else if ($this->dbtype = = = ' Access ') {
$this->conn = newadoconnection (' access ');
$this->conn->connect ("Driver={microsoft Access Driver (*.mdb)};D bq=". $this->dbname. "; Uid= ". $this->user."; Pwd= ". $this->pwd."; ");
}
$this->conn->execute ("Set names Utf-8");
if ($this->dbtype = = ' MySQL ')
$this->conn->debug = $this->debug;
return $this->conn;
}
function Closeconnid () {
$this->conn->disconnect ();
}
}
Class admindb{
function Execsql ($SQLSTR, $conn) {
$sqltype = Strtolower (substr (Trim ($SQLSTR), 0,6));//Go to the first 6 characters of a SQL statement
$rs = $conn->execute ($SQLSTR);
if ($sqltype = = ' select ') {
$array = $rs->getrows ();
if (count ($array) ==0 | | $rs = = FALSE)
return false;
Else
return $array;
}else if ($sqltype = = ' Update ' | | $sqltype = = ' Insert ' | | $sqltype = = ' Delete ') {
This write does not return a result set, or return an empty collection
if ($RS)
return true;
Else
return false;
}
}
}
Class seppage{
var $rs;
var $pagesize;
var $nowpage;
var $array;
var $conn;
var $sqlstr;
function ShowData ($sqlstr, $conn, $pagesize, $nowpage) {
if (!isset ($nowpage) | | $nowpage = = "") {
$this->nowpage = 1;
}else{
$this->nowpage = $nowpage;
}
$this->pagesize = $pagesize;
$this->conn = $conn;
$this->sqlstr = $sqlstr;
Execute Query statement
$this->rs = $this->conn->pageexecute ($this->sqlstr, $this->pagesize, $this->nowpage);// Call this method in the Adodo class
@ $this->array = $this->rs->getrows ();
if (count ($this->array) ==0| | $this->rs = = False)
return false;
Else
return $this->array;
}
function ShowPage ($contentname, $utits, $anotherserchstr, $anotherserchstrs, $class) {
$allrs = $this->conn->execute ($this->sqlstr); Execute Query statement
$record = count ($allrs->getrows ());//Total number of statistical records
$pagecount = Ceil ($record/$this->pagesize);//Calculate the total number of pages
@ $str. = "Total". $contentname. " ". $record." ". $utits." Display per page. $this->pagesize.
". $utits.". $this->rs->absolutepage (). page/Total. $pagecount. "Page";
$str. = "";
if (! $this->rs->atfirstpage ())
$str. = "$anotherserchstrs." Class= ". $class." > Home ";
Else
$str. = "Home";
$str. = "";
if (! $this->rs->atfirstpage ())
$str. = "Rs->absolutepage ()-1)." ¶meter1= ". $anotherserchstr." ¶meter2= ".
$anotherserchstrs. " Class= ". $class." > Previous page ";
Else
$str. = "Previous page";
$str. = "";
if (! $this->rs->atlastpage ())
$str. = "Rs->absolutepage () +1)." ¶meter1= ". $anotherserchstr." ¶meter2= ".
$anotherserchstrs. " Class= ". $class." > next Page ";
Else
$str. = "Next page";
$str. = "";
if (! $this->rs->atlastpage ())
$str. = "$anotherserchstrs." Class= ". $class." > Last ";
Else
$str. = "Last";
if (count ($this->array) = = 0| | $this->rs==false)
Return "";
Else
return $str;
}
}
The above code defines several classes, which are used to implement database connection, operation database, paging implementation, database storage, and a subclass of Smarty class, in fact, this subclass does not implement any special function. Name it as
smartydao.php or this any name can be, of course, there is one thing to note is that I tried the require statement, for these paths, everyone should go to their corresponding directory.
Require ' smartydao.php ';
Database Class Instance
$conobj = new Conndb ("MySQL", "localhost", "root", "brave", "bank");
$conn = $conobj->getconnid ();
Database Operation Class Object
$admindb = new AdminDB ();
Smarty Template Configuration Class object
$smarty = new Smartyproject ();
$seppage = new Seppage ();//Instantiate paging class
This file is an instantiation of the class in the file above, can be named smartydaoimpl.php, or named your own favorite name, the function is very simple, right?
Then the job is to create a test case randomly, to test the paging code, of course, the test also contains two files, one is the controller, another view, the controller code is:
Include_once ' conn/smartydaoimpl.php ';
$arr _page = $seppage->showdata ("SELECT * from Localinfo", $conn, 5, $_get[' page ']);//Perform paged query
if (! $arr _page)
$smarty->assign (' page ', ' F ');
else {
$smarty->assign (' page ', ' T ');
$smarty->assign (' showpage ', $seppage->showpage ("Record", "One", "" "," "," A1 "));//Assignment page template
$smarty->assign (' Arr_page ', $arr _page);//Copy query records to template
}
$smarty->display (' view/fenyeshow.html ');//Specify a template to display the data
Take a name, it's called smarty_adodb_fenye.php. As you can see, we used it at the beginning:
Include_once ' conn/smartydaoimpl.php ';
Here, the previous two files are placed under the Conn folder, and the test case is written in the Conn sibling directory
And then the view file, where the view file is put, don't put it wrong. See the first class we wrote with such a statement
$this->template_dir = Base_path. Smarty_path. ' /templates/';
This is the absolute path that specifies the view file, because,
$smarty->display (' view/fenyeshow.html ');//Specify a template to display the data
This statement,
$smarty->display (' view/fenyeshow.html ');//Specify a template to display the data
So, we're going to
Base_path. Smarty_path. ' /templates/
Under the path to create a folder named View, and then build a file named Fenyeshow.html, of course you can take your favorite name, but to ensure that the specified consistency in the program
The contents of the view template are as follows:
<title>Insert Title here</title>
{section name=id loop= $arr _page}
| {$arr _page[id]. ID} |
{$arr _page[id].temperature} |
{$arr _page[id].illumination} |
{$arr _page[id].moisture} |
{$arr _page[id].times|truncate:5: "..."} |
{/section}
| {$showpage} |
OK, done, as long as in the browser to locate smarty_adodb_fenye.php This file, start the server, OK, you will see the paging effect.
I am doing in the process, also encountered some problems, one is not try truncate statement, the problem is very strange, to the Smarty official website looked down, let, smarty sub-class instantiation, will not instantiate its parent class smarty, therefore, will lead to truncate can not be used, The workaround, naturally, is to call the parent class constructor method in the subclass.
Excerpted from 0+0+0+...=1
http://www.bkjia.com/PHPjc/478454.html www.bkjia.com true http://www.bkjia.com/PHPjc/478454.html techarticle ? PHP define (Base_path,$_server[document_root]); define (smarty_path,\smarttest\smarty\\); Require Base_path. Smarty_path. Smarty.class.php; /* $dir 2 of this path shows to the performance page is this ...