<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Scott Blaine &#187; development</title>
	<atom:link href="http://scottblaine.com/category/development/feed" rel="self" type="application/rss+xml" />
	<link>http://scottblaine.com</link>
	<description>Omaha web developer</description>
	<lastBuildDate>Wed, 12 Oct 2011 01:00:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>The HTML and CSS Behind Google&#8217;s Search Bar</title>
		<link>http://scottblaine.com/the-html-and-css-behind-googles-search-bar</link>
		<comments>http://scottblaine.com/the-html-and-css-behind-googles-search-bar#comments</comments>
		<pubDate>Wed, 28 Sep 2011 01:00:15 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[bar]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=479</guid>
		<description><![CDATA[Earlier this year I was working on a project that had a text entry box similar to Google&#8217;s. It was a long bar in which you entered a line of text with a button at the end to &#8220;Add&#8221; it (instead of &#8220;Search&#8221;). I really liked Google&#8217;s styling for their search bar and figured I [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this year I was working on a project that had a text entry box similar to Google&#8217;s. It was a long bar in which you entered a line of text with a button at the end to &#8220;Add&#8221; it (instead of &#8220;Search&#8221;). I really liked Google&#8217;s styling for their search bar and figured I would simply &#8220;lift&#8221; the HTML and CSS for it. However Google&#8217;s HTML source code, if you&#8217;ve ever looked at it, is heavily minimized. Everything is on one line with short, obscure CSS class names like &#8220;gac_v2.&#8221; Great for serving up webpages quickly, not so fun for trying to figure out how they&#8217;re styling their pages. I tried to find a simple &#8220;how to&#8221; for styling something in a similar way, but alas, there was none to be found.</p>
<p>Not to be deterred, I dug into their code, copied the portion I was interested in, removed all of the useless cruft, gave the CSS classes slightly more useful names, and came up with a working version that looked like this:</p>
<p><a href="http://scottblaine.com/blog/wp-content/uploads/2011/04/example-search-bar.png"><img class="alignnone size-full wp-image-481" title="example-search-bar" src="http://scottblaine.com/blog/wp-content/uploads/2011/04/example-search-bar.png" alt="" width="396" height="54" /></a></p>
<p>That should look rather familiar to you.</p>
<p>Seeing as I couldn&#8217;t find anything on the Intertubes for this (and I&#8217;m probably not the only one) I thought it might be helpful to share the HTML and CSS code for this &#8212; there&#8217;s also one image which is used as a gradient background for the search button (entirely optional; it just makes it look nice). If you find this useful leave a comment and let me know.</p>
<p>Right click and Save as:</p>
<ul>
<li><a href="http://scottblaine.com/blog/wp-content/uploads/2011/04/google-search-bar-example.html">HTML with embedded CSS</a></li>
<li><a href="http://scottblaine.com/blog/wp-content/uploads/2011/04/btn-bg.png">Image file (background image for button)</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/the-html-and-css-behind-googles-search-bar/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I won the Zappos developer contest</title>
		<link>http://scottblaine.com/i-won-the-zappos-developer-contest</link>
		<comments>http://scottblaine.com/i-won-the-zappos-developer-contest#comments</comments>
		<pubDate>Tue, 03 May 2011 02:37:05 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[luck]]></category>
		<category><![CDATA[winning]]></category>
		<category><![CDATA[zappos]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=507</guid>
		<description><![CDATA[Recently I had the good fortune of winning $1000 and being featured in the local news. Here&#8217;s how it happened. In the middle of March I was doing some research for a blog post and I came across a contest that Zappos was running. I had recently decided that I was spending too much time [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had the good fortune of winning $1000 and being featured in the local news. Here&#8217;s how it happened.</p>
<p>In the middle of March I was doing some research for a blog post and I came across <a title="First Zappos Developer Contest" href="http://developer.zappos.com/blog/first-zappos-developer-contest">a contest that Zappos was running</a>. I had recently decided that I was spending too much time building web apps for $0/hour, so I almost didn&#8217;t enter, but I had an idea that I thought I could pull off without investing too much time. My intent was to build something that was fairly simple but very polished.</p>
<p>I spent a couple weeks (about 40 hours total) throwing together <a title="Outfit Her" href="http://outfither.antifantastic.com/">an app</a> in my spare time. I thought my app was good, but I&#8217;ve entered contests like this before where the entrants pour lots of time into it and produce something really stellar. So it was much to my surprise that I ended up <a title="Zappos Developer Contest Winners" href="http://developer.zappos.com/blog/first-developer-contest-winners">winning the top prize</a> in the contest (a cool $500 cash plus a $500 gift certificate to Zappos). That alone was enough to make building the app worth my while, but what happened next was even better.</p>
<p>Shortly after I won I posted a quick <a title="My &quot;I won the Zappos contest&quot; tweet" href="http://twitter.com/#!/blaisco/status/59639026063392768">tweet</a> about it. I have some folks from the Silicon Prairie News as followers and my tweet led to me getting an interview request. I wrote up some answers (reviewed by my wife, thanks!) and <a title="Omaha Developer Wins Zappos API Challenge" href="http://www.siliconprairienews.com/2011/04/omaha-developer-wins-zappos-api-challenge">they posted an article about it</a> the following day. That article was then mentioned in the weekly <a title="PrairieCast with Ben Milne and Drew Larson" href="http://www.siliconprairienews.com/2011/04/prairiecast-with-ben-milne-and-drew-larson-video">PrairieCast postcast</a> (starting at 21:00). I was also contacted by several people who read the article about projects that they wanted my help on.</p>
<p>When I decided to enter the contest in never crossed my mind that it would lead to me being featured on SPN and being offered work. That goes to show though, it is definitely possible to <a title="How to Get a Real Education" href="http://online.wsj.com/article/SB10001424052748704101604576247143383496656.html?mod=WeekendHeader_Right">attract</a> <a title="How To Make Your Own Luck" href="http://www.fastcompany.com/magazine/72/realitycheck.html">luck</a>. You don&#8217;t know how that luck will manifest itself, but it will. Do cool stuff, be social, and luck will find you.</p>
<blockquote><p>Regarding &#8220;Attracting Luck&#8221;: it sounds like a value-free bromide, but it really does work. I&#8217;m not a social butterfly by nature, but I force myself to go out and network. It blows my mind how effective that is, because there exists someone with the ability to give you what you want just by saying &#8220;Yeah, sure&#8221; and that is 1000000000x more likely to happen after you have asked him for it in person versus happening spontaneously. (Ditto hearing, e.g., &#8220;You do SEO consulting? What a coincidence, we need that. Send us a proposal.&#8221;, &#8220;You made what? That&#8217;s interesting. I have someone you should get a coffee with.&#8221;, etc)<br />
-<a title="MicroISV on a Shoestring" href="http://www.kalzumeus.com/">Patrick McKenzie </a></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/i-won-the-zappos-developer-contest/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unable to drag images with jQuery on IE? I found a fix.</title>
		<link>http://scottblaine.com/unable-to-drag-images-with-jquery-on-ie-i-found-a-fix</link>
		<comments>http://scottblaine.com/unable-to-drag-images-with-jquery-on-ie-i-found-a-fix#comments</comments>
		<pubDate>Sat, 02 Apr 2011 00:37:36 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[draggable]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=468</guid>
		<description><![CDATA[I&#8217;ve been working on an app where you drag div&#8217;s from one location to another (using jQuery for draggable/droppable). Inside of said div is some text and an image. I was running into a big problem with IE (Internet Explorer): when clicking on the image to try and drag the div it would not work. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on an app where you drag div&#8217;s from one location to another (using jQuery for draggable/droppable). Inside of said div is some text and an image. I was running into a big problem with IE (Internet Explorer): when clicking on the image to try and drag the div it would not work. It might as well have not been draggable at all. And dragging using the text in the div caused IE to start trying to select text (but at least it would drag). Not optimal behavior.</p>
<p>It took me a long time to find an answer on Google, but here it is:</p>
<p><strong>Don&#8217;t use the &#8220;distance&#8221; and &#8220;delay&#8221; options for draggables; they are currently incompatible with IE.</strong> As soon as I removed them the draggable functionality worked perfectly. I&#8217;m hopeful that the jQuery team will fix this in future versions, but for now, just don&#8217;t use those options.</p>
<p>This was with jQuery 1.5.1, jQuery UI 1.8.11 and IE8. Hopefully I&#8217;ve loaded this post with enough search terms that others will find it quickly before beating their heads against the wall :)</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/unable-to-drag-images-with-jquery-on-ie-i-found-a-fix/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Installing MongoDB on Windows 7</title>
		<link>http://scottblaine.com/installing-mongodb-on-windows-7</link>
		<comments>http://scottblaine.com/installing-mongodb-on-windows-7#comments</comments>
		<pubDate>Sat, 27 Mar 2010 15:03:12 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[windows 7]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=386</guid>
		<description><![CDATA[I was trying to install MongoDB on Windows 7 recently and was having some trouble getting it to work. It doesn&#8217;t help that the installer fails silently, not telling you why it won&#8217;t install. All you know is that you ran the install command and there&#8217;s no MongoDB service installed. Anyway, here are the two [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to install MongoDB on Windows 7 recently and was having some trouble getting it to work. It doesn&#8217;t help that the installer fails silently, not telling you why it won&#8217;t install. All you know is that you ran the install command and there&#8217;s no MongoDB service installed. Anyway, here are the two gotcha&#8217;s I ran into:</p>
<ol>
<li>You need to run the command prompt as administrator.</li>
<li>You need to have the full path to the exe when you run the install command, e.g.:<br />
c:\mongodb-1.4.0\bin\mongod.exe &#8211;install</li>
</ol>
<p>Runs like a champ after that.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/installing-mongodb-on-windows-7/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Image-free tabs with clean CSS</title>
		<link>http://scottblaine.com/image-free-tabs-with-clean-css</link>
		<comments>http://scottblaine.com/image-free-tabs-with-clean-css#comments</comments>
		<pubDate>Sun, 30 Aug 2009 01:00:17 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[tabs]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=370</guid>
		<description><![CDATA[It&#8217;s slightly amazing to me how hard it is to find someone that has image-free tabs that have clean CSS. I found a lot of articles on sliding-door tabs (requiring images) and a few articles that required CSS for each individual tab (yuck!). And what I wanted wasn&#8217;t even complex: Simple, right? I sure wasn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s slightly amazing to me how hard it is to find someone that has image-free tabs that have clean CSS. I found a lot of articles on sliding-door tabs (requiring images) and a few articles that required CSS for each individual tab (yuck!). And what I wanted wasn&#8217;t even complex:</p>
<p><a href="http://scottblaine.com/blog/wp-content/uploads/2009/08/tabs.png"><img class="alignnone size-full wp-image-371" title="tabs" src="http://scottblaine.com/blog/wp-content/uploads/2009/08/tabs.png" alt="tabs" width="260" height="52" /></a></p>
<p>Simple, right? I sure wasn&#8217;t going to go reinvent the wheel by writing my own CSS for them though. Thankfully after searching for a bit I came across <a title="CSS Mini Tabs (the UN-tab, tab)" href="http://www.simplebits.com/bits/minitabs.html">SimpleBits&#8217; Mini Tabs</a>. I didn&#8217;t want mini-tabs, but the HTML and CSS was remarkably clean. Only 4 CSS declarations on a simple HTML list with a class=&#8221;active&#8221; for the current tab. With some modification to the CSS I was able to come up with tabs that looked like the above (even looks good in IE7, amazingly).</p>
<p>So, if you want some decent looking/functioning tabs go yank the HTML/CSS from the above link.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/image-free-tabs-with-clean-css/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use additional one-to-one models with restful_authentication</title>
		<link>http://scottblaine.com/how-to-use-additional-one-to-one-models-with-restful_authentication</link>
		<comments>http://scottblaine.com/how-to-use-additional-one-to-one-models-with-restful_authentication#comments</comments>
		<pubDate>Sun, 09 Aug 2009 01:00:03 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[restful_authentication]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=345</guid>
		<description><![CDATA[Restful_authentication collects some basic information to get an account set up, like a username, email address, and password, which all gets stored in a User model. What if you have some additional information that you&#8217;d like to collect upon registration, and you don&#8217;t want to store it in the User model (addresses, for example)? Rails [...]]]></description>
			<content:encoded><![CDATA[<p>Restful_authentication collects some basic information to get an account set up, like a username, email address, and password, which all gets stored in a User model. What if you have some additional information that you&#8217;d like to collect upon registration, and you don&#8217;t want to store it in the User model (addresses, for example)? Rails 2.3 makes this easy.</p>
<p>Let&#8217;s get an example registration app set up:</p>
<pre>
rails registration
ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication
ruby script/generate authenticated user sessions
</pre>
<p>Cool, now you have your registration app set up with a Users model, controller, and view. If you start up your server and head to http://localhost:3000/users/new/ you should see a very basic registration form. Let&#8217;s add our Address model. For the sake of brevity, I&#8217;m just going to create a field that references the User model and one field for a zip code, but you get the idea.</p>
<pre>
ruby script/generate model Address user:references zipcode:string
rake db:migrate
</pre>
<p>With our Address model in place, we need to let the User model know that it is linked to the Address model. Here&#8217;s what the User model looks like (\app\models\user.rb):</p>
<pre>
require 'digest/sha1'
class User &lt; ActiveRecord::Base
  # Virtual attribute for the unencrypted password
  attr_accessor :password

  validates_presence_of     :login, :email
  # lots more stuff that we're not going to worry about...
</pre>
<p>We&#8217;re going to add three things:</p>
<pre>
require 'digest/sha1'
class User &lt; ActiveRecord::Base
  has_one :address
  accepts_nested_attributes_for :address
  attr_accessible :address_attributes

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  validates_presence_of     :login, :email
  # lots more stuff that we're not going to worry about...
</pre>
<p>We&#8217;ve told the User model that (1) it has one Address model, (2) that it should save data for Address automagically, and (3) that the address_attributes fields are permitted fields to receive data for (if you read through the code a bit further you&#8217;ll see another attr_accessible line, you could add address_attributes there too).</p>
<p>We need to make one change to the User controller (\controllers\users_controller.rb) to prevent an &#8220;Called id for nil&#8221; error later on. At line 6 you should have an empty new method. We&#8217;re going to create an instance variable for User:</p>
<pre>
  # render new.rhtml
  def new
    @user = User.new
  end
</pre>
<p>Let&#8217;s now take a look at the one User view that restful_auth created (\app\views\users\new.html.erb):</p>
<pre>
&lt;%= error_messages_for :user %&gt;
&lt;% form_for :user, :url =&gt; users_path do |f| -%&gt;
&lt;p&gt;&lt;label for=&quot;login&quot;&gt;Login&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :login %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :email %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password_confirmation&quot;&gt;Confirm Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password_confirmation %&gt;&lt;/p&gt;

&lt;p&gt;&lt;%= submit_tag &#039;Sign up&#039; %&gt;&lt;/p&gt;
&lt;% end -%&gt;
</pre>
<p>First of all, we need to change the form_for :user to be form_for @user. Second, we get to add in our zipcode field:</p>
<pre>
&lt;%= error_messages_for :user %&gt;
&lt;% form_for @user, :url =&gt; users_path do |f| -%&gt;
&lt;p&gt;&lt;label for=&quot;login&quot;&gt;Login&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :login %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :email %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password_confirmation&quot;&gt;Confirm Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password_confirmation %&gt;&lt;/p&gt;

&lt;% @user.build_address unless @user.address %&gt;
&lt;% f.fields_for :address do |a| %&gt;
  &lt;p&gt;
    &lt;%= a.label :zipcode %&gt;
    &lt;%= a.text_field :zipcode %&gt;
  &lt;/p&gt;
&lt;% end %&gt;

&lt;p&gt;&lt;%= submit_tag &#039;Sign up&#039; %&gt;&lt;/p&gt;
&lt;% end -%&gt;
</pre>
<p>Here&#8217;s where I ran into the two gotcha&#8217;s. If you don&#8217;t switch form_for :user to form_for @user when you submit the form you&#8217;ll get a very unpleasant error that looks like this:</p>
<pre>
ActiveRecord::AssociationTypeMismatch in UsersController#create
Address(#46729050) expected, got HashWithIndifferentAccess(#23561230)
</pre>
<p>Second, if you don&#8217;t add in line 15, @user.build_address unless @user.address, you&#8217;ll get the following error when you try to view the form:</p>
<pre>You have a nil object when you didn't expect it!</pre>
<p><a href="http://www.pixellatedvisions.com/2009/03/18/rails-2-3-nested-model-forms-and-nil-new-record">More info on why that error occurs here</a>.</p>
<p>Anyway, with the above steps you should now have a registration form that creates records for both User and Address. If you found this helpful, leave a comment and let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/how-to-use-additional-one-to-one-models-with-restful_authentication/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sorting records by distance using PHP &amp; MySQL</title>
		<link>http://scottblaine.com/sorting-records-by-distance-using-php-mysql</link>
		<comments>http://scottblaine.com/sorting-records-by-distance-using-php-mysql#comments</comments>
		<pubDate>Thu, 09 Jul 2009 01:00:00 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[logic]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=276</guid>
		<description><![CDATA[I recently got to the point in my project where I was ready to implement sorting for the search results.&#160; I wanted to sort by the distance between two zip codes. However, I quickly realized I had a major problem on my hands. I had a (potentially) large number of search results that I needed [...]]]></description>
			<content:encoded><![CDATA[<p>I recently got to the point in my project where I was ready to implement sorting for the search results.&nbsp; I wanted to sort by the distance between two zip codes. However, I quickly realized I had a major problem on my hands. I had a (potentially) large number of search results that I needed to sort. Too many to sort using PHP; I needed a way to sort them in MySQL. But the method I&#8217;d planned to use to calculate the distance between zip codes, <a title="PHP Zip Code Range and Distance Calculation" href="http://www.micahcarrick.com/04-19-2005/php-zip-code-range-and-distance-calculation.html">Micah Carrick&#8217;s distance calculation class</a>, was implemented entirely in PHP. I needed some way for the distance to end up in the SQL query so that I could sort on it.</p>
<p>After searching around for a bit I came across <a title="A program that finds zip codes by distance from a particular zip code using the Haversine Formula" href="http://www.phpbuilder.com/snippet/download.php?type=snippet&amp;id=1363">this nice bit of code</a> which creates a MySQL query using PHP variables to calculate the distance. By combining the two methods I was able to create a very workable solution for sorting by distance. Here&#8217;s what I did:</p>
<p>First, download the zipcode SQL from <a title="PHP Zip Code Range and Distance Calculation" href="http://www.micahcarrick.com/04-19-2005/php-zip-code-range-and-distance-calculation.html">Micah&#8217;s page</a> and run each SQL file. That should give you a new shiny table called zip_code filled to the brim with (what else?) zip codes.</p>
<p>Second, you&#8217;ll need the get_zip_point function from Micah&#8217;s code:</p>
<pre>
/**
 * This function pulls just the latitude and longitude from the
 * database for a given zip code.
 */
function get_zip_point($zip)
{
	$sql = "SELECT lat, lon from zip_code WHERE zip_code='$zip'";
	$r = mysql_query($sql);
	if (!$r)
	{
		$this-&gt;last_error = mysql_error();
		return FALSE;
	}
	else
	{
		$row = mysql_fetch_array($r);
		mysql_free_result($r);
		return $row;
	}
}
</pre>
<p>Third, you&#8217;ll want a function to generate the SQL you&#8217;ll use in your query:</p>
<pre>
/**
 * Returns the SQL to calculate distance based on latitude and longitude
 */
function get_distance_SQL($lat, $lon)
{
	$sql = "
	3957 * 2 *
	atan2(
		sqrt(
			pow((sin(0.0174*(lat-$lat)/2)),2) +
			cos(0.0174*$lat) * cos(0.0174*lat) *
			pow((sin(0.0174*(lon-$lon)/2)),2)
		)
		,
		sqrt(1-
			(
				pow((sin(0.0174*(lat-$lat)/2)),2) +
				cos(0.0174*$lat) * cos(0.0174*lat) *
				pow((sin(0.0174*(lon-$lon)/2)),2)
			)
		)
	)
	as distance";

	return $sql;
}
</pre>
<p>Fourth, you wrap it all together with a few lines of PHP. I&#8217;m using CodeIgniter to handle the SQL call, so your mileage may vary a bit on the exact implementation details.</p>
<pre>
list($lat, $lon) = $this-&gt;get_zip_point($user_zip_code);

$this-&gt;db-&gt;select("users.username, zip_code.city, zip_code.state_prefix," . $this-&gt;get_distance_SQL($lat, $lon), FALSE)
		-&gt;from('users')
		-&gt;join('zip_code',"users.zip_code = zip_code.zip_code")
		-&gt;order_by('distance', 'asc');

$search_results = $this-&gt;db-&gt;get();
</pre>
<p>And shazam, you now have a way to sort a set of SQL results by distance.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/sorting-records-by-distance-using-php-mysql/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Making CodeIgniter&#8217;s Pagination library play nice with jQuery</title>
		<link>http://scottblaine.com/making-codeigniters-pagination-library-play-nice-with-jquery</link>
		<comments>http://scottblaine.com/making-codeigniters-pagination-library-play-nice-with-jquery#comments</comments>
		<pubDate>Tue, 23 Jun 2009 01:00:28 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[codeigniter]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=249</guid>
		<description><![CDATA[I&#8217;m back, and despite the fact that I haven&#8217;t posted in a bit I&#8217;ve been accomplishing quite a bit on the site &#8230;or maybe not. You see, I started keeping track of the hours I&#8217;ve been spending working on the site. Over the last 6 days I&#8217;ve put in 11 hours on the site. That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m back, and despite the fact that I haven&#8217;t posted in a bit I&#8217;ve been accomplishing quite a bit on the site &#8230;or maybe not. You see, I started keeping track of the hours I&#8217;ve been spending working on the site. Over the last 6 days I&#8217;ve put in 11 hours on the site. That&#8217;s a pretty good amount of time I&#8217;ve put in, actually, considering that my goal is to put in at least 8 hours (the equivalent of one work day) a week on the site.</p>
<p>On the (perhaps) not so good side, the majority of that time has been spent implementing pagination on the search results page. You would <em>think</em> this would be incredibly easy considering that <a title="CodeIgniter's Pagination Class" href="http://codeigniter.com/user_guide/libraries/pagination.html">CodeIgniter has a Pagination class</a>. It shouldn&#8217;t take me approximately 8 hours, right? Well, apparently it does when you&#8217;re trying to use AJAX to handle your pagination. I was even perfectly willing to accept a complete hack for a solution, but alas, anything I tried to do to cobble together a fix for one issue only introduced another issue somewhere else.</p>
<p>Eventually jQuery came to my rescue. In the configuration for Pagination I set the base_url to &#8221; (an empty string) so that the href on the pagination links would contain only the number for the search results. Then I set up jQuery to find all of the anchor links in my paging div, stop the normal link function, and call my function to load in the search results. The jQuery code came out like this:</p>
<pre>
$(document).ready(function(){
  $("div.paging &gt; a").click(function(e){
    // stop normal link click
    e.preventDefault();

    $('div#search_results').load("/search/get_results/"+$(this).attr("href"));
  });
});
</pre>
<p>This is still something of a hack as I&#8217;m basically creating fake href values for the anchor tags, but whatever, it works! I can come back later and fix it. At this point hacks are okay. Speed is much more valuable than perfectly elegant code as I want to find out if my idea is solid before investing oodles of time in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/making-codeigniters-pagination-library-play-nice-with-jquery/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Form validation callbacks and private functions</title>
		<link>http://scottblaine.com/form-validation-callbacks-and-private-functions</link>
		<comments>http://scottblaine.com/form-validation-callbacks-and-private-functions#comments</comments>
		<pubDate>Tue, 02 Jun 2009 01:00:02 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=149</guid>
		<description><![CDATA[If you&#8217;re familiar with CodeIgniter you probably know about callbacks within form validation. Callbacks allow you to do your own validation of fields. For example, if you want to verify if a username is unique then you could create a username_check function to validate the field. You add the callback rule like this: $this->form_validation->set_rules( 'username', [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re familiar with CodeIgniter you probably know about <a title="Form Validation" href="http://codeigniter.com/user_guide/libraries/validation.html">callbacks within form validation</a>. Callbacks allow you to do your own validation of fields. For example, if you want to verify if a username is unique then you could create a username_check function to validate the field. You add the callback rule like this:</p>
<pre>
$this->form_validation->set_rules( 'username', 'Username',
  'trim|required|callback_username_check' );
</pre>
<p>And then create a matching function like this:</p>
<pre>
function username_check( $username )
{
  // some code
}
?>
</pre>
<p>However, as this is currently implemented someone could access your function as a page at a URL like example.com/index.php/login/<strong>username_check</strong>/ if they guessed the function name. While that may not have any ill side-effects, it&#8217;s probably just as well if no one can access the function besides you.</p>
<p>In come <a title="Controllers" href="http://codeigniter.com/user_guide/general/controllers.html">private functions for controllers</a>, which allow you to create a function like this:</p>
<pre>
function _utility()
{
  // some code
}
</pre>
<p>And if you try to access the function via a URL, like example.com/index.php/login/<strong>_utility</strong>/, you&#8217;ll get a 404 (page not found).</p>
<p>You probably see where I&#8217;m going with this. If you create your callbacks as private functions, no one will be able to access the callbacks as pages. It&#8217;s quite simple to do. You add an underscore before your callback function name:</p>
<pre>
function _username_check( $username )
{
  $valid_username = TRUE; // You would perform some kind of check on the field here

  if ($valid_username == FALSE)
  {
    $this->form_validation->set_message('_username_check', 'The username you have provided is not valid.');
    return FALSE;
  }
  else
  {
    return TRUE;
  }
}
</pre>
<p>And then add an underscore in your callback rule (note the two underscores after callback):</p>
<pre>
$this->form_validation->set_rules( 'username', 'Username',
  'trim|required|callback__username_check' );
</pre>
<p>Done!</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/form-validation-callbacks-and-private-functions/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Authentication with CodeIgniter</title>
		<link>http://scottblaine.com/authentication-with-codeigniter</link>
		<comments>http://scottblaine.com/authentication-with-codeigniter#comments</comments>
		<pubDate>Sat, 30 May 2009 01:00:08 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[redux]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=140</guid>
		<description><![CDATA[Having finished the registration process, I realized I needed some way to keep people logged in, let them log in, log out, retrieve their password, possibly have some kind of email validation, etc. All sorts of things. I started looking through the CodeIgniter user guide and about they closest things they have is the session [...]]]></description>
			<content:encoded><![CDATA[<p>Having finished the registration process, I realized I needed some way to keep people logged in, let them log in, log out, retrieve their password, possibly have some kind of email validation, etc. All sorts of things. I started looking through the CodeIgniter user guide and about they closest things they have is <a title="CodeIgniter's Session Class" href="http://codeigniter.com/user_guide/libraries/sessions.html">the session library</a>. While you could use it to track if a user is logged in, that&#8217;s about the extent of the value it would provide to you.</p>
<p>At first I looked for ways that I might be able to at least run checks on pages to see if a user is logged in or not. It turns out that&#8217;s not terribly difficult (once you figure out how to do it). You need to <a title="Authentication with CodeIgniter - David Winter" href="http://davidwinter.me.uk/articles/2009/02/21/authentication-with-codeigniter/">create your own authentication controller</a> for use with pages where you want to check if the user is logged in. Cool, that works. I could throw the log in/log out functionality in easily enough, but resetting passwords and validating email addresses would be a whole different matter.</p>
<p>That&#8217;s when I decided that I should start looking for a user-contributed authentication library. The CodeIgniter Wiki has <a title="CodeIgniter Authentication Libraries" href="http://codeigniter.com/wiki/Category:Contributions::Libraries::Authentication/">a nice list of them.</a> As I started looking through them and all their various features I realized I should come up with a list of the features that I needed to have. Here&#8217;s what I was looking for in a library:</p>
<ul>
<li>Good coding practices</li>
<li>Good documentation</li>
<li>Small number of files</li>
<li>Database implementation that&#8217;s not complex</li>
<li>Login using username or email address</li>
<li>Emails for lost passwords</li>
<li>Automatic login</li>
<li>Hashing of passwords</li>
<li>Maximum number of failed login attempts</li>
<li>Emails for activation (nice to have)</li>
<li>reCAPTCHA support (nice to have)</li>
</ul>
<p>Not too harsh, right? Unfortunately most of the libraries ended up being too large, too simple, or completely lacking in documentation. It was hard to find a library that fit somewhere in the middle. I did find one library that might meet all of my requirements, and if not, it&#8217;s awfully close: <a title="reduxauth" href="http://code.google.com/p/reduxauth/">Redux 2 (beta)</a>. It has all of the functionality I want, it doesn&#8217;t contain a crazy number of files, the database is simple and well thought out, there&#8217;s a complete sample application that makes use of the library, and more. I&#8217;m going to give it a try and comment in my next post on how well it works.</p>
<p>I should also mention in the course of my search I came across <a title="What Code Igniter authentication library is best? - Stack Overflow" href="http://stackoverflow.com/questions/346980/what-code-igniter-authentication-library-is-best">an excellent post on Stack Overflow regarding authentication libraries for CodeIgniter</a>. There&#8217;s a lot of great discussion on that page and Redux was also listed there as being one of the better solutions available.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/authentication-with-codeigniter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

