gpt4 book ai didi

php - CodeIgniter 全局设置数据库

转载 作者:可可西里 更新时间:2023-11-01 09:01:39 26 4
gpt4 key购买 nike

我正在尝试对每个 Controller 和 View 实现全局设置。

数据库表名为 config 包含

config_name | config_value
title | My Website
email | my@example.com

在我的模型中我有查询:

function config()
{
$query = $this->db
->select('*')
->get('config');
return $query->result();
}

controller中我这样调用模型数据

$data['cfg'] = $this->config_model->config();

现在我想通过使用

View 中显示某些配置位
<title><?php echo $cfg->title; ?></title>

但是我得到一个错误Trying to get property of non-object

我在模型 查询中尝试了结果查询选项:

result();
result_array();

但它不会起作用。我做错了什么?

最佳答案

在你的模型中试试这个:

public function config() {
$q = $this->db->get('config')
return $q->result_array();

这在你的 Controller 中:

$this->load->model("config_model");
$res = $this->config_model->get();
if($res){
$data['result'] = $res;
$this->load->view('Your view here', $data);
} else {
echo "failed";
}

然后你可以在你的 View 中这样访问:

$result[0]['title'];

假设您的配置表只有一行。

关于php - CodeIgniter 全局设置数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com