Making global config data available from database in Codeigniter

It’s been a while since I wrote my last article, mostly because I’m working hard on this private project and have to spend all my time on it. This article discusses a way of setting global configuration options from database, available through the whole website.

I’d still like to share with you this interesting bit of coding. I’m using an auto-loaded model to set some static variables which have been pulled from the database. This database has a table with one row of data which holds all config data.

Have a look at the Settings_model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings_model extends CI_Model {

    public static $db_config;

    public function __construct() {
        parent::__construct();
        $this->_load_settings();
    }

    /**
     *
     * _load_settings: load the settings from database
     *
     *
     */

    protected function _load_settings() {
        $this->db->select('login_enabled, register_enabled, members_per_page, admin_email, home_page, default_theme,
        login_attempts, email_protocol, sendmail_path, smtp_host, smtp_port, smtp_user, smtp_pass, site_title');
        $this->db->from('settings');
        $this->db->limit(1);
        $query = $this->db->get();

        if($query->num_rows() == 1) {
            $row = $query->row();
            self::$db_config['login_enabled'] = $row->login_enabled;
            self::$db_config['registration_enabled'] = $row->register_enabled;
            self::$db_config['members_per_page'] = $row->members_per_page;
            self::$db_config['admin_email_address'] = $row->admin_email;
            self::$db_config['home_page'] = $row->home_page;
            self::$db_config['default_theme'] = $row->default_theme;
            self::$db_config['login_attempts'] = $row->login_attempts;
            self::$db_config['email_protocol'] = $row->email_protocol;
            self::$db_config['sendmail_path'] = $row->sendmail_path;
            self::$db_config['smtp_host'] = $row->smtp_host;
            self::$db_config['smtp_port'] = $row->smtp_port;
            self::$db_config['smtp_user'] = $row->smtp_user;
            self::$db_config['smtp_pass'] = $row->smtp_pass;
            self::$db_config['site_title'] = $row->site_title;
        }
    }

}

/* End of file settings_model.php */
/* Location: ./application/models/settings_model.php */

As you can see I’m using a static array called $db_config and to make sure it is available I’m also autoloading it in config/autoload.php.

$autoload['model'] = array('system/settings_model');

Now if you need to access a variable all you need to do is:

print "<div class=\"input_div\">Site title &raquo; ".
form_input(array('name' => 'site_title', 'id' => 'site_title', 'class' => 'input_text', 'value' => Settings_model::$db_config['site_title'])) ."</div>\r\n";

This should be straight-forward enough but do comment in case of any questions or remarks.

This entry was posted in CodeIgniter, PHP and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>