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

来源:互联网 发布:linux中sed 编辑:程序博客网 时间:2024/05/17 07:18

|-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}>