0

I have the code below that access my table called settings in my database and then creates an array called data. Currently I have it MY_Controller but I want to move it to a library so I can access the settings from models etc. The only problem is every time I try I get an undefined index error.

public function _settings()
{
    // select all settings from database
    $query = $this->db->query('SELECT * FROM settings');

    // get result from database
    $this->data = $query->result_array();

    foreach($this->data as $setting)
    {
        // create data variable from database variables
        $this->data[$setting['name']] = $setting['value'];
    }

    // simplify access to urls
    $this->data['base_url'] = base_url();
    $this->data['site_url'] = site_url();
    $this->data['template_url'] = base_url('assets/templates/' . $this->data['template']);

    // return array of data
    return $this->data;
}

Please help...

4

1 回答 1

0

在库文件夹中创建设置类后,您的代码必须使用&get_instance()来访问codeigniter实例(类似这样)

class settings
{
  public function get_settings()
   {
     $CI = &get_instance();
     $CI->load->model('settings_model','sm');
     return $CI->sm->settings();
   }

}

我的建议是将函数的名称从_settingstosettings或类似的名称更改为用于私有函数而不是公共函数CI 1.7.X的地方.._settings

在您的控制器中,您必须使用$this->load->library('settings');来访问您的库代码..

我希望这个能帮上忙

于 2012-04-11T05:49:07.357 回答