简体   繁体   中英

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.

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

You could create a new model that pulls the config options from the database and then autoload this model so that it's available globally. Then, you could access your options via $this->model_name->function_name($db_column); and have $db_column be the name of the column for the option you're looking for and have your model function select and return the column data.

Personally, I use a custom config file:
http://codeigniter.com/user_guide/libraries/config.html

This is something you shouldn't be doing. The database call is done every time a request is done, and you can't store the database credentials in the database itself.

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