CodeIgniter - Database Based Config


/ Published in: PHP
Save to your folder(s)

Credit: 'Jelmer' from CodeIgniter Forums

1. Create preferences.php in your application/config/ directory.

2. Add this to the controller that writes your config values to the database so that it will also write them to the preferences.php config file.

3. Add 'preferences' to the config array of your application/config/autoload.php file.

4. Access the values using $this->config->item(); like you would with any other config value.

5. Celebrate with a beer.


Copy this code and paste it in your HTML
  1. function _export_preferences() {
  2. $this->load->helper('file');
  3.  
  4. $preferences = $this->db->get('preferences');
  5. $preferences = $preferences->result();
  6.  
  7. $preferences_file = '<?php'."\n\n";
  8. foreach ($preferences as $row) {
  9. $preferences_file .= '$config[\''.addslashes($row->name).'\'] = \''.addslashes($row->value).'\';'."\n";
  10. }
  11.  
  12. write_file(APPPATH.'config/preferences.php', $preferences_file);
  13. }

URL: http://codeigniter.com/forums/viewthread/103684/#674817

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.