<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Scott Nelle.com</title>
	
	<link>http://www.scottnelle.com</link>
	<description>Scott NellÃ©'s Personal Site</description>
	<lastBuildDate>Thu, 24 Sep 2009 00:25:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/scottnelle" type="application/rss+xml" /><feedburner:browserFriendly></feedburner:browserFriendly><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Load A Page Into A WordPress Theme Outside Of The Loop</title>
		<link>http://www.scottnelle.com/138/load-a-page-into-a-wordpress-theme/</link>
		<comments>http://www.scottnelle.com/138/load-a-page-into-a-wordpress-theme/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 16:06:56 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=138</guid>
		<description><![CDATA[I build a fair number of WordPress sites on small budgets for my employer, Union Street Media. Sometimes I need to give our clients an editable region in the sidebar, away from the main blog/page content. Here&#8217;s a quick and dirty trick I use to pull a page into a theme outside of the loop:
 [...]]]></description>
			<content:encoded><![CDATA[<p>I build a fair number of WordPress sites on small budgets for my employer, Union Street Media. Sometimes I need to give our clients an editable region in the sidebar, away from the main blog/page content. Here&#8217;s a quick and dirty trick I use to pull a page into a theme outside of the loop:</p>
<pre><code> &lt;?php
// must use a variable for page id
// http://codex.wordpress.org/Function_Reference/get_page
$id = 3;
$p = get_page($id);
echo $p-&gt;post_content;
?&gt;</code></pre>
<p>You can get your page ID by editing the page. It will show up in the url (the &#8216;post=x&#8217; portion.) It&#8217;s important to note that you have to pass a variable to the <code>get_page()</code> function. If you just pass an integer it will throw a fatal error. No need to go into why that is; just keep it in mind. <code>get_page()</code> essentially wraps <code>get_post()</code> so <a href="http://codex.wordpress.org/Function_Reference/get_post">that function&#8217;s documentation</a> is a good place to start if you want to learn what&#8217;s available to you.</p>
<p>This isn&#8217;t a particularly pretty solution, but it&#8217;s quick and it works well provided you know your page IDs and you&#8217;re not making a theme for distribution. I like to name my page something like &#8216;**Sidebar Content&#8217; so it is easy to differentiate from regular pages.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/138/load-a-page-into-a-wordpress-theme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress 301 Redirect Plugin Released</title>
		<link>http://www.scottnelle.com/129/wordpress-301-redirect-plugin-released/</link>
		<comments>http://www.scottnelle.com/129/wordpress-301-redirect-plugin-released/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 00:10:10 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=129</guid>
		<description><![CDATA[I&#8217;m very pleased to announce that I&#8217;ve released my first WordPress plugin. It&#8217;s called Simple 301 Redirects and it does just what it says on the tin. It provides an interface for redirecting URL requests. It&#8217;s handy for when you&#8217;ve migrated a site to WordPress and are unable to maintain the URL structure. With Simple [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m very pleased to announce that I&#8217;ve released my first WordPress plugin. It&#8217;s called Simple 301 Redirects and it does just what it says on the tin. It provides an interface for redirecting URL requests. It&#8217;s handy for when you&#8217;ve migrated a site to WordPress and are unable to maintain the URL structure. With Simple 301 Redirects you can redirect your old urls like &#8220;/about.html&#8221; to new, clean URLs like &#8220;http://www.yoursite.com/pages/about/&#8221; or whatever you like. Redirecting your old links to new destinations is important for preserving inbound links and pagerank after migrating a site. And this plugin does it in the easiest possible way I could think of.</p>
<p>You can read a little bit more about the plugin at <a title="Simple 301 Redirects WordPress Plugin" href="/simple-301-redirects-plugin-for-wordpress/?PHPSESSID=01a2757d8d44c8ceb0bda63e34911fc0">its official home on this site </a>or go straight to the <a title="Download Simple 301 Redirects" href="http://wordpress.org/extend/plugins/simple-301-redirects/">WordPress plugin directory to download it</a>. I hope you find it helpful!</p>
<p><img class="alignnone size-full wp-image-116" title="Simple 301 Redirects Plugin Screenshot" src="http://www.scottnelle.com/wp-content/uploads/2009/08/301-plugin-screenshot.jpg" alt="Simple 301 Redirects Plugin Screenshot" width="550" height="298" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/129/wordpress-301-redirect-plugin-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Browser Use in 2009</title>
		<link>http://www.scottnelle.com/93/browser-use-in-2009/</link>
		<comments>http://www.scottnelle.com/93/browser-use-in-2009/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 23:53:50 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=93</guid>
		<description><![CDATA[Douglas Bowman at StopDesign recently shared how his site visitors break down in terms of browser use. As a hero of the web standards movement (sort of like our Hercules or Michael Jordan,) Douglas admits that his stats are skewed toward tech-savvy folks. I decided it would be interesting to look into stats from a [...]]]></description>
			<content:encoded><![CDATA[<p>Douglas Bowman at StopDesign recently <a href="http://stopdesign.com/archive/2009/02/06/trading-places.html">shared how his site visitors break down in terms of browser use</a>. As a hero of the web standards movement (sort of like our Hercules or Michael Jordan,) Douglas admits that his stats are skewed toward tech-savvy folks. I decided it would be interesting to look into stats from a more general audience. I develop a lot of real estate websites at work, so that seemed like a natural place to look. I surveyed several of our more heavily-trafficked sites and I&#8217;ve made the following observations (which I have also posted at Douglas&#8217; site:)</p>
<ul>
<li>Internet Explorer users account for between 65% and 85% of all visits</li>
<li>Firefox and Safari account for almost all the rest of the traffic, and Firefox tends to have 2-3 times the share of Safari.</li>
<li>70% of IE users are browsing these sites with IE7. There was very little variation here, even though the percentage of IE users in general varied by 20% across sites.</li>
</ul>
<p>(Observations are based on the last month of data, across a sampling of sites based around the United States. Visitors were primarily from the US.)</p>
<p>It&#8217;s unfortunate to see how dominant Internet Explorer is sector, but progress is evident. It&#8217;s a relief to see that IE7&#8211;a fairly capable browser in its own right&#8211;has seen increased adoption recently.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/93/browser-use-in-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leaving My Own Club</title>
		<link>http://www.scottnelle.com/91/leaving-my-own-club/</link>
		<comments>http://www.scottnelle.com/91/leaving-my-own-club/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 00:14:12 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=91</guid>
		<description><![CDATA[I&#8217;ve decided I&#8217;m leaving the 140 Club. 2 months and about 70 tweets later, I&#8217;ve had my fun and indulged my obsessive nature for long enough.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided I&#8217;m leaving <a href="/49/140-characters/?PHPSESSID=01a2757d8d44c8ceb0bda63e34911fc0">the 140 Club</a>. 2 months and about 70 tweets later, I&#8217;ve had my fun and indulged my obsessive nature for long enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/91/leaving-my-own-club/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Micro-Micro-Niche Advertising</title>
		<link>http://www.scottnelle.com/72/micro-micro-niche-advertising/</link>
		<comments>http://www.scottnelle.com/72/micro-micro-niche-advertising/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 16:45:46 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=72</guid>
		<description><![CDATA[I was reading a local news story and in the sidebar I noticed what appeared to be an ad banner for Barack Obama in the sidebar. It turns out it was actually a link to the news site&#8217;s section of Obama-related stories. What struck me about the banner was the wording: &#8220;President-Elect Barack Obama Click [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-73" title="obama click here" src="http://www.scottnelle.com/wp-content/uploads/2008/12/obama-ad.jpg" alt="" width="180" height="375" />I was reading <a href="http://www.wptz.com/cnn-news/18388059/detail.html">a local news story</a> and in the sidebar I noticed what appeared to be an ad banner for Barack Obama in the sidebar. It turns out it was actually a link to the news site&#8217;s section of Obama-related stories. What struck me about the banner was the wording: &#8220;President-Elect Barack Obama Click Here.&#8221; Apparently they are targeting the extremely small niche of web users that are President-Elect Barack Obama, and imploring them to click on a mysterious banner (and for those counting along at home, that&#8217;s a niche of one.)</p>
<p>Just a little mid-week levity. The moral of the story: think carefully about your calls to action. You need to make it clear to your readers what you want them to do <strong>and</strong> why they should want to do it. Give them a reason to click your banner or sign-up button. And if something isn&#8217;t an advertisement, make sure that it doesn&#8217;t look like one. This banner looks like a paid ad for who knows what and I never would have clicked on it if I weren&#8217;t planning to write this post poking fun at it. It doesn&#8217;t represent a compelling call to action for me. At face value, I can&#8217;t tell what I&#8217;ll find once I click, or even if I&#8217;m supposed to. After all, I&#8217;m not President-Elect Barack Obama. That&#8217;s some other guy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/72/micro-micro-niche-advertising/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog Post Title SEO–The Only Tip You Need</title>
		<link>http://www.scottnelle.com/58/blog-post-title-seo/</link>
		<comments>http://www.scottnelle.com/58/blog-post-title-seo/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 01:29:27 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[SEO]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=58</guid>
		<description><![CDATA[Quick tip: Search engines are designed to work for people, not the other way around. They&#8217;re designed to pick results that are likely to be what the searcher is looking for, and for a blog the post title is an important part of that. If you want people to find your blog post in a [...]]]></description>
			<content:encoded><![CDATA[<p>Quick tip: Search engines are designed to work for people, not the other way around. They&#8217;re designed to pick results that are likely to be what the searcher is looking for, and for a blog the post title is an important part of that. If you want people to find your blog post in a search engine and click through, do the following:</p>
<ol>
<li>Think about what you would type into Google if you were looking for whatever information you&#8217;re about to post. Make it specific.</li>
<li>Put that in the title. You can add some more detail to make the title more &#8220;punchy,&#8221; but make sure your search phrase is in there.</li>
<li>There is no step 3.</li>
</ol>
<p>If a searcher sees exactly what she typed in the title of a search result she&#8217;s going to click. The trick is that your content has to support the title you picked. Otherwise, even if you trick the search engine, your site visitor will take one look and hit the back button.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/58/blog-post-title-seo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>140 Characters</title>
		<link>http://www.scottnelle.com/49/140-characters/</link>
		<comments>http://www.scottnelle.com/49/140-characters/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 19:00:53 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=49</guid>
		<description><![CDATA[I recently launched a just-for-fun project called The 140 Club. As the site mentions, &#8220;The 140 Club is a group of Twitter users loosely committed to writing tweets of no more and no less than 140 characters.&#8221; I&#8217;d like to say that it&#8217;s an artistic experiment in creating something in a constrained environment, but it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I recently launched a just-for-fun project called <a href="http://www.the140club.com/">The 140 Club</a>. As the site mentions, &#8220;The 140 Club is a group of <a href="http://www.twitter.com/">Twitter</a> users loosely committed to writing tweets of no more and no less than 140 characters.&#8221; I&#8217;d like to say that it&#8217;s an artistic experiment in creating something in a constrained environment, but it&#8217;s really just for fun. After finding that I had a knack for writing updates that were close to 140 characters (the maximum on twitter,) I decided I would start rewording my updates to be exactly 140 characters. I announced my plan with the following <a href="http://twitter.com/scottnelle/status/1039710091">tweet</a>:</p>
<blockquote><p>Also my new thing is writing updates of exactly 140 characters. The one before last only made it because I put an extra word in by mistake. </p></blockquote>
<p>Shortly thereafter, Sean Bossie, a client of the company I work for and all around cool guy <a href="http://twitter.com/sbossie/status/1040625995">replied</a>:</p>
<blockquote><p>@scottnelle That cracks me up Scott&#8230;I am now joining The 140 Club. So the thing is: Can you end with incomplete words or must be dead on??</p></blockquote>
<p>Sensing an opportunity to get a chuckle out of Sean, I bought a domain, cracked open Photoshop, and got to work over my lunch break. An hour later I had designed, built, and launched <a href="http://www.the140club.com/">the140club.com</a>. Talk about agile. :-) The 140 Club is currently accepting new members, so why not join up?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/49/140-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>24 Ways 2008</title>
		<link>http://www.scottnelle.com/47/24-ways-2008/</link>
		<comments>http://www.scottnelle.com/47/24-ways-2008/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 01:33:22 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=47</guid>
		<description><![CDATA[Drew McLellan has posted the first article in this year&#8217;s 24 Ways To Impress You Friends, the advent calendar for web geeks.  The site will have a new article about web design or development each day from now until December 24. Over the past few years it&#8217;s become a great holiday season tradition, and [...]]]></description>
			<content:encoded><![CDATA[<p>Drew McLellan has posted the first article in this year&#8217;s <a href="http://24ways.org/">24 Ways To Impress You Friends</a>, the advent calendar for web geeks.  The site will have a new article about web design or development each day from now until December 24. Over the past few years it&#8217;s become a great holiday season tradition, and the only one that doesn&#8217;t seem to start earlier every year.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/47/24-ways-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Updating Unique Field Validation for CI 1.7.0</title>
		<link>http://www.scottnelle.com/44/updating-unique-field-validation-for-ci-170/</link>
		<comments>http://www.scottnelle.com/44/updating-unique-field-validation-for-ci-170/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 02:56:34 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=44</guid>
		<description><![CDATA[Just a quick post to keep my CodeIgniter unique field validation extension up to date. When EllisLab released CodeIgniter 1.7.0, they included a new form validation class which is meant to replace the old validation class.  The old class still exists in the code for now but it&#8217;s been deprecated, so developers are urged [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick post to keep my CodeIgniter unique field validation extension up to date. When EllisLab <a href="http://codeigniter.com/blog/codeigniter_170_released/">released CodeIgniter 1.7.0</a>, they included a new form validation class which is meant to replace the old validation class.  The old class still exists in the code for now but it&#8217;s been deprecated, so developers are urged to use the new form validation class instead.  Only very minor updates are needed to my validation extension in order to make it work with the new code.  These were suggested to me by commenter <a href="http://programadoranonimo.blogspot.com/">libre</a> before I even had a chance to look at implementing them myself.  (Thanks libre!) For everyone&#8217;s benefit, here&#8217;s the updated version of the library (and remember, use this if you&#8217;re running CodeIgniter 1.7.0 or later:)</p>
<pre><code>
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Form_validation Class
 *
 * Extends Form_Validation library
 *
 * Adds one validation rule, "unique" and accepts a
 * parameter, the name of the table and column that
 * you are checking, specified in the forum table.column
 *
 * Note that this update should be used with the
 * form_validation library introduced in CI 1.7.0
 */
class MY_Form_validation extends CI_Form_validation {

	function My_Form_validation()
	{
	    parent::CI_Form_validation();
	}

	// --------------------------------------------------------------------

	/**
	 * Unique
	 *
	 * @access	public
	 * @param	string
	 * @param	field
	 * @return	bool
	 */
	function unique($str, $field)
	{
		$CI =&#038; get_instance();
		list($table, $column) = split("\.", $field, 2);

		$CI->form_validation->set_message('unique', 'The %s that you requested is unavailable.');

		$query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");
		$row = $query->row();
		return ($row->dupe > 0) ? FALSE : TRUE;
	}
}
?&gt;
</code></pre>
<p>You&#8217;ll want to save this file as system/application/libraries/MY_Form_validation.php (instead of MY_Validation.php.)</p>
<p>When you load the library, don&#8217;t forget that it&#8217;s now called form_validation instead of just validation. There have also been some changes to the way you set your rules for the new form validation class.  You can read all about this in the <a href="http://codeigniter.com/user_guide/libraries/form_validation.html">class documentation</a>.  Here&#8217;s the basic format, including usage of the &#8220;unique&#8221; rule from the extension:</p>
<pre><code>
$this->form_validation->set_rules('username','User Name','required|min_length[5]|unique[users.username]');
$this->form_validation->set_rules('emailaddress','Email Address','required|valid_email|unique[users.email]');
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/44/updating-unique-field-validation-for-ci-170/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Extending CodeIgniter’s Validation Library To Check For Unique Values</title>
		<link>http://www.scottnelle.com/41/extending-codeigniters-validation-library/</link>
		<comments>http://www.scottnelle.com/41/extending-codeigniters-validation-library/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 02:01:38 +0000</pubDate>
		<dc:creator>Scott Nelle</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.scottnelle.com/?p=41</guid>
		<description><![CDATA[Updated November 4, 2008: This extension has been updated slightly to work with CI&#8217;s new form validation class, added with version 1.7.0.  If you&#8217;re using an older version of CI you should still use the extension on this page.  If you&#8217;re using 1.7.0 or later, you should check out the updated extension.
I&#8217;m working [...]]]></description>
			<content:encoded><![CDATA[<p class="update"><strong>Updated November 4, 2008:</strong> This extension has been updated slightly to work with CI&#8217;s new form validation class, added with version 1.7.0.  If you&#8217;re using an older version of CI you should still use the extension on this page.  If you&#8217;re using 1.7.0 or later, you should <a href="/44/updating-unique-field-validation-for-ci-170/?PHPSESSID=01a2757d8d44c8ceb0bda63e34911fc0">check out the updated extension</a>.</p>
<p>I&#8217;m working on small web application and I&#8217;m building it with CodeIgniter.  Like most web applications, mine requires user registration and login.  And like most login systems, mine is happiest if each user has a unique username.  While the CodeIgniter validation library is pretty robust, it doesn&#8217;t come with a function for checking a value to see if it is unique or if it already exists in the database.  Fortunately there are a couple of ways to remedy that.</p>
<p>The first way is outlined in the <a href="http://codeigniter.com/user_guide/libraries/validation.html">documentation for the validation library</a> under the heading &#8220;Callbacks: Your own Validation Functions.&#8221;  I tried this and it works well, but it requires you to write the functions in your controller or model.  I thought that looked messy and I wanted something a little more streamlined, so I decided to extend the validation library with my own function.  Extending CodeIgniter&#8217;s libraries is pretty easy.  Here&#8217;s what I came up with&#8211;I&#8217;ll explain the important lines afterward:</p>
<p class="update"><strong>Updated September 12, 2008:</strong> Made some changes based on suggestions from the super-helpful CodeIgniter community.  Thanks guys!</p>
<pre><code>
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Validation Class
 *
 * Extends Validation library
 *
 * Adds one validation rule, "unique" and accepts a
 * parameter, the name of the table and column that
 * you are checking, specified in the forum table.column
 */
class MY_Validation extends CI_Validation {

	function My_Validation()
	{
	    parent::CI_Validation();
	}

	// --------------------------------------------------------------------

	/**
	 * Unique
	 *
	 * @access    public
	 * @param    string
	 * @param    field
	 * @return    bool
	 */
	function unique($str, $field)
	{
		$CI =&#038; get_instance();
		list($table, $column) = split("\.", $field, 2);

		$CI->validation->set_message('unique', 'The %s that you requested is unavailable.');

		$query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");
		$row = $query->row();
		return ($row->dupe > 0) ? FALSE : TRUE;
	}
}
?&gt;
</code></pre>
<p>Save the above code as system/application/libraries/MY_Validation.php and you&#8217;re ready to go. You can now add the &#8220;unique&#8221; rule to your existing validation, like so:</p>
<pre><code>
$rules['username'] = "required|min_length[5]|unique[users.username]";
$rules['emailaddress'] = "required|valid_email|unique[users.email]";
</code></pre>
<p>This will check the values that entered for <code>username</code> and <code>emailaddress</code> to make sure they don&#8217;t already exist in the <code>username</code> and <code>email</code> fields in your table called <code>users</code>.  The general format for the rule, then, is : <code>unique[table.column]</code>.  This matches the SQL syntax for specifying fields when field names are ambiguous.  Pretty cool.</p>
<p>So here&#8217;s what&#8217;s going on in each of the important lines.</p>
<pre><code>
$CI =&#038; get_instance();
</code></pre>
<p>This creates a reference to the CodeIgniter super object and allows you to use it&#8217;s functions in your library.  <a href="http://www.codeignitor.com/user_guide/general/creating_libraries.html">See the user guide for an explanation</a> (under the heading &#8220;Utilizing CodeIgniter Resources within Your Library.&#8221;)</p>
<pre><code>
list($table, $column) = split("\.", $field, 2);
</code></pre>
<p>This line parses out the table name and column name.  Note that it only returns 2 strings.  This way you can specify something like <code>unique[users.email.address]</code>, which would check the users table for a column called email.address.  In mysql periods are allowed in field names but not table names, and the validation rule reflects that.</p>
<pre><code>
$CI->validation->set_message('unique', 'The %s that you requested is unavailable.');
</code></pre>
<p>This line sets the error message in case the value isn&#8217;t unique. <code>%s</code> will be replaced by the field description you enter in your validation fields.  This would be better off in a language file, but for ease of use I&#8217;ve stuck it right in the library.  CodeIgniter allows you to replace entire language files but not simply extend them with a single line.  If that changes in the future I&#8217;ll revisit my decision.</p>
<pre><code>
$query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");
$row = $query->row();
</code></pre>
<p>Here&#8217;s where it checks the database to make sure your field is unique.  In the query I assigned the row count to the keyword &#8220;dupe&#8221; (for duplicate.)  I wanted to use &#8220;exists&#8221; or &#8220;match&#8221; but both are protected words in mysql.  Oh well. Note the use of <code>$CI->db->query</code> instead of <code>$this->db->query</code>.  That&#8217;s explained in the documentation link earlier in this post.</p>
<pre><code>
return ($row->dupe > 0) ? FALSE : TRUE;
</code></pre>
<p>Finally, this line returns the result of the validation check.  In case you&#8217;re not accustomed to using ternary operators, here it is as an if/else:</p>
<pre><code>
if ($row->dupe > 0)
{
	return FALSE; // a match was found, validation fails
}
else
{
	return TRUE; // value is unique, validation passed
}
</code></pre>
<p>So there you have it.  A quick extension to CodeIgniter&#8217;s validation library that allows you to check your database for unique values.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scottnelle.com/41/extending-codeigniters-validation-library/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
