Adodb + smarty + myClass combined with smart data operations
Source: Internet
Author: User
Article Title: Intelligent Operations of adodb + smarty + myClass combined with data classes. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
I recently received a project with a slightly computed content. about 45 database tables are designed. It is indeed a headache for so many tables. How can we achieve the minimum operation but the maximum effect?
After analysis, I decided to write the data auxiliary class to assist ADODB to complete the work.
First, determine the structure of your Directory. The structure of your Directory is as follows:
|-Admin // background
|-Adodb // Directory of the adodb file
|-Smarty // smarty file directory
|-Images // image and style file?
|-Dataclass // data operation folder
|-Class_test.php // Test class
|-Configs // system configuration folder
|-Config. inc. php // system configuration file
|-Cache // buffer Directory
|-Templates // template file
|-Templates_c // template parsing folder
| -Test.htm // test template file
Include. inc. php // The system contains a file set.
Smarty_adodb.inc.php // smarty adodb class declaration file
Test. php // test file
Let's get started! First, define your config. inc. php configuration file:
$ _ DB [host] = 'localhost'; # Database IP address
$ _ DB [user] = 'root'; # user name
$ _ DB [pass] = 'root'; # database password
$ _ DB [name] = 'emp'; # Database name
$ _ DB [type] = 'mysql'; # type
?>
Smarty_adodb.inc.php
$ Db = & ADONewConnection ($ _ DB [type]);
$ Db-> Connect ($ _ DB [host], $ _ DB [user], $ _ DB [pass], $ _ DB [name]); # adodb link
$ Tpl = new Smarty;
$ Tpl-> template_dir = "./templates ";
$ Tpl-> compile_dir = "./templates/templates_c ";
$ Tpl-> left_delimiter = '<{';
$ Tpl-> right_delimiter = '}> ';
?>
Include. inc. php
Include_once ('./configs/config. inc. php'); # load data link configuration
Include_once ('./adodb. inc. php'); # load the adodb data Class
Include_once ('./smarty/Smarty. class. php'); # load the smarty template class
Include_once ('./smarty_adodb.inc.php'); # load the collection file called by the smarty and adodb classes.
Include_once ('./dataclass/class_test.php'); # load the holobby data Class
?>
Then we start writing data operations. The structure of the database is as follows:
Create table 'test '(
'Id' int (10) unsigned not null auto_increment,
'Name' varchar (20) not null default '',
'Addtime' varchar (20) not null default '',
KEY 'id' ('id ')
)
Class_test.php
Class Test {
Function getTest_ByID ($ id ){
Global $ db;
If (empty ($ id )){
Return false;
}
$ SQL = "SELECT * FROM 'test' where ID = '$ ID '";
$ Result = $ db-> Execute ($ SQL );
$ Data = $ result-> FetchRow ();
Return $ data;
}
Function listTest ($ order = 'id '){
Global $ db;
If (empty ($ order )){
$ Order = 'id ';
}
$ SQL = "SELECT * FROM 'test' order by $ order desc ";
$ Result = $ db-> Execute ($ SQL );
$ Rs = array ();
While ($ data = $ result-> FetchRow ()){
Array_push ($ rs, $ data );
}
Return $ rs;
}
Function setTest ($ id = '', $ pairs, $ work = ''){
Global $ db;
If (empty ($ id )){
$ SQL = "insert into Test ";
$ SQL. = "(". join (array_keys ($ pairs ),",").")";
$ SQL. = "values ";
$ SQL. = "(" ". join (array_values ($ pairs ),"","")."")";
} Else {
If ($ work = 'update '){
$ SQL = "$ work Test ";
Array_walk ($ pairs, create_function ('& $ value, & $ name',' $ value = $ name. "=" ". $ value .""";'));
$ SQL. = "set". join (array_values ($ pairs ),",");
$ SQL. = "where id = $ id ";
} Elseif ($ work = 'delete '){
$ SQL = "$ work from Test where ID = '$ ID '";
}
}
$ Result = $ db-> Execute ($ SQL );
Return $ result;
}
}
?>
The above class is the most critical. I can see from this point that everything else is easy to say. Okay. let's start the instance:
Test. php
Include_once ('./include. inc. php ');
$ Test = new Test ();
$ Rs = $ test-> listTest ();
Foreach ($ rs as $ array ){
$ List [] = $ array;
$ Tpl-> assign ("list", $ list );
}
$ Tpl-> display ("test.htm ");
$ Info = array ("name" => "No response header", "addtime" => date ("Y-m-d "));
$ Test-> setTest ('5', $ info, 'update ');
?>
Next, let's write an HTM.
Test.htm
<{Section name = sec loop = $ list}>
<{$ List [sec]. name}>
<{/Section}>
Note: the actual database name of the class name is not the same as above, but only occasionally changed. If the operation is abnormal, correct it by yourself.
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.