Php trial of smarty and ADODB for paging Data Reading

Source: Internet
Author: User

<? Php
Define ('base _ path', $ _ SERVER ['document _ root']);
Define ('smarty _ path', '\ smartTest \ SMARTY \\');
Require BASE_PATH.SMARTY_PATH. 'smarty. class. php ';
/* This path of $ dir2 is displayed on the presentation page. The strings below are the same, so that the smarty cannot find the templates path */
// $ Dir2 = "../Smarty/tempplates /";
Class SmartyProject extends Smarty {

Function SmartyProject (){
/* The parent ::__ construct (); must be added. Otherwise, the Smarty will not be constructed, and the truncate cannot be used,
* This seems unreasonable, but in fact,
* This does solve the problem that truncate cannot be used */
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'; // reference the main file of adodb.
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)}; Dbq = ". $ 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),); // the first 6 Characters of the 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 operation does not return the result set, or returns an empty set.
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 the 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 the query statement
$ Record = count ($ allrs-> GetRows (); // count the total number of records
$ Pagecount = ceil ($ record/$ this-> pagesize); // calculates the total number of pages
@ $ Str. = "Total". $ contentname. "". $ record. "". $ utits. "displayed on each page". $ this-> pagesize.
"". $ Utits. "No.". $ this-> rs-> AbsolutePage (). "Page/total". $ pagecount. "Page ";
$ Str. = "";
If (! $ This-> rs-> AtFirstPage ())
$ Str. = "<a href =". $ _ SERVER ['php _ SELF ']. "? Page = 1 & parameter1 = ". $ anotherserchstr." & parameter2 = ".
$ Anotherserchstrs. "class =". $ class. "> homepage </a> ";
Else
$ Str. = "<font color = '#555555'> homepage </font> ";
$ Str. = "";
If (! $ This-> rs-> AtFirstPage ())
$ Str. = "<a href =". $ _ SERVER ['php _ SELF ']. "? Page = ". ($ this-> rs-> AbsolutePage ()-1)." & parameter1 = ". $ anotherserchstr." & parameter2 = ".
$ Anotherserchstrs. "class =". $ class. "> previous page </a> ";
Else
$ Str. = "<font color = '#555555'> previous page </font> ";
$ Str. = "";
If (! $ This-> rs-> AtLastPage ())
$ Str. = "<a href =". $ _ SERVER ['php _ SELF ']. "? Page = ". ($ this-> rs-> AbsolutePage () + 1)." & parameter1 = ". $ anotherserchstr." & parameter2 = ".
$ Anotherserchstrs. "class =". $ class. "> next page </a> ";
Else
$ Str. = "<font color = '#555555'> next page </font> ";
$ Str. = "";
If (! $ This-> rs-> AtLastPage ())
$ Str. = "<a href =". $ _ SERVER ['php _ SELF ']. "? Page = ". $ pagecount." & parameter1 = ". $ anotherserchstr." & parameter2 = ".
$ Anotherserchstrs. "class =". $ class. "> last page </a> ";
Else
$ Str. = "<font color = '#555555'> last page </font> ";
If (count ($ this-> array) = 0 | $ this-> rs = false)
Return "";
Else
Return $ str;
}
}
The code above defines several classes for database connection, database operations, paging implementation, database storage, and a subclass of the smarty class, in fact, this subclass does not implement any special functions. Name it
SmartyDAO. php or whatever name can be used. Of course, note that I have tried the require statement in it. For these paths, everyone should go to their own directories.

<? Php
Require 'smartydao. php ';
// Database instance
$ Conobj = new ConnDB ("mysql", "localhost", "root", "brave", "bank ");
$ Conn = $ conobj-> GetConnId ();
// Database operation objects
$ Admindb = new AdminDB ();
// Smarty template configuration class Object
$ Smarty = new SmartyProject ();
 
$ Seppage = new SepPage (); // instantiate the paging class

This file is the instantiation of the class in the above file. It can be named smartyDaoImpl. php or your favorite name. The function is very simple, right?
The task is to create a test case to test the paging code. Of course, this test also contains two files: one is the controller and the other is the Controller code:

<? Php
Include_once 'conn/smartyDaoImpl. php ';
$ Arr_page = $ seppage-> ShowData ("select * from localinfo", $ conn, 5, $ _ GET ['page']); // execute paging Query
If (! $ Arr_page)
$ Smarty-> assign ('page', "F ");
Else {
$ Smarty-> assign ('page', 'T ');
$ Smarty-> assign ('showpage', $ seppage-> showpage ("record", "," "," a1 ")); // assign a pagination Template
$ Smarty-> assign ('Arr _ page', $ arr_page); // copy the query record to the template.
}
$ Smarty-> display ('view/fenyeShow.html '); // specifies the template for displaying data.
Name it. It is called smarty_ADODB_fenye.php. We can see that at the beginning we used:

Include_once 'conn/smartyDaoImpl. php ';
Here, we will explain that the first two files are stored in the conn folder, and the test cases are written in the conn directory at the same level.
In the View File, do not put the view file in the wrong place. We can see that the first class we wrote has such a statement.

$ This-> template_dir = BASE_PATH.SMARTY_PATH. '/templates /';
The absolute path of the View File is specified here, because,

$ Smarty-> display ('view/fenyeShow.html '); // specifies the template for displaying data.
This statement,

$ Smarty-> display ('view/fenyeShow.html '); // specifies the template for displaying data.
Therefore, we need

BASE_PATH.SMARTY_PATH. '/templates/
Create a folder named "view'. Then, create a file named" fenyeshow.html ". Of course, you can use the name you like, but ensure the consistency specified in the program.
The content in the view template is as follows:

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
</Head>
<Body>
<Table>
{Section name = id loop = $ arr_page}
<Tr>
<Td height = "20" align = "center" bgcolor = '# ffff'> {$ arr_page [id]. Id} </td>
<Td height = "20" align = "center" bgcolor = '# ffff'> {$ arr_page [id]. temperature} </td>
<Td height = "20" align = "center" bgcolor = '# ffff'> {$ arr_page [id]. illumination} </td>
<Td height = "20" align = "center" bgcolor = '# ffff'> {$ arr_page [id]. moisture} </td>
<Td height = "20" align = "center" bgcolor = '# ffff'> {$ arr_page [id]. times | truncate: 5 :"... "} </td>
</Tr>
{/Section}
 
<Tr>
<Td height = "35" colspan = "3" align = "center" bgcolor = "# FFFFFF" >{$ showpage} </td>
</Tr>
</Table>
</Body>
</Html>

Okay. After that, you only need to locate the smarty_ADODB_fenye.php file in the browser and start the server. OK, the paging effect will be displayed.
During my work, I also encountered some problems. One problem was that I could not try the truncate statement. This problem was very strange. I checked it on the smarty official website and asked me to let the smarty subclass be instantiated, the parent class Smarty is not instantiated. Therefore, the truncate cannot be used. The solution is to call the parent class constructor In the subclass.

From 0 + 0 + 0 +... = 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.