Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

config data stored in database

I might be barking up completely the wrong tree so forgive me if this isn't at all how I should be doing things.

I've a config table in my database which is to hold config details for my website. I want to load this data into the config.php file for code igniter?

Is this something I should be doing or totally wrong?

If I put a database call at the top of the config.php file will this get called every time someone loads the site or just the first time?

Sorry if this sounds totally stupid I'm a bit confused.

Edit

I'm not meaning database details, for example want to store something like the amount of post to be shown per page. The script I'm creating is to be used on more than one server the amount of post per page for example needs to be editable. I could of cause load this into a session but I thought loading it as a constant*? in a config file would be a better idea.

like image 966
flyersun Avatar asked Apr 14 '11 09:04

flyersun


1 Answers

Here is how I do this:

First, our model:

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

.

class Prefs extends CI_Model
{
    function __construct()
    {
        parent::__construct();      
        $pre = array();
        $CI = &get_instance();

        if ($this->config->item("useDatabaseConfig")) {
            $pr = $this->db->get("settings")->result();     
            foreach($pr as $p)
            {
                $pre[addslashes($p->key)] = addslashes($p->value);
            }       
        }
        else
        {
            $pre = (object) $CI->config->config;
        }   
        $CI->pref = (object) $pre;      
    } 
}
  • Auto-load this model.
  • in your config/config.php, add this line (or another custom one, if you use one): $config["useDatabaseConfig"] = true;

In your database, you need a "settings" table with "key" and "value" columns.

Thats all. With this model, you can set whenever you want to use database and whenever go on with config/*.php files.

Say, you can change $config["useDatabaseConfig"] variable right before you call this model method (then, autoload should be disabled.)

I do attain the variable to the CI instance, just because it is easier and nice.
Read the config data like this: $this->pref->sess_cookie_name

like image 189
Oytun Avatar answered Sep 25 '22 13:09

Oytun