<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Christopher Hill</title>
	
	<link>http://www.chrisjhill.co.uk</link>
	<description>My personal site</description>
	<lastBuildDate>Mon, 13 Aug 2012 20:15:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/chrisjhill" /><feedburner:info uri="chrisjhill" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>jQuery plugin: JustGage</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/RJk6-vzn6qY/jquery-plugin-justgage</link>
		<comments>http://www.chrisjhill.co.uk/article/jquery-plugin-justgage#comments</comments>
		<pubDate>Mon, 13 Aug 2012 20:14:18 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1070</guid>
		<description><![CDATA[JustGage is a handy JavaScript plugin for generating and animating nice &#038; clean gauges. It is based on Raphaël library for vector drawing, so it’s completely resolution independant and self-adjusting.]]></description>
			<content:encoded><![CDATA[<blockquote><p>JustGage is a handy JavaScript plugin for generating and animating nice &#038; clean gauges. It is based on Raphaël library for vector drawing, so it’s completely resolution independant and self-adjusting.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/RJk6-vzn6qY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/jquery-plugin-justgage/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/jquery-plugin-justgage</feedburner:origLink></item>
		<item>
		<title>Javascript: URI.js</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/DPrEQEq03wk/javascript-uri-js</link>
		<comments>http://www.chrisjhill.co.uk/article/javascript-uri-js#comments</comments>
		<pubDate>Tue, 24 Jul 2012 08:54:59 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1067</guid>
		<description><![CDATA[URI.js is a javascript library for working with URLs. It offers a &#8220;jQuery-style&#8221; API (Fluent Interface, Method Chaining) to read and write all regular components and a number of convenience methods like .directory() and .authority().]]></description>
			<content:encoded><![CDATA[<blockquote><p>URI.js is a javascript library for working with URLs. It offers a &#8220;jQuery-style&#8221; API (Fluent Interface, Method Chaining) to read and write all regular components and a number of convenience methods like .directory() and .authority().</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/DPrEQEq03wk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/javascript-uri-js/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/javascript-uri-js</feedburner:origLink></item>
		<item>
		<title>HTML5 mockup site: Moqups</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/DhiQHpttU8c/html5-mockup-site-moqups</link>
		<comments>http://www.chrisjhill.co.uk/article/html5-mockup-site-moqups#comments</comments>
		<pubDate>Thu, 12 Jul 2012 08:02:36 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1064</guid>
		<description><![CDATA[There are many alternatives (Balsamiq and Go Mockingbird, to name a few) and haven&#8217;t delved too deeply, but looks like a solid (free, for now) alternative.]]></description>
			<content:encoded><![CDATA[<p>There are many alternatives (Balsamiq and Go Mockingbird, to name a few) and haven&#8217;t delved too deeply, but looks like a solid (free, for now) alternative.</p>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/DhiQHpttU8c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/html5-mockup-site-moqups/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/html5-mockup-site-moqups</feedburner:origLink></item>
		<item>
		<title>An introduction to PDO: The basics and benefits</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/Mr2KnHZZ0PQ/an-introduction-to-pdo-the-basics-and-benefits</link>
		<comments>http://www.chrisjhill.co.uk/article/an-introduction-to-pdo-the-basics-and-benefits#comments</comments>
		<pubDate>Wed, 11 Jul 2012 12:54:14 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1051</guid>
		<description><![CDATA[MySQL is/was deeply integrated within PHP. No doubt you have seen and used the mysql_*() functions at some stage, but there is an alternative that makes your database interactions much more reliable. It&#8217;s called PDO (PHP Data Object) and here&#8217;s a quick guide that should get you up to speed in no time at all. [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL is/was deeply integrated within PHP. No doubt you have seen and used the <tt>mysql_*()</tt> functions at some stage, but there is an alternative that makes your database interactions much more reliable. It&#8217;s called PDO (PHP Data Object) and here&#8217;s a quick guide that should get you up to speed in no time at all.</p>
<h2>Why use PDO?</h2>
<p>The number one reason is security. With plain MySQL functions you need to sanitise the input manually using type casting or <tt>mysql_real_escape_string()</tt>. With PDO this is all taken care for us and means we no longer have to worry about SQL injection. Great.</p>
<p>The ability to easily migrate to a different database engine (SQLite, Oracle, MSSQL, ODBC, etc.) is the cherry on top of the cake.</p>
<h2>How to connect to a database</h2>
<p>Connecting is just as easy as in standard MySQL functions, it just looks slightly different. We wrap the connection attempt in a try-catch statement so we can gracefully handle errors. E.g.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mysql:host=localhost;dbname=my_db_table&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch<span style="color: #009900;">&#40;</span>PDOException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>One thing I always do is instantly tell PDO I want associative arrays returned. I prefer them to objects, but your milage may vary. I also say that we want to deal exclusively in UTF8.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Connection string, UTF-8 encoding, and fetch associative arrays</span>
<span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PDO<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mysql:host=localhost;dbname=my_db_table;charset=utf8&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>PDO<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_DEFAULT_FETCH_MODE</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_ASSOC</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Inserting rows</h2>
<p>Finally, some database interaction which lets us use the benefit of PDO &#8211; prepared statements. Essentially we insert placeholders into our SQL and define a value for those outside the statement. We do not, I repeat, we do not ever place user input directly into the SQL. This is susceptible to SQL injection.</p>
<p>Secondly, there are two ways to define these placeholders in your SQL statement: Positional and Named. You only ever want to use Named placeholders. Positional are, quite frankly, a silly idea. Positional placeholders ultimately will end up in inserting data in the wrong columns, or selecting data using an incorrect parameter. I&#8217;m only going to show you the correct way (in my opinion) so as to not give you bad habits.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Define the SQL with named placeholders</span>
<span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO `table` (`id`, `title`) VALUES (:id, :title)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Execute the query, replacing the placeholders with their true value</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">':id'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">':title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'I am the title'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can see that we create a variable <tt>$sth</tt> (statement handler) which calls the <tt>$dbh</tt> (database handler) prepare method. The SQL should look pretty standard until we reach the <tt>:id</tt> and <tt>:title</tt>. These are our named placeholders. We can run this query by calling the <tt>execute()</tt> function which takes an array of the placeholder data. Quite simple.</p>
<p>The benefits of this are that our data is automatically escaped &#8211; we do not have to worry about calling <tt>mysql_real_escape_string()</tt>, after all we might not be using MySQL. Also, the benefits are that we can call the <tt>execute()</tt> functions many times without re-declaring the SQL statement, which will speed up our code execution.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Insert three rows into the table</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">':title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'First title'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">456</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">':title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Second title'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">789</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">':title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Third title'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Selecting</h2>
<p>Now that you have just mastered inserting data, selecting is a piece of cake, it&#8217;s very similar, and also gives us a chance to go over a few new functions.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Select some data</span>
<span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT `id`, `title` FROM `table` WHERE `id` &gt; :country_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Execute the query, replacing the placeholders with their true value</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">':country_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">200</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// How many records did we find?</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'We found '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rowCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' records.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Loop over the data</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'My ID is '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' and my title is '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><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>To get the number of rows returned you can use the <tt>rowCount()</tt> function (akin to the <tt>mysql_num_rows()</tt>), and to loop over the data you can use the <tt>fetch()</tt> function (akin to the <tt>mysql_fetch_assoc()</tt>). If you wanted to return all of the rows into a variable without performing a loop you can do <tt>fetchAll()</tt>. Otherwise there are no gotcha&#8217;s here.</p>
<h2>Updating data</h2>
<p>You&#8217;re going to be bored of me saying this, but there is nothing different here than the previous examples:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Update a row</span>
<span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE `table` SET `title` = :title WHERE `id` = :id LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Execute the query, replacing the placeholders with their true value</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">':title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'I am the newly updated title'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">':id'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Did we manage to update the row?</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rowCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yup, the row was updated.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Nope, row was not updated.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Deleting data</h2>
<p>Again, nothing too complex:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Delete a row</span>
<span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM `table` WHERE `id` = :id LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Execute the query, replacing the placeholders with their true value</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">':id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Did we manage to delete the row?</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rowCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yup, the row was deleted.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Nope, row was not deleted.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><tt>bindParam()</tt>, <tt>bindValue()</tt>, and <tt>execute()</tt></h2>
<p>This comes down to preference. I prefer the <tt>execute()</tt> because we are avoiding function calls, speeding up our code, and easier to update. But I&#8217;ll go over all three.</p>
<p><tt>bindParam()</tt> is asking for a reference. It is not asking for an immediate value, it only receives the value once the execute() function is called. This is probably the most confusing aspect of PDO, so let me give you an example.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Select some data</span>
<span style="color: #000088;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT `id`, `title` FROM `table` WHERE `id` &gt; :country_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set binds</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tableId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Define variables</span>
<span style="color: #000088;">$tableId</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">123</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Execute</span>
<span style="color: #000088;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This works because, although <tt>$tableId</tt> does not exist when referenced, it is defined before our <tt>execute()</tt> call. The reason I do not like this is because it is leaving an uncertainty to the statement. You could quite easily bind the parameter, then change the variable for some reason and cause an error. You also have the problem that literals can not be used since they cannot be referenced. So, <tt>bindParam(':id', 123)</tt> would result in an error.</p>
<p><tt>bindValue()</tt> to the rescue! This works in the same way as <tt>bindParam()</tt>, but it uses the value straight away, be it a literal or a variable.</p>
<p><tt>execute()</tt> is what we have been using in the previous code samples, so does not need an introduction.</p>
<h2><tt>exec()</tt>, <tt>execute()</tt>, and <tt>query()</tt></h2>
<p>These all do the same thing, run a query. But they do so in different ways. In my opinion you should only ever use <tt>execute()</tt> as it is the only one which works with prepared statements which, in time, the other two can become susceptible to SQL injection. They also have their nuances, <tt>exec()</tt> can only be used for queries that do not return data objects (e.g., SELECT statements), and generally speaking <tt>query()</tt> is only used on SELECT statements. But since you cannot use prepared statements (e.g., placeholders), I wouldn&#8217;t bother with them.</p>
<h2>Wrapup</h2>
<p>You now know the basics of PDO. You can see it isn&#8217;t a scary monster and it really is easy to switch. You now know how to insert, select, update, and delete rows. You know how to safely pass in parameters using placeholders (remember, named not positional!) and so you now never have to worry about SQL injection.</p>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/Mr2KnHZZ0PQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/an-introduction-to-pdo-the-basics-and-benefits/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/an-introduction-to-pdo-the-basics-and-benefits</feedburner:origLink></item>
		<item>
		<title>jQuery plugin: Handsontable</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/J4E6lPq2w9o/jquery-plugin-handsontable</link>
		<comments>http://www.chrisjhill.co.uk/article/jquery-plugin-handsontable#comments</comments>
		<pubDate>Tue, 10 Jul 2012 20:57:01 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1048</guid>
		<description><![CDATA[Handsontable is a minimalistic approach to Excel-like table editor in HTML &#038; jQuery. Now with column and row headers!]]></description>
			<content:encoded><![CDATA[<blockquote><p>Handsontable is a minimalistic approach to Excel-like table editor in HTML &#038; jQuery. Now with column and row headers!</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/J4E6lPq2w9o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/jquery-plugin-handsontable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/jquery-plugin-handsontable</feedburner:origLink></item>
		<item>
		<title>jQuery plugin: Morris.js</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/9B9_dx013eo/jquery-plugin-morris-js</link>
		<comments>http://www.chrisjhill.co.uk/article/jquery-plugin-morris-js#comments</comments>
		<pubDate>Tue, 10 Jul 2012 08:03:28 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1046</guid>
		<description><![CDATA[Making good-looking graphs shouldn&#8217;t be hard. Morris.js is a lightweight library that uses jQuery and Raphaël to make drawing time-series graphs easy.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Making good-looking graphs shouldn&#8217;t be hard. Morris.js is a lightweight library that uses jQuery and Raphaël to make drawing time-series graphs easy.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/9B9_dx013eo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/jquery-plugin-morris-js/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/jquery-plugin-morris-js</feedburner:origLink></item>
		<item>
		<title>jQuery plugin: Turn.js</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/LELCSCMIj-o/jquery-plugin-turn-js</link>
		<comments>http://www.chrisjhill.co.uk/article/jquery-plugin-turn-js#comments</comments>
		<pubDate>Tue, 10 Jul 2012 08:02:49 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1044</guid>
		<description><![CDATA[Turn.js is a JavaScript library that will make your content look like a real book or magazine using all the advantages of HTML5. The web is getting beautiful with new user interfaces based in HTML5; turn.js is the best fit for a magazine, book or catalog based in HTML5.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Turn.js is a JavaScript library that will make your content look like a real book or magazine using all the advantages of HTML5. The web is getting beautiful with new user interfaces based in HTML5; turn.js is the best fit for a magazine, book or catalog based in HTML5.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/LELCSCMIj-o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/jquery-plugin-turn-js/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/jquery-plugin-turn-js</feedburner:origLink></item>
		<item>
		<title>Try Git in your browser</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/3uWO1Fa1tdw/try-git-in-your-browser</link>
		<comments>http://www.chrisjhill.co.uk/article/try-git-in-your-browser#comments</comments>
		<pubDate>Wed, 04 Jul 2012 15:58:36 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1042</guid>
		<description><![CDATA[Git allows groups of people to work on the same documents (often code) at the same time, and without stepping on each other&#8217;s toes. It&#8217;s a distributed version control system.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Git allows groups of people to work on the same documents (often code) at the same time, and without stepping on each other&#8217;s toes. It&#8217;s a distributed version control system.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/3uWO1Fa1tdw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/try-git-in-your-browser/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/try-git-in-your-browser</feedburner:origLink></item>
		<item>
		<title>Brackets: The Open Source Code Editor for the Web</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/GqJ2CdCsVrE/brackets-the-open-source-code-editor-for-the-web</link>
		<comments>http://www.chrisjhill.co.uk/article/brackets-the-open-source-code-editor-for-the-web#comments</comments>
		<pubDate>Sun, 01 Jul 2012 12:39:10 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1036</guid>
		<description><![CDATA[Brackets is a new open source project founded by Adobe to push tooling on the web forward. Based on the principle that the best developers to build tools for web developers are in fact web developers, Brackets is built in JavaScript, HTML and CSS.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Brackets is a new open source project founded by Adobe to push tooling on the web forward. Based on the principle that the best developers to build tools for web developers are in fact web developers, Brackets is built in JavaScript, HTML and CSS.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/GqJ2CdCsVrE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/brackets-the-open-source-code-editor-for-the-web/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/brackets-the-open-source-code-editor-for-the-web</feedburner:origLink></item>
		<item>
		<title>Update: Reversi board game in PHP and Javascript</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/5_9moP9Qauw/reversi-board-game-made-in-php</link>
		<comments>http://www.chrisjhill.co.uk/article/reversi-board-game-made-in-php#comments</comments>
		<pubDate>Fri, 29 Jun 2012 13:00:51 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1017</guid>
		<description><![CDATA[I like playing the occasional game of Reversi, and thought it would prove to be an interesting project (creating something like RedHotPawn for chess). Several hours programming later I had a simple working prototype. A couple more hours later I added in some missing features and smartened everything up. Demonstration and Download To view a [...]]]></description>
			<content:encoded><![CDATA[<p>I like playing the occasional game of <a href="http://en.wikipedia.org/wiki/Reversi">Reversi</a>, and thought it would prove to be an interesting project (creating something like <a href="http://www.redhotpawn.com/">RedHotPawn</a> for chess). Several hours programming later I had a simple working prototype. A couple more hours later I added in some missing features and smartened everything up.</p>
<h2>Demonstration and Download</h2>
<p><a href="http://laboratory.chrisjhill.co.uk/reversi/">To view a demonstration of the code in action, click here.</a></p>
<p><a href="https://github.com/chrisjhill/Reversi">You can view the Github repository here.</a></p>
<h2>The PHP backend</h2>
<p>It is designed to be flexible &#8212; it can handle boards of any size and will place the starting 4 disks in the center. The PHP is also very quick &#8212; the size of the board does not really effect the performance due to the way its programmed. It only allows legal moves to be played, each move needs to flip at least one of your opponents disks, if you play an invalid move it will give you the option to skip your play.</p>
<h2>A quick snippet of the traversing function in the PHP class</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Traverse the board to see if we can take any of our opponants disks.
 *
 * To traverse the board we can add, substract, or do nothing with each X and Y coord.
 * Keep traversing until we reach an empty position, a wall, or our own disk. Once
 * reached an end, traverse back down the coords replacing the disks with our own.
 *
 * @param $xDiff int
 * @param $yDiff int
 * @access private
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doTraverse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xDiff</span><span style="color: #339933;">,</span> <span style="color: #000088;">$yDiff</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Set variables</span>
    <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_x<span style="color: #339933;">;</span>
    <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_y<span style="color: #339933;">;</span>
    <span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Begin the loop</span>
    <span style="color: #b1b100;">do</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Work out the new coords to test</span>
        <span style="color: #000088;">$x</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$xDiff</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$y</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$yDiff</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// What is in the next position?</span>
        <span style="color: #000088;">$next</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_boardContent<span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            ? <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_boardContent<span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span>
            <span style="color: #339933;">:</span> <span style="color: #0000ff;">'e'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Edge</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Have we hit an edge or an empty position?</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$next</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'e'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$next</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Have we reached our own disk colour?</span>
        <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$next</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_turnInPlay<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// We are currently at our own disk, move back one so we are at our</span>
            <span style="color: #666666; font-style: italic;">// .. last free (potentially) disk.</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xDiff</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">--;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xDiff</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$yDiff</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">--;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$yDiff</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Are we where we started?</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_x <span style="color: #339933;">||</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Change this disk to the player who just moved</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_boardContent<span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_turnInPlay<span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// Set the number of disks this flipped</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_disksFlipped<span style="color: #339933;">++;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// Move back one coord to begin another replacement</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xDiff</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">--;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xDiff</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$yDiff</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">--;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$yDiff</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// We have converted all of the possible disks, exit the traverse</span>
            <span style="color: #000088;">$continue</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$continue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>The Javascript frontend</h2>
<p>Javascript provides a real-time view of what disks will be flipped. The traversing function is a virtual copy of the PHP classes.</p>
<h2>Functionality to be built</h2>
<p>One piece of functionality that is really needed is ensuring a legal move is actually possible. If it isn&#8217;t then it automatically skips to the next player, and if that player cannot play a legal move either then it ends the game. I&#8217;m not sure if it will be best to implement this in either PHP or Javascript &#8212; both options have their benefits.</p>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/5_9moP9Qauw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/reversi-board-game-made-in-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/reversi-board-game-made-in-php</feedburner:origLink></item>
		<item>
		<title>jQuery version 1.9 and beyond</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/fOX-WWCPuno/jquery-version-1-9-and-beyond</link>
		<comments>http://www.chrisjhill.co.uk/article/jquery-version-1-9-and-beyond#comments</comments>
		<pubDate>Fri, 29 Jun 2012 10:29:02 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1015</guid>
		<description><![CDATA[Seems as though the jQuery team are planning on dropping support for IE 6/7/8 in jQuery 2.0, expected to be released in early 2013. Now, although I don&#8217;t particularly like IE, I think that might be a little too aggressive a development path. Unfortunately few people still use IE 6, a little more 7, and [...]]]></description>
			<content:encoded><![CDATA[<p>Seems as though the jQuery team are planning on dropping support for IE 6/7/8 in jQuery 2.0, expected to be released in early 2013. Now, although I don&#8217;t particularly like IE, I think that might be a little too aggressive a development path. Unfortunately few people still use IE 6, a little more 7, and quite a chunk 8.</p>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/fOX-WWCPuno" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/jquery-version-1-9-and-beyond/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/jquery-version-1-9-and-beyond</feedburner:origLink></item>
		<item>
		<title>Node 0.8.0 released</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/aG_WZ39SYmk/node-0-8-0-released</link>
		<comments>http://www.chrisjhill.co.uk/article/node-0-8-0-released#comments</comments>
		<pubDate>Mon, 25 Jun 2012 21:00:53 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1013</guid>
		<description><![CDATA[I am thrilled to announce the arrival of a new stable version of Node.js. Compared with the v0.6 releases of Node, this release brings significant improvements in many key performance metrics, as well as cleanup in several core APIs,]]></description>
			<content:encoded><![CDATA[<blockquote><p>I am thrilled to announce the arrival of a new stable version of Node.js. Compared with the v0.6 releases of Node, this release brings significant improvements in many key performance metrics, as well as cleanup in several core APIs,</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/aG_WZ39SYmk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/node-0-8-0-released/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/node-0-8-0-released</feedburner:origLink></item>
		<item>
		<title>‘Twisted light’ carries 2.5 terabits of data per second</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/XJ04GAdA4dA/twisted-light-carries-2-5-terabits-of-data-per-second</link>
		<comments>http://www.chrisjhill.co.uk/article/twisted-light-carries-2-5-terabits-of-data-per-second#comments</comments>
		<pubDate>Mon, 25 Jun 2012 15:18:44 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=1011</guid>
		<description><![CDATA[Researchers have clocked light beams made of &#8220;twisted&#8221; waves carrying 2.5 terabits of data &#8211; the capacity of more than 66 DVDs &#8211; per second.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Researchers have clocked light beams made of &#8220;twisted&#8221; waves carrying 2.5 terabits of data &#8211; the capacity of more than 66 DVDs &#8211; per second.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/XJ04GAdA4dA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/twisted-light-carries-2-5-terabits-of-data-per-second/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/twisted-light-carries-2-5-terabits-of-data-per-second</feedburner:origLink></item>
		<item>
		<title>PHP sockets with Ratchet</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/IsvJfvAYZEk/php-sockets-with-ratchet</link>
		<comments>http://www.chrisjhill.co.uk/article/php-sockets-with-ratchet#comments</comments>
		<pubDate>Fri, 22 Jun 2012 08:14:50 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=991</guid>
		<description><![CDATA[Ratchet is a component library for PHP that provides developers with the tools to create real time, bi-directional applications between clients and servers. This is not your Grandfather&#8217;s Internet.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Ratchet is a component library for PHP that provides developers with the tools to create real time, bi-directional applications between clients and servers. This is not your Grandfather&#8217;s Internet.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/IsvJfvAYZEk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/php-sockets-with-ratchet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/php-sockets-with-ratchet</feedburner:origLink></item>
		<item>
		<title>reFiddle: Helping to debug regular expressions</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/pvlImhZGaWA/refiddle-helping-to-debug-regular-expressions</link>
		<comments>http://www.chrisjhill.co.uk/article/refiddle-helping-to-debug-regular-expressions#comments</comments>
		<pubDate>Thu, 21 Jun 2012 21:22:35 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=983</guid>
		<description><![CDATA[Test your regular expressions with live matching and custom string matching.]]></description>
			<content:encoded><![CDATA[<p>Test your regular expressions with live matching and custom string matching.</p>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/pvlImhZGaWA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/refiddle-helping-to-debug-regular-expressions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/refiddle-helping-to-debug-regular-expressions</feedburner:origLink></item>
		<item>
		<title>Google Maps adds UK waterways to travel routes</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/vjIZ4Zd9owg/google-maps-adds-uk-waterways-to-travel-routes</link>
		<comments>http://www.chrisjhill.co.uk/article/google-maps-adds-uk-waterways-to-travel-routes#comments</comments>
		<pubDate>Thu, 21 Jun 2012 15:48:50 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=979</guid>
		<description><![CDATA[Google Maps users may soon find it easier to plan a trip on some of England and Wales&#8217;s waterways. The search engine has teamed up with the Canal and River Trust, a charity that will be in charge of the two countries&#8217; water network from July.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Google Maps users may soon find it easier to plan a trip on some of England and Wales&#8217;s waterways. The search engine has teamed up with the Canal and River Trust, a charity that will be in charge of the two countries&#8217; water network from July.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/vjIZ4Zd9owg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/google-maps-adds-uk-waterways-to-travel-routes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/google-maps-adds-uk-waterways-to-travel-routes</feedburner:origLink></item>
		<item>
		<title>jQuery plugin: Swiping Blind Effect</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/8bXAd2867U8/jquery-plugin-swiping-blind-effect</link>
		<comments>http://www.chrisjhill.co.uk/article/jquery-plugin-swiping-blind-effect#comments</comments>
		<pubDate>Thu, 21 Jun 2012 13:44:54 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=963</guid>
		<description><![CDATA[This plugin stemmed from a need to have an effect similar to that of a blind, how it displays the content beneath slowly from left to right in strips. Demonstration To view a demonstration of this plugin, click here. What does it look like? How to use the plugin You use it the same as [...]]]></description>
			<content:encoded><![CDATA[<p>This plugin stemmed from a need to have an effect similar to that of a blind, how it displays the content beneath slowly from left to right in strips.</p>
<h2>Demonstration</h2>
<p><a href="http://laboratory.chrisjhill.co.uk/jquery-blindeffect/">To view a demonstration of this plugin, click here.</a></p>
<h2>What does it look like?</h2>
<p><img src="/public/uploads/2012/06/blindeffect-example.png" alt="" /></p>
<h2>How to use the plugin</h2>
<p>You use it the same as any other plugin. Simply make sure the DOM has fully loaded, then call the function on an element ID or class:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.element&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">blindEffect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        speed<span style="color: #339933;">:</span>  <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Each panel will animate for 1 second</span>
        panels<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span>    <span style="color: #006600; font-style: italic;">// Split the element into 10 different panels</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>The plugin code</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">blindEffect</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Define the default settings</span>
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            speed<span style="color: #339933;">:</span>           <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span>   <span style="color: #006600; font-style: italic;">// How fast the animation will last for each panel</span>
            panels<span style="color: #339933;">:</span>          <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>     <span style="color: #006600; font-style: italic;">// The number of panels to animate</span>
            backgroundColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#FFF&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// What background colour should the panels have?</span>
            leftToRight<span style="color: #339933;">:</span>     <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>   <span style="color: #006600; font-style: italic;">// Animate from the right to the left?</span>
            delay<span style="color: #339933;">:</span>           <span style="color: #CC0000;">75</span><span style="color: #339933;">,</span>     <span style="color: #006600; font-style: italic;">// How long to delay the animation between each panel</span>
            opacity<span style="color: #339933;">:</span>         <span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #339933;">,</span>    <span style="color: #006600; font-style: italic;">// Also animate the opacity of the panels?</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Merge the users options in with the settings</span>
        <span style="color: #006600; font-style: italic;">// We can now refer to these options in our function via options.variable</span>
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Run through each of the found elements</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// Assign $(this) to e for simplicity and speed</span>
            <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Is this element already in animation?</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.panel&quot;</span><span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// What is the height of this container?</span>
            <span style="color: #003366; font-weight: bold;">var</span> containerHeight <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// How wide will each panel need to be?</span>
            <span style="color: #003366; font-weight: bold;">var</span> panelWidth <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;width&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> options.<span style="color: #660066;">panels</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Start placing the panels over the image</span>
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</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> options.<span style="color: #660066;">panels</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// Prepend the panel</span>
                e.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;panel&quot;'</span> <span style="color: #339933;">+</span>
                    <span style="color: #3366CC;">' style=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">leftToRight</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'right'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'left'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">':'</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> panelWidth<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px;'</span> <span style="color: #339933;">+</span>
                        <span style="color: #3366CC;">'width:'</span> <span style="color: #339933;">+</span> panelWidth <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px;'</span> <span style="color: #339933;">+</span>
                        <span style="color: #3366CC;">'height:'</span> <span style="color: #339933;">+</span> containerHeight <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px;'</span> <span style="color: #339933;">+</span>
                        <span style="color: #3366CC;">'background:'</span> <span style="color: #339933;">+</span> options.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;&amp;nbsp;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Now that the panels are visible we start to hide the panels</span>
            <span style="color: #006600; font-style: italic;">// Do we want right to left, or left to right?</span>
            <span style="color: #003366; font-weight: bold;">var</span> marginLeft <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">leftToRight</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                marginLeft <span style="color: #339933;">=</span> panelWidth <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Loop over each panel</span>
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</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> options.<span style="color: #660066;">panels</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.panel:eq(&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">panels</span> <span style="color: #339933;">-</span> i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span>
                    <span style="color: #006600; font-style: italic;">// Add a delay so the animation is nice</span>
                    .<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> options.<span style="color: #660066;">delay</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #006600; font-style: italic;">// Animate the width and opacity</span>
                    <span style="color: #006600; font-style: italic;">// Also animate marginLeft so left to right looks nice</span>
                    .<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                        width<span style="color: #339933;">:</span>      <span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #339933;">,</span>
                        opacity<span style="color: #339933;">:</span>    options.<span style="color: #660066;">opacity</span><span style="color: #339933;">,</span>
                        marginLeft<span style="color: #339933;">:</span> marginLeft
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #006600; font-style: italic;">// How fast we need to animate each panel</span>
                    options.<span style="color: #660066;">speed</span><span style="color: #339933;">,</span>
                    <span style="color: #006600; font-style: italic;">// Remove the panel after the animation, it serves no use</span>
                    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Return jQuery object</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/8bXAd2867U8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/jquery-plugin-swiping-blind-effect/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/jquery-plugin-swiping-blind-effect</feedburner:origLink></item>
		<item>
		<title>jQuery plugin: Joyride</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/-jALrxSPPR0/jquery-plugin-joyride</link>
		<comments>http://www.chrisjhill.co.uk/article/jquery-plugin-joyride#comments</comments>
		<pubDate>Wed, 20 Jun 2012 15:10:33 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=961</guid>
		<description><![CDATA[We have been meaning to add this kind of functionality to Signable for a while. Will have to add it to the ever-growing todo list.]]></description>
			<content:encoded><![CDATA[<p>We have been meaning to add this kind of functionality to Signable for a while. Will have to add it to the ever-growing todo list.</p>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/-jALrxSPPR0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/jquery-plugin-joyride/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/jquery-plugin-joyride</feedburner:origLink></item>
		<item>
		<title>StackOverflow challenge: Replicate (almost) HTML text-align justify</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/AyexVHuKagE/stackoverflow-challenge-replicate-almost-html-text-align-justify</link>
		<comments>http://www.chrisjhill.co.uk/article/stackoverflow-challenge-replicate-almost-html-text-align-justify#comments</comments>
		<pubDate>Wed, 20 Jun 2012 08:28:58 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=981</guid>
		<description><![CDATA[<p>Quite an interesting little test, I thought. Has zero practical world application, but it does require the person to have a somewhat analytical mind and problem solving skills. Also a reasonable knowledge of PHP. So I thought I would have a go (without looking at others' solutions). 10 minutes later a working script which does exactly what was requested.</p>]]></description>
			<content:encoded><![CDATA[<p>Was browsing through <a href="http://www.reddit.com">/r/php</a> when I stumbled across a post titled &#8220;<a href="http://www.reddit.com/tb/v4bv6">Fun (if you&#8217;re like me) question on stackoverflow</a>&#8220;. It links to a StackOverflow post where someone says:</p>
<blockquote><p>Just tanked a job interview where I was asked to implement a function with this signature: <tt>function justify($str_in, $desired_length)</tt> It needs to mimic what HTML&#8217;s text-align: justify would do, here&#8217;s some examples (desired_length = 48)</p>
</blockquote>
<p>Quite an interesting little test, I thought. Has zero practical world application, but it does require the person to have a somewhat analytical mind and problem solving skills. Also a reasonable knowledge of PHP. So I thought I would have a go (without looking at others&#8217; solutions). 10 minutes later a working script which does exactly what was requested.</p>
<h2>Quick walkthrough</h2>
<p>We start off by exploding the string into separate lines and looping through each one. Start with the simplest first: if the line is already over 48 characters in length then simply truncate the string and replace the spaces with dots. Next we want to explode the words into an array, which allows us to handle the second simplest test case: single words. If there is only one word then simply use <tt>str_pad()</tt>.</p>
<p>Multiple words now. The hardest part (though not very). We need to work out how many dots we need, and how many that should be between each word (don&#8217;t forget to <tt>floor()</tt> it so we have a whole number!). The problem is this can leave fractions, so work out how many extra dots we need. We can then loop over each word and add the dots. If we have any extra dots then we can output just one of them&#8212;there will never be more than 1 extra dot per word as then that would mean the dots per word would increase!</p>
<p>A nice way to spend 10 minutes whilst watching a Euro 2012 game.</p>
<h2>The resulting PHP</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> justify<span style="color: #009900;">&#40;</span><span style="color: #000088;">$strIn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$desiredLength</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// First lets split the lines into an array</span>
    <span style="color: #000088;">$strIn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$strIn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Loop over each line to test</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$strIn</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Strip whitespace from beginning and end</span>
        <span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Is the string longer than 48 characters?</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$desiredLength</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Cut it down to 48 characters and replace the spaces with dots</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">48</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Less than 48 characters</span>
        <span style="color: #666666; font-style: italic;">// Split words into an array</span>
        <span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// How many words are there?</span>
        <span style="color: #000088;">$wordCount</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Are we dealing with only one word?</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$wordCount</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// One word, just str_pad it</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #990000;">str_pad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$desiredLength</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> STR_PAD_BOTH<span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Multiple words</span>
        <span style="color: #000088;">$stringLengthMinusDots</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$dotsRequired</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$desiredLength</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$stringLengthMinusDots</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// How many dots do we need inbetween each word?</span>
        <span style="color: #666666; font-style: italic;">// Note: We want to deduct 1 from $wordCount, we do not want dots after the last word</span>
        <span style="color: #000088;">$inbetween</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dotsRequired</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$inbetweenOver</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$desiredLength</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$stringLengthMinusDots</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inbetween</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$wordCount</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// How many dots have we outputted?</span>
        <span style="color: #000088;">$dotsOutputted</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Loop over the words</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Echo out the word</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$word</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Have we already gone over our allowance?</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$dotsOutputted</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$dotsRequired</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Do we have extra dots that we need to output?</span>
            <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inbetweenOver</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Extra dot, deduct one from our over count, increase the output dot count</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$inbetweenOver</span><span style="color: #339933;">--;</span>
                <span style="color: #000088;">$dotsOutputted</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Echo dots and add to count</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$inbetween</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$dotsOutputted</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$inbetween</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// New line</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// And run the function</span>
justify<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello world there ok then
hello
ok then
this string is almost certainly longer than 48 I Think so needs to be shorter
two words
three ok words
1 2 3 4 5 6 7 8 9'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">48</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<hr />
<p><strong>Update</strong>: Just saw someone on the StackOverflow post performed a benchmark with peoples answers. Run 100,000 times mine clocked in at 2.3 seconds on a 2011 MacBook Pro using <a href="http://www.mamp.info/en/index.html">MAMP</a> (with quite a few applications running!). I imagine this will be faster than the users &#8220;single core Ubuntu VM&#8221;, though, so not too scientific&hellip;</p>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/AyexVHuKagE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/stackoverflow-challenge-replicate-almost-html-text-align-justify/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/stackoverflow-challenge-replicate-almost-html-text-align-justify</feedburner:origLink></item>
		<item>
		<title>Responsive design illustration</title>
		<link>http://feedproxy.google.com/~r/chrisjhill/~3/6UYP9fzfkSE/responsive-design-illustration</link>
		<comments>http://www.chrisjhill.co.uk/article/responsive-design-illustration#comments</comments>
		<pubDate>Tue, 19 Jun 2012 14:54:58 +0000</pubDate>
		<dc:creator>Christopher Hill</dc:creator>
				<category><![CDATA[Fragment]]></category>

		<guid isPermaLink="false">http://www.chrisjhill.co.uk/?p=944</guid>
		<description><![CDATA[Resize your browser to reveal just a handful of the kind of devices you should expect web pages to be viewed on.]]></description>
			<content:encoded><![CDATA[<blockquote><p>Resize your browser to reveal just a handful of the kind of devices you should expect web pages to be viewed on.</p>
</blockquote>
<img src="http://feeds.feedburner.com/~r/chrisjhill/~4/6UYP9fzfkSE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chrisjhill.co.uk/article/responsive-design-illustration/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chrisjhill.co.uk/article/responsive-design-illustration</feedburner:origLink></item>
	</channel>
</rss>
