Article Source: www.litou.net
Recently, I received a project, I was scored, and the design database table was about 45. So many tables, it is indeed a headache. How to do the least work but can achieve the greatest effect? I have been analyzed to write data auxiliary classes to assist ADODB to do work. First, determine your directory structure, the directory structure is as follows:
| -admin // Backstage | -adodb // adoDB file directory | -smarty // smarty file directory | -Images // picture and style file 珓 | -dataclass // data operation class folder | -class_test.php // Test Class | -configs // System Configuration Folder | -config.inc.php // System Profile | -cache // Buffer Directory | -Templates // Template File | -Templates_C // Template Resolution Folder | -Test.htm // Test Template file include.inc.php // system contains file set smarty_adodb.inc.php // smarty adoDB class declaration file Test.php // Test file
Do your best, let's start working! First, define your config.inc.php configuration file:
php $ _db [host] = 'localhost'; # database ip $ _db [user] = 'root'; # username $ _DB [pass] = 'root'; # database password $ _db [name] = 'yop '; # Database name $ _DB [type] =' mysql '; # type?>
Smarty_adodb.inc.php
$ db = & adonewconnection ($ _ db [type]); $ db -> connection ($ _ 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
PHP INCLUDE_ONCE ('./ Configs / Config.inc.php'); # loading data link to configure include_once ('./ AdoDb / AdoDb.inc.php'); # 装 a a 数据 数据 类 cd_once ('./ Smarty / Smarty.class.php '); # loading smarty template classes include_once (' ./ Smarty_adodb.inc.php '); # loading smarty and adoDB class call set file include_once (' ./ Dataclass / class_test.php '); # 加 装Hobby data class?>
Then we start writing the data operation class, the author's database structure 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
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); $ r = array (); while ($ Data = $ result-> fetchrow ()) {Array_Push ($ RS, $ DATA);} Return $ RS;} Function Settest ($ ID = ', $ pairs, $ Work =') {Global $ db; if (Empty) {$ SQL = "INSERT INTO TEST"; $ SQL. = "(". Join (Array_Keys), ","). ")"; $ SQL. = "VALUES"; $ SQL. = "(" ". Join (Array_VALUES", "" ""). "") ";} else {ix ($ Work == 'Updat E ') {$ SQL = "$ WORK TEST"; Array_walk ($ pairs, create_function (' & $ value, & $ name ", '$ value = $ name." = "". $ value. "" ";' )); $ Sql. = "Set". Join (Array_Values), ","); $ sql. = "Where id = $ ID";} elseif ($ Work == 'delete') {$ SQL = "$ work from test where id = '$ ID'";
}}} $ Results = $ db-> execute ($ SQL); RETURN $ Result;}}?> The above class is the most critical. This place can understand that all other are saying. Ok, let's start instances:
Test.php
php incrude_once ('./ include.inc.php'); $ test = new test (); $ r = $ test-> listtest (); foreach ($ r) {$ list [] = $ Array; $ TPL-> Assign ("List", $ list); $ TPL-> Display ("test.htm"); $ info = array ("name" => "curved head", "addtime" = > Date ("ymd")); $ test-> settest ('5', $ info, 'update');?>
Then we write an HTM out
Test.htm
<{section name = sec loop = $ list}> <{$ list [sec] .name}>
<{/ section}>
Note: The actual class database name is not as good as, only even change. If the operation is abnormal, please correct it to see it. How, did you come out? The above class file implements data intelligent operation, what is wrong, please discuss with me. Q: 20438713 http://www.openphp.cn/index.php?module=article&id=40