Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Get config values from DB
#1

[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?
#2

[eluser]SpooF[/eluser]
You can alter your configuration values with the config class.

http://ellislab.com/codeigniter/user-gui...onfig.html
#3

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

subsite1.mysite.com
subsite2.mysite.com
...

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?
#4

[eluser]flojon[/eluser]
You could use
Code:
$_SERVER['SERVER_NAME']
in your config file to detect which application to serve. See this thread for more info:
http://ellislab.com/forums/viewthread/81424/
#5

[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:
Code:
define('SITE', 1);

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

etc...

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:

Code:
$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?
#6

[eluser]SpooF[/eluser]
You could do something like this.

Code:
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

Code:
$this->_loadConfig(SITE);

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

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

[eluser]SpooF[/eluser]
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.
#9

[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.
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB