<?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>I want to be free &#187; ideas</title>
	<atom:link href="http://i1t2b3.com/category/ideas/feed/" rel="self" type="application/rss+xml" />
	<link>http://i1t2b3.com</link>
	<description>Any fool can make things bigger and more complex</description>
	<lastBuildDate>Wed, 11 Apr 2012 12:59:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Delete Your Code #4: Ambiguous terms</title>
		<link>http://i1t2b3.com/2011/12/14/ambiguous-terms/</link>
		<comments>http://i1t2b3.com/2011/12/14/ambiguous-terms/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 21:36:31 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[delete your code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=796</guid>
		<description><![CDATA[Every application operates in a knowledge domain. File browser works with files, directories and drives. Library software deals with books and editions. Geo tool must be aware of coordinates. Idea is, developers can save time and lines of code if they agree the terms of the knowledge domain and be strict with it. Of course, [...]]]></description>
			<content:encoded><![CDATA[<p>Every application operates in a knowledge domain.</p>
<p>File browser works with files, directories and drives. Library software deals with books and editions. Geo tool must be aware of coordinates.</p>
<p>Idea is, developers can save time and lines of code if they agree the terms of the knowledge domain and be strict with it.</p>
<p><img src="http://i1t2b3.com/wp-content/uploads/2011/12/signs.jpg" alt="" title="Ambiguous terms" width="200" height="164" class="alignright size-full wp-image-800" />Of course, all of us use some terms — this rule is to use a well defined set of terms and to avoid synonyms.</p>
<p>Examples of these rule violations:</p>
<ul>
<li>&#8220;folder&#8221; vs. &#8220;directory&#8221;;</li>
<li>&#8220;date&#8221; or &#8220;timestamp&#8221;;</li>
<li>&#8220;partner&#8221; a.k.a. &#8220;affiliate&#8221;;</li>
<li>&#8220;DNS Provider&#8221; as &#8220;product supplier&#8221;;</li>
<li>short for &#8220;longitude&#8221; is &#8220;lon&#8221; or &#8220;lng&#8221;?</li>
<li>does &#8220;website address&#8221; differ from &#8220;URL&#8221;?</li>
<li>&#8220;region&#8221;, &#8220;area&#8221; and &#8220;subarea&#8221; &mdash; in contrast to ADM1, ADM2 and ADM3;</li>
<li>the application I deal with at my job has: &#8220;brand&#8221;, &#8220;model&#8221;, &#8220;market&#8221; and &#8220;platform&#8221;, which are also often called &#8220;manufacturer&#8221;, &#8220;vehicle&#8221;, &#8220;locale&#8221; and &#8220;client&#8221; respectively.</li>
</ul>
<p>If you apply this rule, you will not waste time creating the multiple mapper classes. More reusage, more OOP inheritance, more rapport. More motivation to work on a solid, well defined project with people, you talk the same language with. Isn&#8217;t this nice?</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/12/14/ambiguous-terms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete Your Code #2: less OOP visibility keywords</title>
		<link>http://i1t2b3.com/2011/12/06/delete-your-code-1-less-oop-visibility-keywords/</link>
		<comments>http://i1t2b3.com/2011/12/06/delete-your-code-1-less-oop-visibility-keywords/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 17:17:11 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[delete your code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=740</guid>
		<description><![CDATA[Hey! I gonna describe tricks that I use to have less code. Why it is important to have less code, you ask? Less code means less bugs, less support, less developer brains waste. Today&#8217;s trick is extremely simple &#8212; when you have a long set of public, protected or private class properties, remove the visibility [...]]]></description>
			<content:encoded><![CDATA[<p>Hey! I gonna describe tricks that I use to have less code. Why it is important to have less code, you ask? Less code means less bugs, less support, less developer brains waste.</p>
<p>Today&#8217;s trick is extremely simple &mdash; when you have a long set of <code>public</code>, <code>protected</code> or <code>private</code> class properties, remove the visibility keyword set to each declaration but define it once as comma separated declaration.</p>
<p>Same applies to class constants as well.</p>
<p>Example:</p>
<p><code class="php">
<pre>// Before:
class Cat {
    const KINGDOM = 'Animalia';
    const PHYLUM  = 'Chordata';
    const FAMILY  = 'Felidae';

    public $tail;
    public $whisker = '\/';
    public $head;
    public $legs = array(1,2,3,4);
}
</pre>
<p></code></p>
<p><code class="php">
<pre>// After:
class Cat {
    const
        KINGDOM = 'Animalia',
        PHYLUM  = 'Chordata',
        FAMILY  = 'Felidae';

    public
        $tail,
        $whisker  = '\/',
        $head,
        $legs = array(1,2,3,4);
}
</pre>
<p></code></p>
<p>One benefit is that the code looks clear and it&#8217;s much easier to scan rather then to read.</p>
<p>Second benefit is when you need to change the visibility for a property, you don&#8217;t have to edit the visibility keyword near the property name (which might be an error prone process when you are tired) &mdash; you just move the line up or down, which usually has a shortcut in IDE.</p>
<p>Known disadvantage is that most documenting engines don&#8217;t support this ferature.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/12/06/delete-your-code-1-less-oop-visibility-keywords/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>6 points against writing an own CMS</title>
		<link>http://i1t2b3.com/2011/07/25/6-points-against-writing-own-cms/</link>
		<comments>http://i1t2b3.com/2011/07/25/6-points-against-writing-own-cms/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 19:50:29 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=734</guid>
		<description><![CDATA[Once upon a time I was asked at my job to work on company website. There was a choice among existing CMS for the website to be driven by, plus I could think about implemention of something of my own &#8212; but you know what? I didn&#8217;t feel comfortable to start a CMS from scratch. [...]]]></description>
			<content:encoded><![CDATA[<p>Once upon a time I was asked at my job to work on company website. There was a choice among existing CMS for the website to be driven by, plus I could think about implemention of something of my own &mdash; but you know what? I didn&#8217;t feel comfortable to start a CMS from scratch. Now I know why, exactly.</p>
<p>I found this nice article called <noindex><a rel="nofollow" href="http://www.contenthere.net/2007/07/homebrew-cms.html">Homebrew CMS</a> (by Seth Gottlieb)</noindex>, which lists six points of a modern content management system which you as architect should consider before a start:</p>
<ol>
<li>Versioning &mdash; it makes data model much more sophisticated;</li>
<li>Localization &mdash; should images be translated, what is your default language, etc;</li>
<li>Preview &mdash; the content being previewed must be connected with all the website and not visible to others;</li>
<li>Deployment &mdash; dependency management like images which are pasted in the content;</li>
<li>Usability &mdash;  <i>is probably the most common reason why companies abandon their home grown CMS;</i></li>
<li>Access control &mdash; your system must control not functions only, but also the data.</li>
</ol>
<p>All in all, an open source free CMS might be the best choice for you too. Spare your energy for something else.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/07/25/6-points-against-writing-own-cms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web application check list: geo data</title>
		<link>http://i1t2b3.com/2011/07/08/web-application-check-list-geo-data/</link>
		<comments>http://i1t2b3.com/2011/07/08/web-application-check-list-geo-data/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 12:49:48 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=595</guid>
		<description><![CDATA[Things you would likely to check in your application. First story is about geo and localization features of your application. Detect browser languages, find the one your application supports and set it as your application&#8217;s current languages. If it&#8217;s not set, first auto-detect country (see below) and then use the country&#8217;s main language. Auto-detect country: [...]]]></description>
			<content:encoded><![CDATA[<p>Things you would likely to check in your application. First story is about geo and localization features of your application.</p>
<ol>
<li>Detect browser languages, find the one your application supports and set it as your application&#8217;s current languages. If it&#8217;s not set, first auto-detect country (see below) and then use the country&#8217;s main language.</li>
<li>Auto-detect country: first by IP (free database of countries IP addresses can be downloaded <noindex><a rel="nofollow"  href="http://www.maxmind.com/app/geoip_country">here</a></noindex>), then by browser locale (en-US means it&#8217;s a USA user), then by auto-detected language (<em>ru</em> language gonna mean Russia. Yes, the user can be located in Ukraine or Belarus, not in Russia &#8211; but let&#8217;s face it, it&#8217;s much closer than nothing).</li>
<li>On the grounds you&#8217;ve detected the country, you can preset the currency for the user.</li>
<li>Save this data in user&#8217;s profile when he/she registers in your application. If your application cares about other languages the user can speak, you can parse the rest of browser languages and save them too.</li>
<li>If entities of your application can be located on map, set <noindex><a rel="nofollow"  href="http://geotags.com/geo/geotags2.html">geo meta tags</a></noindex> to tell the search engines about place your entity is at. (<noindex><a rel="nofollow" rel="nofollow" href="http://googlewebmastercentral.blogspot.com/2008/08/how-to-start-multilingual-site.html?showComment=1218284034655#c7625131685626266953">success story</a></noindex>). Find out more on geotagging at <noindex><a  rel="nofollow" href="http://en.wikipedia.org/wiki/Geotagging">Wikipedia</a></noindex>.</li>
</ol>
<p>List of countries with a set of supported languages and default currency code for each one can be got from GeoNames (<noindex><a rel="nofollow"  href="http://download.geonames.org/export/dump/countryInfo.txt">countries info dump</a></noindex>).</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/07/08/web-application-check-list-geo-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tech improvements would be funny to have</title>
		<link>http://i1t2b3.com/2011/06/29/fun-improvements/</link>
		<comments>http://i1t2b3.com/2011/06/29/fun-improvements/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 18:30:01 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[fun]]></category>
		<category><![CDATA[ideas]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=374</guid>
		<description><![CDATA[a password field with auto-suggest feature a message telling you that &#8216;Such password is already taken&#8216; (especially indicating by which user) a authentication system that recognizes you not by the username and password you&#8217;ve typed, but by the speed of typing and delays between characters case sensative domains names a hyper link, that opens multiple [...]]]></description>
			<content:encoded><![CDATA[<ul>
<li>a password field with auto-suggest feature</li>
<li>a message telling you that &#8216;<em>Such password is already taken</em>&#8216; (especially indicating by which user)</li>
<li>a authentication system that recognizes you not by the username and password you&#8217;ve typed, but by the speed of typing and delays between characters</li>
<li><img src="http://i1t2b3.com/wp-content/uploads/2011/06/5F7AD-eye-blink1.gif" alt="" title="eye-blink" width="111" height="135" class="alignright size-full wp-image-718" /> case sensative domains names</li>
<li>a hyper link, that opens multiple windows when clicked</li>
<li> a page advert analyzing your face via webcam and rotating banners when you blink</li>
<li>porn sites that capture the video of you via your webcam while you surf them (maybe it exists already? beware!)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/06/29/fun-improvements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open source project as a CV</title>
		<link>http://i1t2b3.com/2011/06/21/open-source-project-as-a-cv/</link>
		<comments>http://i1t2b3.com/2011/06/21/open-source-project-as-a-cv/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 20:56:41 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[marketing]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=118</guid>
		<description><![CDATA[I think it&#8217;s a nice idea to contribute to open source project at least to just highlight this fact in your CV/resume when you are looking for a job. Benefits: you show your level of commitment to something money is not the only motivator for you the code written by you is publically visible — [...]]]></description>
			<content:encoded><![CDATA[<p>I think it&#8217;s a nice idea to contribute to open source project at least to just highlight this fact in your CV/resume when you are looking for a job.</p>
<p>Benefits:
<ul>
<li>you show your level of commitment to something</li>
<li>money is not the only motivator for you</li>
<li>the code written by you is publically visible — you don&#8217;t need to explain what major design patterns you know, which technologies you are familiar with and how good you usually polish your code, that all can be seen</li>
</ul>
<p><img class="alignright size-full wp-image-708" title="captain Ellerby" src="http://i1t2b3.com/wp-content/uploads/2011/06/captain-ellerby.jpg" alt="" width="164" height="213" /></p>
<p>I think it can be compared with marriage. Remember that Alec Baldwin quote  from &#8220;The Departed&#8221; movie?</p>
<blockquote><p><em>Marriage is an important part of getting ahead: lets people know you&#8217;re not a homo; married guy seems more stable; people see the ring, they think at least somebody can stand the son of a bitch; ladies see the ring, they know immediately you must have some cash or your cock must work.</em></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/06/21/open-source-project-as-a-cv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 reasons not to use Assembla</title>
		<link>http://i1t2b3.com/2011/06/14/10-reasons-not-to-use-assembla/</link>
		<comments>http://i1t2b3.com/2011/06/14/10-reasons-not-to-use-assembla/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 13:24:03 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[assembla]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=541</guid>
		<description><![CDATA[Although Assembla is one of the best ticket systems, I hate it at times. Main reason is that their team plays with fonts and colors and thus changes the site appearence, and at the same time ignores the major issues. Here is the list that irritates me the most: When you create a ticket, the [...]]]></description>
			<content:encoded><![CDATA[<p>Although Assembla is one of the best ticket systems, I hate it at times. Main reason is that their team plays with fonts and colors and thus changes the site appearence, and at the same time ignores the major issues.</p>
<p>Here is the list that irritates me the most:</p>
<ol>
<li>When you create a ticket, the editor doesn&#8217;t work properly. For example, if you hightlight a URL with intention to mark it as a link, they still ask you for the URL in a prompt (a month ago they would even just replaced it with a default markup text like &#8220;<em>[[http://server.com | URL TEXT]]</em>&#8220;)</li>
<li>The same is WIKI based on tinyMCE — it fails so often, that 50% of times I have to open the content HTML source and fix things manually</li>
<li>Affiliate system — it pays you back only if you refered 3+ new users. I have just 2 leads, and I loose $80 every month&#8230;</li>
<li>Some time ago they had an offer to blog about them and get $5 reward — I wrote a post, and their marketing guy Ryan told me that they gonna pay me only after a referal would pay a bill — which was not mentioned in the offer.</li>
<li>If you want to export tickets data (for example, to count some stats regarding development performance), you will have to deal with their internal semi-JSON format with no documentation (only not-so-self-explanatory feilds names)</li>
<li>Previously they had a project name in project space URL, now they use a unreadable hash — if you work with more then 1 project, you&#8217;d feel how unhandy it is</li>
<li>There are 3 (three!) different SVN repositories types (SVN+Trac, External SVN, Source/SVN) in Tools which is confusing. Every one has its own set of possibilities — you have to choose what is more vital for you, you cannot have it all.</li>
<li>Current filter choise is lost if you leave Assembla page for 5-10 minutes, and you have to switch to it over again. The choise is saved in a HTTP-secure cookie (it means you cannot change it by JavaScript). Damn, why?</li>
<li>They don&#8217;t show hours total for tickets  no matter how you group them. It&#8217;s one of the most important things for developers, guys.</li>
<li>If you want just to get content of the project, you cannot.  I mean not SVN Checkout, but kind of SVN Export — download all folders/files without SVN client. They had a button that allowed to download the project as ZIP archieve, but now it&#8217;s not there. I have a few PHP projects which many people download and use, and they complain often about this missing feature.</li>
</ol>
<p>All in all, Assembla still has a big way to go to maturity.</p>
<p>UPDATE [Feb 7, 2012] &#8211; previously, when you click on a milestone name on a ticket page, you go to the milestone and can see all tickets. Now you get the ugly build-in JavaScript prompt to rename the milestone. WAT?!%$??</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/06/14/10-reasons-not-to-use-assembla/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bulk SQL loading</title>
		<link>http://i1t2b3.com/2011/05/30/bulk-sql-loading/</link>
		<comments>http://i1t2b3.com/2011/05/30/bulk-sql-loading/#comments</comments>
		<pubDate>Mon, 30 May 2011 13:59:50 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=349</guid>
		<description><![CDATA[If you want to load a list of SQL files into your database on Windows, you can create a .cmd file with such content and run it. Place it in the same folder where your .sql files are. @echo off SET DATABASE=my_database SET USER=root SET PASS=1 SET FORMAT=*.sql FOR /F "usebackq" %%i IN (`dir /on [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to load a list of SQL files into your database on Windows, you can create a .cmd file with such content and run it.<br />
Place it in the same folder where your .sql files are.</p>
<pre><code class="dos">@echo off
SET DATABASE=my_database
SET USER=root
SET PASS=1
SET FORMAT=*.sql

FOR /F "usebackq" %%i IN (`dir /on /b %FORMAT%`) DO echo %%i &amp;&amp; mysql --user=%USER% --password=%PASS% --default-character-set=utf8 %DATABASE% &lt; %%i</code></pre>
<p>Make sure, <code>mysql</code> command can be run. If no, either set the full path to mysql command tool, or add the path to PATH environment variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2011/05/30/bulk-sql-loading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to rate Points Of Interest automatically</title>
		<link>http://i1t2b3.com/2010/03/13/how-to-rate-points-of-interest-automatically/</link>
		<comments>http://i1t2b3.com/2010/03/13/how-to-rate-points-of-interest-automatically/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 13:13:31 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[ideas]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=579</guid>
		<description><![CDATA[During my work on sunnyrentals.com I&#8217;ve got a task to add 2 closest airports to every property an owner creates. I implemented it quite fast since we have a database of airports with coordinates. While playing with this feature, we found out that it is not good enough — the real task must be to [...]]]></description>
			<content:encoded><![CDATA[<p>During my work on <strong>sunnyrentals.com</strong> I&#8217;ve got a task to add 2 closest airports to every property an owner creates.</p>
<p>I implemented it quite fast since we have a database of airports with coordinates.</p>
<p>While playing with this feature, we found out that it is not good enough — the real task must be to add 2 <strong>closest and biggest</strong> airports. The problem is that we don&#8217;t have any data in airports DB to guess how big or famous a particular airport is.</p>
<p>So we need to rate every airport somehow&#8230;</p>
<p>The solution we found was simple — we need to google for the airport name and get the search results count. The count can be considered as rating value — London Heathrow airport has 2.33 million results while Kiev Zhulyany airport has only 0.77 mln which looks fair.</p>
<p>Several things to pay attention to:</p>
<ul>
<li>the query we formed was [city name] + [airport name] + &#8216; airport&#8217;</li>
<li>if this query gives zero result, I omit the city name — at times it hepls</li>
<li>we put the query into quotes to google for the exact phrase, otherwise the London City airport gets the highest rating due to the fact that &#8220;<em>city</em>&#8221; is a general term</li>
<li>if the airport name includes the city name (<em>Melbourne Intl</em>), we omit the city name — &#8221;<em>Melbourne Intl airport</em>&#8221; is better then &#8220;<em>Melbourne Melbourne Intl airport</em>&#8220;</li>
<li>in addition to previous idea — if the airport name sounds like the city name, we omit the city name as well. Example: Narsarsuaq airport in Narssarssuaq city. I used <code>soundex</code> function for this comparison — it&#8217;s present in PHP and MySQL.</li>
</ul>
<p>To get the google results you can use the <noindex><a rel="nofollow" href="http://code.google.com/intl/en-US/apis/ajaxsearch/documentation/#fonje">Google Search API</a></noindex>:</p>
<pre><code>$queryTemplate = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&amp;q=%s';
$airportQuery = '"London Heathrow airport"';
$query = sprintf( $queryTemplate, urlencode( $airportQuery ) );
$json = json_decode( file_get_contents( $query ), 1 );
$rating = (int)$json['responseData']['cursor']['estimatedResultCount'];</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2010/03/13/how-to-rate-points-of-interest-automatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Currency exchange in your application</title>
		<link>http://i1t2b3.com/2009/09/28/currency-exchange/</link>
		<comments>http://i1t2b3.com/2009/09/28/currency-exchange/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 10:53:17 +0000</pubDate>
		<dc:creator>Skakunov Alexander</dc:creator>
				<category><![CDATA[db]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://i1t2b3.com/?p=555</guid>
		<description><![CDATA[That&#8217;s easy, you need 2 things: Fresh currencies exchange rates Some way to excange amount from one currency to another. This how I did it: get values from European Central Bank (ECB) for step #1 and wrote MySQL user defined function for step #2. Here is how to export currencies rates from ECB (EUR is [...]]]></description>
			<content:encoded><![CDATA[<p>That&#8217;s easy, you need 2 things:</p>
<ol>
<li>Fresh currencies exchange rates</li>
<li>Some way to excange amount from one currency to another.</li>
</ol>
<p>This how I did it: get values from European Central Bank (ECB) for step #1 and wrote MySQL user defined function for step #2.</p>
<p>Here is how to export currencies rates from ECB (EUR is a base currency, and I add self rate as 1:1). First I create such  database table:</p>
<pre><code class="sql">CREATE TABLE IF NOT EXISTS `currency` (
  `code` char(3) NOT NULL DEFAULT '',
  `rate` decimal(10,5) NOT NULL COMMENT 'Rate to EUR got from www.ecb.int',
  PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Currency rates (regularly updated)';
</code></pre>
<p>Now let&#8217;s fill it with rates:</p>
<pre><code class="php">&lt;?php

class CurrencyController extends Controller_Ajax_Action {

  public function importAction() {

    $db = Zend_Registry::get('db');
    $db-&gt;beginTransaction();

    $url = 'http://www.ecb.int/stats/eurofxref/eurofxref.zip?1c7a343768baab4322620e3498553b5a';
    try {
      $contents = file_get_contents($url);
      $contents = archive::unzip($contents);
      $contents = explode(&quot;\n&quot;, $contents);

      $names = explode(',', $contents[0]);
      $rates = explode(',', $contents[1]);

      $names[] = 'EUR';
      $rates[] = 1;

      for ($i = 1; $i &lt; sizeof($names); $i++) {
        if (!(float) $rates[$i]) continue;
        $db-&gt;query( sprintf('INSERT INTO `currency`(`code`, `rate`)
              VALUES (&quot;%s&quot;, %10.5f)
              ON DUPLICATE KEY UPDATE `rate`=VALUES(`rate`)',
             trim( $names[$i] ),
             trim( $rates[$i] )
        ) );
      }

      $db-&gt;commit();
    } catch ( Exception $O_o ) {
      error_log( $O_o-&gt;getMessage() );
      $db-&gt;rollback();
    }

  }
}</code></pre>
<p>Now let&#8217;s create a SQL function for handy converts. Create a <code>udf.sql</code> file and add this in it:</p>
<pre><code class="sql">DELIMITER //

DROP  FUNCTION IF EXISTS EXCHANGE;
CREATE FUNCTION EXCHANGE( amount DOUBLE, cFrom CHAR(3), cTo CHAR(3) ) RETURNS DOUBLE READS SQL DATA DETERMINISTIC
    COMMENT 'converts money amount from one currency to another'
BEGIN
    DECLARE rateFrom DOUBLE DEFAULT 0;
    DECLARE rateTo DOUBLE DEFAULT 0;

    SELECT `rate` INTO rateFrom FROM `currency` WHERE `code` = cFrom;
    SELECT `rate` INTO rateTo   FROM `currency` WHERE `code` = cTo;

    IF ISNULL( rateFrom ) OR ISNULL( rateTo ) THEN
        RETURN NULL;
    END IF;

    RETURN amount * rateTo / rateFrom;
END; //

DELIMITER ;</code></pre>
<p>and run this command in your shell:</p>
<pre><code>mysql --user=USER --password=PASS DATABASE &lt; udf.sql</code></pre>
<p>This is how you can use this function &mdash; how to convert 10 US dollars to Canadian dollars:</p>
<pre><code class="sql">SELECT EXCHANGE( 10, 'USD', 'CAD')</code></pre>
<p>which results in $10 = 10.93 Canadian dollars.</p>
<p>P.S. Consider adding the currency export action call to your cron scripts.</p>
<p>P.P.S. A function to unzip the data file can be got at <noindex><a rel="nofollow" href="http://ua2.php.net/manual/en/ref.zip.php">php.net</a></noindex></p>
]]></content:encoded>
			<wfw:commentRss>http://i1t2b3.com/2009/09/28/currency-exchange/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

