简体   繁体   中英

How do I put this code into a library for codeigniter?

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

after creating your settings class in the library folder your code must use the &get_instance() to reach the codeigniter instance ( something like this )

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

}

and my advice is to change the name of your function from _settings to settings or something like that because in CI 1.7.X the _settings where used for private functions not public one..

in your controller you will have to use the $this->load->library('settings'); to get access to your library code..

i hope this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM