Home » php

adodb+smarty+myClass 结合-数据类的智能操作

10 02月 2010 Views: No Comment Tags:

本文转载于http://blog.csdn.net/phphot,作者利用一个函数实现了对数据库的插入、更新与删除,赞!
最近接到一项目,精略计算了一下内容,设计数据库表为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

<?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数据类 
?>

接着我们开始写数据操作类,笔者的数据库结构如下:

<?php 
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

<?php 
<{section name=sec loop=$list}> 
<{$list[sec].name}> 
 
 
<{/section}>

注:实际类名数据库名并不如上,只偶有改变。如果操作异常,请自行改正

运行一下看看。
怎么样,出来了吗效果。

原创文章如转载,请注明:转载自Xixis Blog [ http://www.xixis.net/ ]
本文链接地址:http://www.xixis.net/archives/adodb-smarty-myclass-binding-the-data-type-intelligent-operation.html

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.