国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 網(wǎng)絡(luò)通信 > 正文

adodb+smarty+myClass結(jié)合數(shù)據(jù)類的智能操作

2019-11-04 12:29:18
字體:
供稿:網(wǎng)友

  最近接到一項目,精略計算了一下內(nèi)容,設(shè)計數(shù)據(jù)庫表為45個左右。這么多表,的確夠頭疼的。怎么做到最少操作但能達到最大的效果呢?
  本人經(jīng)過分析,決定自己寫數(shù)據(jù)輔助類來協(xié)助ADODB來完成工作。
  首先,確定你的目錄結(jié)構(gòu),本人目錄結(jié)構(gòu)如下:
  
  -admin //后臺
  -adodb //adodb文件目錄
  -smarty //smarty文件目錄
  -images //圖片及樣式文件?
  -dataclass //數(shù)據(jù)操作類文件夾
  -class_test.php //測試類
  -configs //系統(tǒng)配置文件夾
  -config.inc.php //系統(tǒng)配置文件
  -cache //緩沖目錄
  -templates //模板文件
  -templates_c //模板解析文件夾
  -test.htm //測試模板文件
  include.inc.php //系統(tǒng)包含文件集
  smarty_adodb.inc.php //smarty adodb類聲明文件
  test.php //測試文件
  
  做好以上工作,讓我們開始工作吧!首先,定義你的 config.inc.php 配置文件:
  
  <?php
  $_DB[host] = 'localhost'; #數(shù)據(jù)庫ip
  $_DB[user] = 'root'; #用戶名
  $_DB[pass] = 'root'; #數(shù)據(jù)庫密碼
  $_DB[name] = 'yop'; #數(shù)據(jù)庫名
  $_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'); #加載數(shù)據(jù)鏈接配置
  include_once('./adodb/adodb.inc.php'); #加載adodb數(shù)據(jù)類
  include_once('./smarty/Smarty.class.php'); #加載smarty模板類
  include_once('./smarty_adodb.inc.php'); #加載smarty及adodb類調(diào)用集合文件
  include_once('./dataclass/class_test.php'); #加載HOBBY數(shù)據(jù)類
  ?>
  
  接著我們開始寫數(shù)據(jù)操作類,筆者的數(shù)據(jù)庫結(jié)構(gòu)如下:
  
  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}>
  
  注:實際類名數(shù)據(jù)庫名并不如上,只偶有改變。假如操作異常,請自行改正


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 太原市| 星座| 黎平县| 泸州市| 新宁县| 淮南市| 长春市| 五河县| 万宁市| 龙江县| 攀枝花市| 龙江县| 宜都市| 通山县| 都匀市| 平远县| 宁乡县| 宜丰县| 苏尼特右旗| 太仆寺旗| 崇礼县| 班戈县| 仁怀市| 牡丹江市| 融水| 康乐县| 拉孜县| 武夷山市| 阿拉善右旗| 封开县| 富民县| 颍上县| 天长市| 丰城市| 辽宁省| 大荔县| 新乡县| 五原县| 天峨县| 张家界市| 临西县|