• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Get config values from DB

[eluser]MgM Web[/eluser]
I have some config values stored in my DB and would like to use them in my config file. But when trying to connect to my DB from within the config file I get an error:

Undefined property: CI_Config::$db

Is there a way to get a config value from a database instead of hardcoded in the config file?

You can alter your configuration values with the config class.


[eluser]MgM Web[/eluser]
Well where should I place this code to make sure it is set whenever a person open the site?
Perfect place would be the config file, but that is not working.

My scenario is that I am using one codeigniter instance and host multiple sites using subdomains.


And every page are using the same layout but are show different data to the person viewing the page.

That is why I need to store the config values in a database since the config files only let me have one set of config value.

Have anyone implemented a simular structure?

You could use
in your config file to detect which application to serve. See this thread for more info:

[eluser]MgM Web[/eluser]
It is unfortunately not the same, the switching of the different subsites is not a problem I have fixed this by defining a constant in the index.php file in the subfolder of the subdomain.

subsite1.mysite.com - index.php:
define('SITE', 1);

subsite2.mysite.com - index.php:
define('SITE', 2);


So this is no problem, the problem is that I want to set different config values regarding on what site the user is visiting.

So when I access subsite1.mysite.com I would like that the value $config['language'] (just an example) should come from the database. In my database I have a table with name "Sites" and the structure might be like this:

id (site ID, subsite1 = 1)
language (the default language for the site)

So when i access subsite1.mysite.com I query the database like this:

$my_config = $this->db->query("SELECT language FROM Sites WHERE id = ".SITE);

This query would then return a value which I then would like to insert into the $config['language'] variable in config.php.

Do you get the point?

You could do something like this.

function _loadConfig($site) {
    $querydata = array($site);
    $my_config = $this->db->query('SELECT * FROM sites_config WHERE id = ?',$querydata);
    if($my_config->num_rows() > 0) {
        foreach($my_config->row() as $key=>$value) {
            $this->config->set_item($key, $value);
        return true;
    } else {
        return false;    

Then place this in your contructor


If you store all your sites configurations in a single table it will load the values of each field into the config array using the fields name as the name of the config value.

[eluser]MgM Web[/eluser]
Might be a stupid question, but which constructor do you mean?

You should put it inside of each controller you have. You could make a model or a library that holds the _loadConfig function, that way you don't have to put it into every conroller.

In each of your controller files there is going to be either a function named __contruct or one named after your controller. It should have parent::Controller(); inside of it. Thats where you want to load your configurations. Main reason is because that function is called every time the page is requested.

[eluser]Pascal Kriete[/eluser]
Expanding on what SpooF suggested.

A: Extend the base controller and call it there.
B: Autoload the library and have the constructor initialize the call

Also, remember that some config variables are used before any of your own stuff is called.

[eluser]MgM Web[/eluser]
Ok, thank you for all good suggestions.

Do you know if there would be any noticable change in performance by calling this library in every controller or shouldnt that be a problem?

It is importantant that the site is running fast, therefore I can not have any "partly good" solutions.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.