<?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>Coderholic &#187; web dev</title>
	<atom:link href="http://www.coderholic.com/category/web-dev/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coderholic.com</link>
	<description>Addicted to Development</description>
	<lastBuildDate>Tue, 31 Aug 2010 09:19:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JavaScript: The Good Parts</title>
		<link>http://www.coderholic.com/javascript-the-good-parts/</link>
		<comments>http://www.coderholic.com/javascript-the-good-parts/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 10:44:37 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[books]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=745</guid>
		<description><![CDATA[I&#8217;ve been a huge fan of Douglas Crockford and his articles about JavaScript for a long time. I often point people to his The World&#8217;s Most Misunderstood Programming Language article when I hear them complaining about the language. It&#8217;s taken me a couple of years to get around to reading his book, JavaScript: The Good [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://crockford.com/goodparts.gif" style="float: left; border: 1px solid grey; margin-right: 10px;"/> I&#8217;ve been a huge fan of Douglas Crockford and his <a href="http://javascript.crockford.com/">articles about JavaScript</a> for a long time. I often point people to his <a href="http://javascript.crockford.com/javascript.html">The World&#8217;s Most Misunderstood Programming Language</a> article when I hear them complaining about the language. It&#8217;s taken me a couple of years to get around to reading his book, <a href="http://www.amazon.co.uk/gp/product/0596517742?ie=UTF8&#038;tag=dowlingmeuk-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=0596517742">JavaScript: The Good Parts</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=dowlingmeuk-21&#038;l=as2&#038;o=2&#038;a=0596517742" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, but it exceeded all of my expectations.</p>
<p>Being an avid reader of his online stuff, and having watched various <a href="http://www.youtube.com/results?search_query=crockford+javascript&#038;aq=f">talks of his</a> I knew the book would be well written and informative, but I thought it&#8217;d probably just repeat much of what I&#8217;d already read, without providing much new information or insights. To a certain extent this was true, the book does reiterate what&#8217;s said in many of his online articles and talks, but the book is absolutely amazing for a different reason: <strong>The code examples</strong>.</p>
<p>The book is extremely succinct. There&#8217;s no padding, and very little dialog to join one section to the next. There is a common thread thoughout the book though, and that&#8217;s the code. Examples of good coding practices are repeated, and functions written in earlier chapters are often reused in later ones. So although the book appears to be presenting one feature at a time with a small code example you&#8217;re actually building up more and more complex javascript applications, and the book ends with a full JSON parser!</p>
<p>Crockford&#8217;s coding style seems to match his writing style: succinct and to the point. There&#8217;s some really great code in the book, that I think even non-JavaScript developers could appreciate. Here&#8217;s my favourite, from the chapter on regular expressions:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> parse_url <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.ora.com:80/goodparts?q#fragment'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> parse_url.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> names <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'url'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'scheme'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'slash'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'host'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'port'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'path'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'query'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'hash'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> blanks <span style="color: #339933;">=</span> <span style="color: #3366CC;">'       '</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> names.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">writeln</span><span style="color: #009900;">&#40;</span>names<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">':'</span> <span style="color: #339933;">+</span> blanks.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>names<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> result<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which outputs:</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">url:    http://www.ora.com:80/goodparts?q#fragment
scheme: http
slash:  //
host:   www.ora.com
port:   80
path:   goodparts
query:  q
hash:   fragment</pre></div></div>

<p>I also love the explanations Crockford gives as to why he avoids using certain parts of JavaScript, and limits himself to a &#8220;good&#8221; subset of the language. He doesn&#8217;t simply say &#8220;This is bad and you shouldn&#8217;t use it&#8221;, it&#8217;s usually accompanied by a story about a time when Crockford had used that feature and got caught out, which is much more compelling.</p>
<p>I think it&#8217;s fair to say that <a href="http://www.amazon.co.uk/gp/product/0596517742?ie=UTF8&#038;tag=dowlingmeuk-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=0596517742">JavaScript: The Good Parts</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=dowlingmeuk-21&#038;l=as2&#038;o=2&#038;a=0596517742" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> is one of my favourite programming books: It&#8217;s succinct, packed full of great code examples and best practices for writing maintainable and bug free code. It&#8217;s a fantastic JavaScript reference, but much of what the book talks about is relevant to programming in general. Highly recommended!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/javascript-the-good-parts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>8 Reasons Why You Should Try Django</title>
		<link>http://www.coderholic.com/8-reasons-why-you-should-try-django/</link>
		<comments>http://www.coderholic.com/8-reasons-why-you-should-try-django/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 22:14:36 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=336</guid>
		<description><![CDATA[I have been using Python for quite a few years, but mostly for writing one off sysadmin scripts, command line utilities, and of course PyRadio. Most of my web development work has been done with PHP. The language gets a lot of bad press, some deserved and some not so much. I&#8217;ve had my own [...]]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-505 aligncenter" title="django" src="http://www.coderholic.com/wp-content/uploads/django1.png" alt="django" width="500" height="228" /></p>
<p>I have been using Python for quite a few years, but mostly for writing one off sysadmin scripts, command line utilities, and of course <a href="http://www.coderholic.com/pyradio">PyRadio</a>. Most of my web development work has been done with PHP. The language gets a lot of bad press, some deserved and some not so much. I&#8217;ve had my own <a href="http://www.coderholic.com/beware-phps-split/">gripes</a>, but all-in-all I&#8217;ve been fairly happy with PHP.</p>
<p>Several months ago, though, I thought I&#8217;d give <a href="http://www.djangoproject.com/">Django</a> a try, a python based web framework. I was completely blown away!  Compared to the PHP frameworks I&#8217;d worked with, such as <a href="http://cakephp.org/">Cake</a>, it was just so much more of a pleasure to work with. So here are 8 reasons why you should give Django a try yourself if you haven&#8217;t already. You won&#8217;t be disappointed!</p>
<p><strong>1. Great Documentation</strong></p>
<p>The Django documentation is well written, extremely comprehensive, and up to date. The <a href="http://docs.djangoproject.com/en/">official documentation</a> contains details API references, loads of relevant examples, and tutorials for those getting started. If that isn&#8217;t enough there&#8217;s also a whole <a href="http://www.djangobook.com/">book</a> that&#8217;s available for free online.</p>
<p><strong>2. It&#8217;s Python</strong></p>
<p>The fact that it&#8217;s Python is a huge plus point for me. It&#8217;s a great language that doesn&#8217;t suffer from many of the well documented inconsistencies that PHP does, and includes some nice features such as decorators and first-class functions. Going back to PHP you soon start to miss the little things, such as the ability to assign <a href="http://diveintopython.org/native_data_types/declaring_variables.html#odbchelper.multiassign">multiple values at once</a>, and the simplicity of <a href="http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice">slicing lists</a>.</p>
<p><strong>3. The ORM</strong></p>
<p>Django&#8217;s object relational mapper completely abstracts away the database, meaning you don&#8217;t need to worry about your database schema or constructing SQL queries. If you&#8217;re using to writing SQL queries then the <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/">QuerySet</a> API takes a little getting used to, but it&#8217;s really worth the effort. Projects like <a href="http://south.aeracode.org/">South</a> make the ORM even more powerful, allowing you to make schema changes and data migrations automatically.</p>
<p><strong>4. Built in Development Server</strong></p>
<p>Where PHP really shines is on its ease of deployment. Setting up a local development server can be a bit of a pain though, especially if you&#8217;re working on several different sites. Django comes with a built in development server though, so you can be up and running within minutes! From your project&#8217;s root directory you just do</p>
<pre>./manage.py runserver</pre>
<p>and access your django website from http://localhost:8000 &#8211; awesome!</p>
<p><strong>5. The Admin Interface</strong></p>
<p>Django&#8217;s built-in admin interface is practically a full blown CMS, allowing you to add, delete or update your data. It&#8217;s pretty much all automatic, but it&#8217;s also fairly configuration. See the <a href="http://docs.djangoproject.com/en/dev/intro/tutorial02/">documentation</a> to see what it can do!</p>
<p><strong>6. Reusable Applications</strong></p>
<p>Django projects are broken up into &#8220;applications&#8221;, and there are lots of existing reusable applications that you can use for your own projects, such as those for <a href="http://code.google.com/p/django-registration/">user registration</a>, <a href="http://github.com/flashingpumpkin/django-socialregistration">facebook integration</a>, <a href="http://github.com/nathanborror/django-basic-apps">blogging</a>, and <a href="http://github.com/search?langOverride=&amp;language=&amp;q=django&amp;repo=&amp;start_value=2&amp;type=Repositories&amp;x=13&amp;y=19">many many more</a>.</p>
<p>Existing applications are great, but the whole project/application distinction also forces you to think about your own project structure and therefore more likely to make reusable components that you can use in more of your own projects, or even share for others to use.</p>
<p><strong>7</strong><strong>. Templates</strong></p>
<p>I&#8217;ve always been a bit dubious about the merits of PHP template engines such as Smarty. The Django template layer is great though. The inheritance model works well, and the restrictive language really forces you to have a very clean separation of presentation and logic.</p>
<p><strong>8</strong><strong>. Forms</strong></p>
<p>I usually find dealing with user input one of the most boring parts of web development. It takes time to get it right, and its repetitive. The Django Form API really simplifies things. You can define your form class, include and validation rules, and simply add a few lines to your template and few lines to your view and you&#8217;re done!</p>
<hr/>
<p>So those are my 8 reasons why you should give django a go. If you&#8217;re already a django user let me know if you have any points to add!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/8-reasons-why-you-should-try-django/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Debugging Javascript in Internet Explorer 6</title>
		<link>http://www.coderholic.com/debugging-javascript-in-internet-explorer-6/</link>
		<comments>http://www.coderholic.com/debugging-javascript-in-internet-explorer-6/#comments</comments>
		<pubDate>Sat, 16 May 2009 16:33:17 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=244</guid>
		<description><![CDATA[If you&#8217;ve ever got to work with javascript and IE6 then you might want to read the guest post I wrote for Six Revisions: Debugging Javascript in Internet Explorer 6. The post describes how to configure IE6 so that instead of displaying subtle and cryptic error messages when you run into a Javascript issue it [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever got to work with javascript and IE6 then you might want to read the guest post I wrote for Six Revisions: <a href="http://sixrevisions.com/javascript/javascript-debugging-techniques-in-ie-6/">Debugging Javascript in Internet Explorer 6</a>. </p>
<p>The post describes how to configure IE6 so that instead of displaying subtle and cryptic error messages when you run into a Javascript issue it will instead allow you to debug the problem with the help of Visual Studio&#8217;s powerful debugger. I&#8217;ve put together a small slideshow that includes a summary of the content:</p>
<div style="width:425px;text-align:left" id="__ss_1444937"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/bendowling/debugging-javascript-in-ie6?type=presentation" title="Debugging Javascript in IE6">Debugging Javascript in IE6</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=ie6-090516112052-phpapp02&#038;stripped_title=debugging-javascript-in-ie6" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=ie6-090516112052-phpapp02&#038;stripped_title=debugging-javascript-in-ie6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/bendowling">bendowling</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/debugging-javascript-in-internet-explorer-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Ultimate Scalability Presentation</title>
		<link>http://www.coderholic.com/the-ultimate-scalability-presentation/</link>
		<comments>http://www.coderholic.com/the-ultimate-scalability-presentation/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 19:37:54 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[software design]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=231</guid>
		<description><![CDATA[At work we&#8217;re experiencing some fairly rapid growth, and our single production server is starting the feel the strain. I&#8217;ve been doing a lot of investigation into how we can scale the site, and thankfully there is lots of information out there. The &#8220;Do you Scale&#8221; presentation I saw at PHP London a couple of [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://www.mendeley.com">work</a> we&#8217;re experiencing some fairly rapid growth, and our single production server is starting the feel the strain. I&#8217;ve been doing a lot of investigation into how we can scale the site, and thankfully there is lots of information out there. </p>
<p>The &#8220;<a href="http://www.coderholic.com/php-london/">Do you  Scale</a>&#8221; presentation I saw at PHP London a couple of months ago gave a good high level overview of scalability issues, and included some useful techniques to help you scale.</p>
<p>I think I&#8217;ve found the ultimate scalability presentation though: &#8220;Real World Web: Performance &#038; Scalability&#8221;. The 189 slides contained within this presentation cover almost everything I&#8217;ve read elsewhere, and it&#8217;s packed full of practice advice!</p>
<p><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=rwwmysql2008-1216304757388368-8&#038;stripped_title=real-world-web-performance-scalability" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=rwwmysql2008-1216304757388368-8&#038;stripped_title=real-world-web-performance-scalability" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/the-ultimate-scalability-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Top 10 Freelance Developer Job Sites</title>
		<link>http://www.coderholic.com/top-10-freelance-developer-job-sites/</link>
		<comments>http://www.coderholic.com/top-10-freelance-developer-job-sites/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 06:59:34 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=133</guid>
		<description><![CDATA[Whether your a full time freelancer or someone who does projects on the side for a bit of extra cash the web is a great place to find new clients and exciting projects. Below is a list of ten of the best freelance development job sites on the Internet: 1. All Dev Jobs Mainly web [...]]]></description>
			<content:encoded><![CDATA[<p>Whether your a full time freelancer or someone who does projects on the side for a bit of extra cash the web is a great place to find new clients and exciting projects. Below is a list of ten of the best freelance development job sites on the Internet:</p>
<p>1. <a href="http://alldevjobs.com/jobs/development-jobs/freelance/">All Dev Jobs</a><br />
Mainly web development projects.</p>
<p>2. <a href="http://www.authenticjobs.com/">Authentic Jobs</a><br />
A fairly active job board that attracts some great project postings.</p>
<p>3. <a href="http://jobs.freelanceswitch.com/">Freelance Switch</a><br />
The freelance switch job board is very active, and contains a wide range of development jobs. There is a catch though: It costs $7 a month to reply to any of the postings.</p>
<p>4. <a href="http://jobs.jsninja.com/">Javascript Ninja Jobs</a><br />
This is a job board for freelance javascript jobs from John Resig, the author of the jQuery library.</p>
<p>5. <a href="http://jobs.metafilter.com/jobtype/1">MetaFilter Jobs</a><br />
Freelance development projects, mostly consisting of web and mobile application development.</p>
<p>6. <a href="http://www.noagenciesplease.com/">No Agencies Please</a><br />
Primarily a site for UK based work, but there are lots of remote projects. Includes a whole range of different development projects, and isn&#8217;t just limited to web development.</p>
<p>7. <a href="http://www.programmermeetdesigner.com/">Programmer Meet Designer</a><br />
A site that aims to introduce programmers to designers. Many of the projects that get listed are for skills exchanges (eg. you code my site I&#8217;ll design yours. There are occasionally paid jobs though, so it&#8217;s worth checking the site every so often. </p>
<p>8. <a href="http://www.searchwebjobs.com/">Search Web Jobs</a><br />
Lots of web development jobs, primarily involving CSS, HTML, PHP and MySQL.</p>
<p>9. <a href="http://jobs.smashingmagazine.com/freelance/programming">Smashing Jobs</a><br />
The job board of the online web development magazine.</p>
<p>10. <a href="http://jobs.webdirections.org/jobs/Developer/freelance/">Web Directions Jobs</a><br />
Web development jobs. Mostly PHP and Javascript.</p>
<p>So that&#8217;s 10 of the best places to find freelance development work online. Checking those 10 sites every for new projects isn&#8217;t easy though! That&#8217;s why I developed the next site.</p>
<p><strong>Bonus!</strong></p>
<p>11. <a href="http://jobs.plasis.co.uk/">Plasis Jobs</a><br />
Plasis Jobs aggregates freelance development jobs from 8 of the above 10 sites, so instead of having to check lots of different sites everyday to make sure you don&#8217;t miss out on a great new project you only need to check one! It also filters the jobs so that only freelance development jobs are shown, rather than full time jobs, or jobs for designers.</p>
<p><strong>Suggestions?</strong></p>
<p>Are there any freelance development sites you use that aren&#8217;t listed here? Please leave a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/top-10-freelance-developer-job-sites/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>PHP Database Query Logging with PDO</title>
		<link>http://www.coderholic.com/php-database-query-logging-with-pdo/</link>
		<comments>http://www.coderholic.com/php-database-query-logging-with-pdo/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 22:47:38 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=164</guid>
		<description><![CDATA[One thing I really like about CakePHP is that when it&#8217;s in debug mode it prints out a list of all the database queries used to generate a page, along with the time it took to run each query. This makes it really easy to find problem queries that are slowing down your site. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>One thing I really like about <a href="http://cakephp.org/">CakePHP</a> is that when it&#8217;s in debug mode it prints out a list of all the database queries used to generate a page, along with the time it took to run each query. This makes it really easy to find problem queries that are slowing down your site. It&#8217;s such a great feature that I wanted something similar for projects that don&#8217;t use CakePHP. </p>
<p>What I came up with was a LoggedPDO class that extends PHP&#8217;s <a href="http://uk.php.net/pdo">PDO</a> class. If you&#8217;re already using PDO in your project you only need to change a single line of code to enable database query logging:</p>
<pre name="code" class="php">
// Standard PDO...
$db = new PDO($dsn, $username, $password);
// ...should be changed to
$db = new LoggedPDO($dsn, $username, $password);
</pre>
<p>And then all your database queries will be logged, timed and all information printed out at the end of each page. My code also includes a decorator for PDOStatements so any prepared statements that get executed also get logged. Below is a small sample of the code in use:</p>
<pre name="code" class="php">
$db = new LoggedPDO("mysql:dbname=test;host=localhost", "root");
$results = $db-&gt;query("SHOW TABLES");
$tables = $results-&gt;fetchAll(PDO::FETCH_COLUMN);
foreach($tables AS $table) {
	$statement = $db-&gt;prepare("DESCRIBE :table");
	$statement-&gt;bindParam(":table", $table, PDO::PARAM_STR);
	$results = $statement-&gt;execute();
	$columns = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
}
</pre>
<p>Which would output:</p>
<table border=1>
<tr>
<th>Query</th>
<th>Time (ms)</th>
</tr>
<tr>
<td>SHOW TABLES</td>
<td>0.174</td>
</tr>
<tr>
<td>[PS] DESCRIBE :table</td>
<td>0.06</td>
</tr>
<tr>
<td>[PS] DESCRIBE :table</td>
<td>0.032</td>
</tr>
<tr>
<th>3 queries</th>
<th>0.266</th>
</tr>
</table>
<p>Full code for the both the LoggedPDO and LoggedPDOStatement classes is below:</p>
<pre name="code" class="php">
/**
* Extends PDO and logs all queries that are executed and how long
* they take, including queries issued via prepared statements
*/
class LoggedPDO extends PDO
{
	public static $log = array();

	public function __construct($dsn, $username = null, $password = null) {
		parent::__construct($dsn, $username, $password);
	}

	/**
	 * Print out the log when we're destructed. I'm assuming this will
	 * be at the end of the page. If not you might want to remove this
	 * destructor and manually call LoggedPDO::printLog();
	 */
	public function __destruct() {
		self::printLog();
	}

	public function query($query) {
		$start = microtime(true);
		$result = parent::query($query);
		$time = microtime(true) - $start;
		LoggedPDO::$log[] = array('query' =&gt; $query,
				                  'time' =&gt; round($time * 1000, 3));
		return $result;
	}

	/**
	 * @return LoggedPDOStatement
	 */
	public function prepare($query) {
		return new LoggedPDOStatement(parent::prepare($query));
	}

	public static function printLog() {
		$totalTime = 0;
		echo '&lt;table border=1&gt;&lt;tr&gt;&lt;th&gt;Query&lt;/th&gt;&lt;th&gt;Time (ms)&lt;/th&gt;&lt;/tr&gt;';
		foreach(self::$log as $entry) {
			$totalTime += $entry['time'];
			echo '&lt;tr&gt;&lt;td&gt;' . $entry['query'] . '&lt;/td&gt;&lt;td&gt;' . $entry['time'] . '&lt;/td&gt;&lt;/tr&gt;\n';
		}
		echo '&lt;tr&gt;&lt;th&gt;' . count(self::$log) . ' queries&lt;/th&gt;&lt;th&gt;' . $totalTime . '&lt;/th&gt;&lt;/tr&gt;\n';
		echo '&lt;/table&gt;';
	}
}

/**
* PDOStatement decorator that logs when a PDOStatement is
* executed, and the time it took to run
* @see LoggedPDO
*/
class LoggedPDOStatement {
	/**
	 * The PDOStatement we decorate
	 */
	private $statement;

	public function __construct(PDOStatement $statement) {
		$this-&gt;statement = $statement;
	}

	/**
	* When execute is called record the time it takes and
	* then log the query
	* @return PDO result set
	*/
	public function execute() {
		$start = microtime(true);
		$result = $this-&gt;statement-&gt;execute();
		$time = microtime(true) - $start;
		LoggedPDO::$log[] = array('query' =&gt; '[PS] ' . $this-&gt;statement-&gt;queryString,
				                  'time' =&gt; round($time * 1000, 3));
		return $result;
	}
	/**
	* Other than execute pass all other calls to the PDOStatement object
	* @param string $function_name
	* @param array $parameters arguments
	*/
	public function __call($function_name, $parameters) {
		return call_user_func_array(array($this-&gt;statement, $function_name), $parameters);
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/php-database-query-logging-with-pdo/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>jQuery Draggable Implementation</title>
		<link>http://www.coderholic.com/jquery-draggable-implementation/</link>
		<comments>http://www.coderholic.com/jquery-draggable-implementation/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 00:09:22 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=158</guid>
		<description><![CDATA[jQuery UI is an excellent add-on library for jQuery that provides all sorts of UI widgets, effects and behaviours. One particularly useful function is provides is to make any element on the page draggable, so that it can be moved about with the mouse. Because of all the features that jQuery UI provides the library [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jqueryui.com">jQuery UI</a> is an excellent add-on library for jQuery that provides all sorts of UI widgets, effects and behaviours. One particularly useful function is provides is to make any element on the page <a href="http://docs.jquery.com/UI/Draggables">draggable</a>, so that it can be moved about with the mouse.</p>
<p>Because of all the features that jQuery UI provides the library is fairly large, about 450KB uncompressed. It is possible to only include specific parts of the library, but even the bare minimum required for the draggables feature weighs in at just over 40KB uncompressed. </p>
<p>Therefore I decided to write my own lightweight draggable implementation using only jQuery. The code is shown below:</p>
<pre name="code" class="javascript">
// Make an element draggable using jQuery
var makeDraggable = function(element) {
	element = jQuery(element);

	// Move the element by the amount of change in the mouse position
	var move = function(event) {
		if(element.data('mouseMove')) {
			var changeX = event.clientX - element.data('mouseX');
			var changeY = event.clientY - element.data('mouseY');

			var newX = parseInt(element.css('left')) + changeX;
			var newY = parseInt(element.css('top')) + changeY;

			element.css('left', newX);
			element.css('top', newY);

			element.data('mouseX', event.clientX);
			element.data('mouseY', event.clientY);
		}
	}

	element.mousedown(function(event) {
		element.data('mouseMove', true);
		element.data('mouseX', event.clientX);
		element.data('mouseY', event.clientY);
	});

	element.parents(':last').mouseup(function() {
		element.data('mouseMove', false);
	});

	element.mouseout(move);
	element.mousemove(move);
}
</pre>
<p>The element you pass to makeDraggable should already have absolute or fixed CSS positioning, and a top and left CSS value set. Something like the following:</p>
<pre name="code" class="html">
&lt;div id='draggable' style='position: absolute; top: 100; left: 200; padding: 50px; background: black; border: 2px solid #aaa;'&gt;
This box is draggable!
&lt;/div&gt;
&lt;script type="text/javascript"&gt;
makeDraggable(jQuery('#draggable'));
&lt;/script&gt;
</pre>
<p>Then just click on the div and move the mouse to drag it around. </p>
<p>Obviously it isn&#8217;t as flexible as the jQuery UI version, but it is a useful alternative if you want to save on the amount of data your user&#8217;s will need to download and you don&#8217;t require any other jQuery UI features. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/jquery-draggable-implementation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ubiquity CmdUtils.injectHTML workaround</title>
		<link>http://www.coderholic.com/ubiquity-cmdutilsinjecthtml-work-around/</link>
		<comments>http://www.coderholic.com/ubiquity-cmdutilsinjecthtml-work-around/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 01:19:24 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=137</guid>
		<description><![CDATA[If you haven&#8217;t already heard about Mozilla Ubiquity I suggest you watch the introductory video, which will explain it far better than I can. I&#8217;ve been having a bit of a play around with it, and written a few small commands. The API documentation is really good, but this being early alpha software (currently v0.1.5) [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t already heard about <a href="http://labs.mozilla.com/projects/ubiquity/">Mozilla Ubiquity</a> I suggest you watch the <a href="http://labs.mozilla.com/2008/08/introducing-ubiquity/">introductory video</a>, which will explain it far better than I can.</p>
<p>I&#8217;ve been having a bit of a play around with it, and written a few small commands. The <a href="file:///home/bmd/.mozilla/firefox/2bq2tiac.default/extensions/ubiquity@labs.mozilla.com/index.html#feed-parts/header/cmdutils.js">API documentation</a> is really good, but this being early alpha software (currently v0.1.5) not everything works as expected. </p>
<p>For one of my commands I wanted to inject some HTML into the current webpage. Looking through the docs CmdUtils.injectHTML seemed to be exactly what I was after. Unfortunately it doesn&#8217;t work, but the workaround is simple enough:</p>
<pre name="code" class="javascript">
var html = 'content to inject';
var doc = CmdUtils.getDocument();
doc.body.innerHTML += html;
</pre>
<p>Hopefully this will be fixed in the next release.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/ubiquity-cmdutilsinjecthtml-work-around/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling Contact Form Failure</title>
		<link>http://www.coderholic.com/handling-contact-form-failure/</link>
		<comments>http://www.coderholic.com/handling-contact-form-failure/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 15:53:40 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=104</guid>
		<description><![CDATA[There is an article over a DZone today &#8220;What if your contact form fails?&#8221;. The article discusses what to do if the contact form on your website fails to send a message. The approach they suggest is to display a message to the user saying it failed, and presenting them with a mailto link so [...]]]></description>
			<content:encoded><![CDATA[<p>There is an article over a DZone today <a href="http://css.dzone.com/news/what-if-your-contact-form-fail">&#8220;What if your contact form fails?&#8221;</a>. The article discusses what to do if the contact form on your website fails to send a message. The approach they suggest is to display a message to the user saying it failed, and presenting them with a mailto link so they can send you an email themselves.</p>
<p>This is one approach to the failed contact form problem, but it isn&#8217;t going to help much if the problem is actually with your email address. For example, you may have exceeded your disk quota, or you might have a badly configured mail server. </p>
<p>Another problem with the mail solution is that it requires the user to perform another action. They&#8217;ve gone to the trouble of filling out a contact form. If that fails they might not bother to send to send you an email too (although the suggested approach in the article does reduce the effort required).</p>
<p><strong>An Alternative Approach</strong></p>
<p>So what other approaches are there? What I do is store all contact form information in a database. Below is the SQL to create the basic messages table:</p>
<pre name="code" class="sql">
CREATE TABLE messages (
    id int auto_increment NOT NULL,
    sent timestamp NOT NULL,
    sender varchar(255) NOT NULL,
    message text,

    PRIMARY KEY(id)
);
</pre>
<p>Then when a contact form gets submitted you can store the information in the database before sending an email. Below is some sample PHP code, using PDO to access the database:</p>
<pre name="code" class="php">
// Get the form data
$sender = $_POST['sender'];
$message = $_POST['message'];

// Add the message to the database
$query = "INSERT INTO messages(sender, message) VALUES (:sender, :message);
$stmt = $pdo->prepare($query);
$stmt->bindParam(':sender', $sender, PDO::PARAM_STR);
$stmt->bindParam(':message', $message, PDO::PARAM_STR);
$stmt->execute();

// Send the mail
mail("me@my.domain", "Contact Form Message", $message, "From: {$sender}");
</pre>
<p>All that is left to do is create an admin page that lists all messages, and allows us to delete them. We could even include a link to the admin page in the emails we send to ourselves. By using this approach we never lose the message content, even if the mail call fails, because they all get stored in the database. All we need to do is check the admin page every so often and we&#8217;ll soon see if there are any new messages there.</p>
<p>Of course, this approach doesn&#8217;t have to be used in isolation. We can combine it with the approach suggested in the DZone article, and present the user with a mail link if sending the mail does fail. If the user decides not to send a mail though, we haven&#8217;t lost anything.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/handling-contact-form-failure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firefox 4k XML node limit</title>
		<link>http://www.coderholic.com/firefox-4k-xml-node-limit/</link>
		<comments>http://www.coderholic.com/firefox-4k-xml-node-limit/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 21:12:07 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[web dev]]></category>

		<guid isPermaLink="false">http://www.coderholic.com/?p=63</guid>
		<description><![CDATA[Did you know that Firefox has a 4096 character limit for XML nodes? I didn&#8217;t until today, and it took me quite a while to track it down! Here is the scenario: You make an AJAX request for some data, you then navigate the DOM of the returned XML data and extract the data you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know that Firefox has a 4096 character limit for XML nodes? I didn&#8217;t until today, and it took me quite a while to track it down! Here is the scenario: You make an AJAX request for some data, you then navigate the DOM of the returned XML data and extract the data you&#8217;re after. Something like:</p>
<pre name="code" class="javascript">
function ajaxResponseHander(xml)
{
    var dataNode = xml.firstChild;
    var data = dataNode.firstChild.nodeValue;
    // now do something with the extracted data...
}
</pre>
<p>This works fine in IE, Safari, and Opera. It&#8217;ll even work fine in Firefox. That is until your data exceeds 4k. At that point you&#8217;ll only get the first 4096 characters. This is because Firefox splits any node that exceeds 4k into multiple nodes. Say, for example, you have a node with 6316 characters then you will end up having a node with the following properties:</p>
<pre name="code" class="javascript">
dataNode.childNodes.length == 2;
dataNode.childNodes[0].length == 4096;
dataNode.childNodes[1].length == 2220;
</pre>
<p>Fortunately Firefox provides the textContent attribute, so we can just use:</p>
<pre name="code" class="javascript">
var data = dataNode.textContent; // data.length == 6316
</pre>
<p>Although because IE doesn&#8217;t support the textContent attribute, we&#8217;re better off writing a function:</p>
<pre name="code" class="javascript">
function getNodeText(xmlNode)
{
    if(!xmlNode) return '';
    if(typeof(xmlNode.textContent) != "undefined") return xmlNode.textContent;
    return xmlNode.firstChild.nodeValue;
}
</pre>
<p>I hope I&#8217;ve saved someone some pain!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coderholic.com/firefox-4k-xml-node-limit/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
