CodeIgniter Forums
Loading items from database for custom config file? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Loading items from database for custom config file? (/showthread.php?tid=61078)



Loading items from database for custom config file? - El Forum - 09-12-2014

[eluser]Skoobi[/eluser]
Hi I've created a config file for my site items like the site name site email and so on but i want to try and store them in the database and have the config file load them from there.

At the moment i have my config file my_config.php :

Code:
$config['site_name']  = 'My CMS';
$config['site_email']  = '';
$config['display_members'] = true;
$config['display_registration'] = true;

but what i want to do is pull it from the database so it comes as an array.

Code:
$config['site_name']  = $settings['site_name'];
$config['site_email']  = $settings['site_email'];
$config['display_members'] = $settings['login_link'];
$config['display_registration'] = $settings['register_link'];


Is there a way i can do this like i do in the controllers???

Cheers
Chris


Loading items from database for custom config file? - El Forum - 09-13-2014

[eluser]InsiteFX[/eluser]
If you use the CI Active Record then you can return it as an array.

See the Users Guide on the Database Active Record and Query Results.




Loading items from database for custom config file? - El Forum - 09-13-2014

[eluser]Skoobi[/eluser]
I have tried but i get this error


Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: MX_Config::$load

Filename: config/my_config.php

Line Number: 3


Fatal error: Call to a member function database() on a non-object in /application/config/my_config.php on line 3

Heres the code I've tried

Code:
$this->load->database();

$query = $this->db->get('settings');
return $query->row_array();

$settings = $query;


// Site Prefs (Soon to be pulled fropm the database)
$config['site_name']  = $settings['site_name'];

Im pretty sure I've done it wrong!!!


Cheers
Chris


Loading items from database for custom config file? - El Forum - 09-13-2014

[eluser]InsiteFX[/eluser]
You need to use the CI Super OBject to access anything in a custom library etc;

Code:
$CI = get_instance();

$CI->db->get('settings');

// or get it global
protected $CI;

// in your __construct() get the CI instance then access
$this->CI->db->get('settings');



Loading items from database for custom config file? - El Forum - 09-14-2014

[eluser]Skoobi[/eluser]
Brilliant many thanks for your help that worked a charm!

Cheers
Chris