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.

Removing duplicate “Open with” applications on the Macintosh

Starting with Mountain Lion, I have noticed that I will often see the same program listed multiple times when right-clicking on a document to use the “Open With” functionality. For example, I just updated TextMate (which seems to be updated very regularly lately) and my Open With menu looked like this:

Duplicates in the Mac Open With menu

To fix this problem, you need to rebuild the Mac’s Launch Services Database. Open up your terminal and issue the following command:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

That should fix the issue. For some reason, I noticed that when I right-clicked on the same item after running the command, I still saw double. Clicking on another item “refreshed” the list (and returning to the original item worked correctly). It may have just taken that long for it to take effect. I did not have to run the command with elevated privileges.

Setting UTF-encoding with a PHP header

Sometimes, I need to write a one-off PHP script to export/import data from a database, for example. Since I am not using any kind of front-end interface, I have found that I sometimes have to specifically set the encoding on the page to avoid any extended character issues with the database. I use:

header('Content-Type: text/html; charset=utf-8');

And that seems to work just fine.

Protect a cron job from outside access

I recently set up a PHP file that generates an XML-formatted sitemap for a dynamic site and set up a cron job to update the sitemap from time-to-time. I didn’t see any reason to allow outside access to executing the script, so I looked into using .htaccess to restrict access to the script. It turns out that it is pretty easy:

Order deny,allow
Allow from
Allow from additional_ip
Deny from all

You can also add in additional IP addresses if you still want to be able to hit the script from your office or home machine. Just put more IP addresses on their own lines. It is that easy. If you wanted to protect only certain files, you could wrap your rules in something like:

<Files index.html> or use wildcards like: <Files *.php> to block all access to PHP scripts.

Tweaking the inline style of an element in Firebug

One thing that I always like about Chrome’s Web Inspector over Firebug in Firefox is that in Chrome, there is an easy way to tweak the CSS of an individual element by editing its inline style.



It is always available right at the top of the CSS editing tools in Chrome and I always found it to be a handy way to tweak an element without the distraction of all other similar items being changed. In Firebug, you just get the latest style at the top of the window.



I don’t know why I never tried this, but I finally figured out that simply right-clicking in Firebug’s style window gives you the option to “Edit Element Style…” Yes, it is that simple. Choosing that option will put an option right at the top, just like in Chrome’s Web Inspector.


Coda 2 – Searching multiple files

I have seen a few people post in the Coda Users discussion group and had a friend of mine recently mention that they did not know how to search multiple files in Coda 2. Here is how to do it. You can either use the keyboard shortcut for Find in Files, Shift-Command-F, or select the menu option under Edit > Find:

Find in Files


This will open up the Find in Files interface within the Sidebar. Here you can enter terms to search for and you can also do a replace across files. At the top of the sidebar, you can choose to search the entire site, open files or, you can choose a specific folder (there will be a little triangle to the right of the last folder used – in my example, “templates”).

Find in Files interface


When clicking the triangle, you can use the Open/Save dialog box to choose another folder and you’ll also see a history of recent folders you have searched in.

Finally, the magnifying glass under the “Replace” text field with give you some more options such as using regular expressions for search and ignoring case. It will also give you recent searches you performed.

After searching, you will be presented with a list of matching files and the number of occurrences in each file. To see the actual matches within each file, click the arrow and you will see a list of the matches. Then, click on any of the lines to open the file and jump to the match.

search results

One thing that I would *really* like to see added to Coda’s searching is the ability to filter the files being searched. It searches all files. I would like to be able to, say, just search .php file, for example. I think that is its biggest weakness. The performance of the search is actually pretty zippy and I like the interface showing the collapsed file results and being able to drill down to the specific matches.


Right aligning an image with a caption for Outlook HTML email

If you ever wanted a nostalgic trip back to the early days of web development, I recommend coding an HTML email. Nothing brings back the memories of table-based layout like creating an email. Before it was Netscape 4 that we had to worry about… now, we have the joy of working with Outlook’s Word-based HTML renderer.

I ran into a problem with an HTML email and Outlook today – right aligning an image, with caption, and flowing content around it. Normally, for an email, I would use tables and columns to separate the content. In this case, though, there was a single image and a lot of textual content which caused the layout to be a bit unbalanced. So, it was decided to wrap the content around the photo. This wouldn’t be a problem if just using an image – align right and inline CSS styles work fine with Outlook. However, I needed to have a photo caption underneath the photo.

Outlook cannot float a div (as far as I know) so I could not use a div with a width and float or align it right. I tried a paragraph tag – no dice. I came to the conclusion that I would just need to add the caption to the image in Photoshop. A co-worker suggested trying to right-align a table. I created a table with a set width with a single column that just contained the image and the caption below it. I then put inline CSS on the table to float it to the right and… it worked!

Finding the perfect text editor

I have kind of a reputation for not being able to decide on a text editor. Name it and, if it is on the Mac, I have either tried it or perhaps even own it. The text editors that I own for web development are Sublime Text 2, Coda 2, BBedit 10.5, TextMate, DreamWeaver and PHPStorm. I have tried quite a few others: NetBeans, Chocolat, SKEdit, Aptana, Eclipse, Zend Studio and surely others that I am not remembering right now. I want to go through the editors that I own and give some pros and cons, as I see them, for what I do – mainly PHP web development. I will try to go through each in the near future so stay tuned.

TextMate line number glitch

A rather annoying bug that I keep running into with the TextMate 2 alpha is that the highlighted line number in the line number column often doesn’t represent the actual line I am on. Here is an example:



It seems pretty re-create-able and, to get it to go away, I can simply scroll with the mouse wheel. Fairly obnoxious, though, especially when trying to use the code folding tools in the line number column.