I recently wanted to set up a system to allow a client to edit sitewide configuration settings on a custom CodeIgniter site. I didn’t want to hardcode it in a configuration file and I wasn’t sure exactly how it would need to be added to in the future. I decided to use a system much like WordPress with a custom key/value table in the database – like WordPress’s wp_options table. So, I created a similar table – a primary, auto-incremented key, a settingKey varchar field, a settingValue text field and a modified date field with a trigger (just to be able to monitor changes on the setting). My MySQL for the table looks like this:
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `siteSettings`
-- ----------------------------
DROP TABLE IF EXISTS `siteSettings`;
CREATE TABLE `siteSettings` (
`settingID` int(4) NOT NULL AUTO_INCREMENT,
`settingKey` varchar(50) NOT NULL,
`settingValue` text,
`recordLastModDate` datetime DEFAULT NULL,
PRIMARY KEY (`settingID`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
delimiter ;;
CREATE TRIGGER `siteSettingsUpdateLastModDate` BEFORE UPDATE ON `siteSettings` FOR EACH ROW BEGIN
SET NEW.recordLastModDate = CURRENT_TIMESTAMP;
END;
Once the table was in place, I looked into ways to always have these configuration settings available to me through the CodeIgniter site. I saw some cool stuff online using hooks and I think that would have worked but it seemed a little intense for the simple site I was working on. So, I decided instead to create a model, settings_model.php, and then add the model to the autoload.php file so that it would always trigger:
$autoload['model'] = array('settings_model');
In the model itself, I had a function, LoadSitewideSettings that would simply get the settings from the database table and add them to CodeIgniter’s configuration settings. Here is my LoadSitewideSettings function:
public function LoadSitewideSettings() {
$settings = array();
// Get the settings from the database
$query = $this->db->get('siteSettings');
foreach ($query->result() as $settingRow) {
$settings[$settingRow->settingKey] = $settingRow->settingValue;
}
$this->config->set_item('siteSettings', $settings);
}
So, I just grab all the data from that database table, run through each item to get the keys, and then use CodeIgniter’s config->set_item function to assign the values to CodeIgniter’s configuration variables. I create an array and save all of those elements in the variable, ‘siteSettings,’ to try to avoid any naming collisions with other CodeIgniter variable names.
Finally, I call my LoadSitewideSettings function in the constructor of my model to make sure I grab the configuration values. When I want to access these configuration variables, I use CodeIgniter’s config->item command like this:
$this->config->item('defaultOrderEmails', 'siteSettings');
So, the record in my configuration file with the key, defaultOrderEmails, will be grabbed and made available to me anywhere on the site.
Thanks for Awesome Topic
Thanks alot i have implemented this successfully. and can continue with my sitewide Settings