/项目目录/common/common/function.php里就可以使用了。
function cd($key, $value = null){
/**
* 公共方法,可以把配置参数保存到数据库
*
* 用法:
* cd('配置项'); //读取配置项
* cd('配置项', '数据') //写入配置项
* cd('配置项', 'del') //从数据库中移除该配置项
*
*
*在数据库中建立config表,表前缀改成自己的
* create table if not exists `think_config` (
* `id` int(11) not null auto_increment,
* `key` varchar(255) character set utf8 not null,
* `value` varchar(255) character set utf8 not null,
* primary key (`id`)
* ) engine=myisam default charset=utf8 auto_increment=1 ;
*/
$cd = m('config');
$where = array( 'key' => $key);
//如果有$value为空,就返回$key的值
if($value == null){
return $cd->where( $where )->getfield('value');
}
//如果$value为del,就删除这条记录的值
else if($value == del)
{
return $cd->where( $where )->delete();
}
//如果$value有正常值,其它操作
else{
//如果存在$key,更新
if($cd->where($where)->find()){
$cd->value = $value;
return $cd->where($where)->save();
}
//如果不存在$key,写入一条新数据
else{
$cd->key = $key;
$cd->value = $value;
return $cd->add();
}
}
}
ad:真正免费,域名+虚机+企业邮箱=0元