Sitewide settings in CodeIgniter

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:


-- ----------------------------
-- Table structure for `siteSettings`
-- ----------------------------
DROP TABLE IF EXISTS `siteSettings`;
CREATE TABLE `siteSettings` (
`settingKey` varchar(50) NOT NULL,
`settingValue` text,
`recordLastModDate` datetime DEFAULT NULL,
PRIMARY KEY (`settingID`)
delimiter ;;
CREATE TRIGGER `siteSettingsUpdateLastModDate` BEFORE UPDATE ON `siteSettings` FOR EACH ROW BEGIN
delimiter ;

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.

Simple AppleScript to do lookup in CodeIgniter documentation

Recently, in Coda, I wanted to create a simple way to look up a word directly in the CodeIgniter documentation. While the reference books are nice, there are multiple PHP documentation sources one might want to look up while working. For example, one might want to look up a native PHP function or, if working in Drupal or WordPress, look up something at those particular websites. So, using example code from others, I wrote up a quick AppleScript that allows one to look up the selected word in the CodeIgniter docs (which is really just a Google site search on the domain). Without further ado, here it is:

tell application "Coda 2"
tell selected split of selected tab of front window
if selected text is not equal to "" then
set someText to selected text
end if
set myQuery to do shell script "php -r 'echo trim(urlencode(" & "\"" & someText & "" & "\"));'"
end tell
end try
end tell
tell application "System Events"
if myQuery is not equal to "" then
open location "" & myQuery & "&sa=Go"
end if
on error
end try
end tell

Very simple stuff! Basically, it looks in the active split and grabs the selected text if any is available. From there it uses PHP to encode the text for a URL (to deal with any problem special characters) and then, if there is resulting text, opens the location:[selected_text]

Again, nothing special. I have no doubt that some nice bells and whistles could be added to this script. One thing that isn’t immediately obvious to me is how to select a word that the cursor is in using AppleScript.

AppleScripts in the Coda AppleScript menu can be put into folders so I can have a Documentation folder and then have as many sources as I need nested in a logical way.

Easy way to pass multiple variables to a CodeIgniter validation callback

I was recently working on a custom form validation callback for my CodeIgniter script. By default, one gets the field that you’re validating available in the callback function. For example, if I have the following form validation rule:

 $this->form_validation->set_rules('discountGlobal', 'Global Discount', 'trim|integer|max_length[1]|xss_clean|prep_for_form|only_one_global_discount[]');

My custom callback is only_one_global_discount. I am not explicitly passing anything through to it. However, in the function I have in my MY_Form_validation.php file, I can access the $discountGlobal value:

public function only_one_global_discount($discountGlobal) {
$CI =& get_instance();
echo $discountGlobal;

You can also pass a second value, though. You would change your callback call to:

$this->form_validation->set_rules('discountGlobal', 'Global Discount', 'trim|integer|max_length[1]|xss_clean|prep_for_form|only_one_global_discount['.$test.']');

Where $test is the second value you want available to you in your function. The function call would then change to:

public function only_one_global_discount($discountGlobal, $test) {

And the value of $test would be available to you in the function. But what can you do if you need a third, fourth or even more values? My solution is to package up the variables you need in an array. Just passing through an array just gives me “Array” when using a print_r on it in my callback function. So, I used json_encode on the array before passing it through and then json_decode in my callback function. Viola! I have access to as many variables as I need.