歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> adodb+smarty+myClass 結合數據類的智能操作

adodb+smarty+myClass 結合數據類的智能操作

日期:2017/2/27 9:30:00   编辑:更多Linux
  最近接到一項目,精略計算了一下內容,設計數據庫表為45個左右。這麼多表,的確夠頭疼的。怎麼做到最少操作但能達到最大的效果呢?  本人經過分析,決定自己寫數據輔助類來協助ADODB來完成工作。  首先,確定你的目錄結構,本人目錄結構如下:    -admin //後台  -adodb //adodb文件目錄  -smarty //smarty文件目錄  -images //圖片及樣式文件珓  -dataclass //數據操作類文件夾  -class_test.PHP //測試類  -configs //系統配置文件夾  -config.inc.php //系統配置文件  -cache //緩沖目錄  -templates //模板文件  -templates_c //模板解析文件夾  -test.htm //測試模板文件  include.inc.php //系統包含文件集  smarty_adodb.inc.php //smarty adodb類聲明文件  test.php //測試文件    做好以上工作,讓我們開始工作吧!首先,定義你的 config.inc.php 配置文件:    <?php  $_DB[host] = 'localhost'; #數據庫IP  $_DB[user] = 'root'; #用戶名  $_DB[pass] = 'root'; #數據庫密碼  $_DB[name] = 'yop'; #數據庫名  $_DB[type] = 'mysql'; #類型  ?>    smarty_adodb.inc.php    <?  $db = &ADONewConnection($_DB[type]);  $db ->Connect($_DB[host],$_DB[user],$_DB[pass],$_DB[name]); #adodb鏈接  $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'); #加載數據鏈接配置  include_once('./adodb/adodb.inc.php'); #加載adodb數據類  include_once('./smarty/Smarty.class.php'); #加載smarty模板類  include_once('./smarty_adodb.inc.php'); #加載smarty及adodb類調用集合文件  include_once('./dataclass/class_test.php'); #加載HOBBY數據類  ?>    接著我們開始寫數據操作類,筆者的數據庫結構如下:    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);  $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;  }  }  ?>    上面這個類是最關鍵的。這個地方能明白,其它的都好說了。好,下面我們開始實例:    test.php    <?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"=>"無喱頭","addtime"=>date("Y-m-d"));  $test->setTest('5',$info,'update');  ?>    接著我們寫個HTM出來    test.htm    <{section name=sec loop=$list}>  <{$list[sec].name}>  <BR>  <{/section}>    注:實際類名數據庫名並不如上,只偶有改變。如果操作異常,請自行改正




Copyright © Linux教程網 All Rights Reserved