<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tom&#8217;s Big Box</title>
	<atom:link href="http://tomsbigbox.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://tomsbigbox.com</link>
	<description>Well it&#039;s more like a cuboid really.</description>
	<lastBuildDate>Sat, 12 May 2012 14:03:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Implement the WordPress Geolocation Plugin</title>
		<link>http://tomsbigbox.com/implement-the-wordpress-geolocation-plugin/</link>
		<comments>http://tomsbigbox.com/implement-the-wordpress-geolocation-plugin/#comments</comments>
		<pubDate>Sat, 12 May 2012 14:01:29 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[geo]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[location]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[plot route]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[posts]]></category>
		<category><![CDATA[travel map]]></category>
		<category><![CDATA[v3]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1377</guid>
		<description><![CDATA[For a while now WordPress has had apps across the mobile spectrum, and a great feature of these is geolocation - great, but who wants to know where I'm blogging from right? Well recently I had to build a blog for someone travelling around the world, and it quickly became apparent that using the data saved by the app on the site would be a great feature. In this tutorial...]]></description>
			<content:encoded><![CDATA[<p>For a while now WordPress has had apps across the mobile spectrum, and a great feature of these is geolocation - great, but who wants to know where I'm blogging from right? Well recently I had to build a blog for someone travelling around the world, and it quickly became apparent that using the data saved by the app on the site would be a great feature. In this tutorial we'll see the basic implementation of adding the plugin, but more importantly look at how we can then plot a route across the globe using data from every post. So let's get started!</p>
<p>First off you'll need to go and grab <a href="" target="_blank">the Geolocation plugin</a> and install it on your site. The default behaviour of this plugin is to insert a link at the bottom of single posts and show a map to users when they hover over it. But what if we want to show the map by default? And what about using all that geolocation data from each post? Well the first request is relatively simple - in fact it's just a change of CSS. Here's the alteration to <code class="sh_html">style.css</code> in the goelocation folder:</p>
<pre class="sh_css code-full-width">
#map { background: #fff; border: solid 1px #999; padding: 20px; visibility: hidden; }
.home #map { display: none; }
</pre>
<p>The second rule is useful if you use the <code class="sh_php">the_content()</code> on your home page, this is because only 1 map is actually used for every post meaning that otherwise you'll just have a big empty div. </p>
<h3>Plotting your posts on a map</h3>
<p>Here's the exciting bit, to begin with we'll look at creating a route out of post data, and displaying it on a nice big map. Thankfully the WordPress app doesn't use anything <a href="http://www.youtube.com/watch?v=T5xcVxkTZzM" target="_blank">big, scary, or evil</a> to store its data, it just stores it as a custom variable! So accessing the data is easy as pie! Now in this case I'm assuming that you're whacking the PHP code in something like <code class="sh_html">map.php</code> - a template for a page, and that the JavaScript coming later will be in <code class="sh_html">footer.php</code> - this is important because the JS will be using a PHP variable - namely the coordinates to plot. If you have a different structure, you'll need to find some way of getting this data from PHP, either via an AJAX request, or clever script positioning. Let's take a look at how we'd go about getting all of this data into our PHP backend, before utilising it with the Google Maps JavaScript API:</p>
<pre class="sh_php code-full-width">
&lt;?php
    $points = '';
    query_posts('posts_per_page=500'); 

    while ( have_posts() ) : the_post(); 

        $points .= '(' . get_post_meta($post->ID, 'geo_latitude', true) . ',' . get_post_meta($post->ID, 'geo_longitude', true) . '),'; &nbsp;

    endwhile; 

    $points = substr($points, 1, -2); // Remove the initial '(' and final '),'
?>

&lt;div id="canvas">&lt;/div>
</pre>
<p>So above we have the standard WordPress loop that cycles through the 500 most recent posts (courtesy of <code class="sh_php">query_posts()</code>) - this should all be familiar. Inside the loop we keep adding to the <code class="sh_php">$points</code> variable in the format <code>(latitude_1, longitude_1), (latitude_2, longitude_2)</code> and so on. We use the handy <code class="sh_php">get_post_meta()</code> function (<a href="http://codex.wordpress.org/Function_Reference/get_post_meta" target="_blank">here's the reference</a>), to get the coordinates, and we end by removing the first bracket and last bracket and comma - we do this because when we switch to JS we need a clean array. Finally we have a div with the ID "canvas" - this is where we'll put our map - so feel free to style this in your CSS, I added the following to the theme's <code class="sh_html">style.css</code>:</p>
<pre class="sh_css code-full-width">
#canvas {
    height:600px;
    width: 96%;
    margin-left: 2%;
}
</pre>
<p>And now we're ready to move into the crazy realm of JavaScript!</p>
<p>Luckily for us the Geolocation plugin already takes care of adding the <a href="https://developers.google.com/maps/documentation/javascript/" target="_blank">Google Maps API v3</a>, so we can get straight into using it with our site! But before we get into our JavaScript we're going to be good coders and only deliver our code to users on the map page, so we'll use yet another handy WordPress function to detect when we're on the <code class="sh_html">map.php</code> template page. Go ahead and whack this into <code class="sh_html">footer.php</code>:</p>
<pre class="sh_php code-full-width">
&lt;?php  if ( is_page_template('map.php') ) : ?>

     /* JS goes here... */

&lt;?php endif; ?>
</pre>
<p>Now let's get to the really meaty code. The code below uses 1 jQuery call, and it's just to check that the DOM is fully loaded, so you can use any equivalent function or whack the code into <code class="sh_html">onload=""</code> in the body tag. First up we'll look at getting the data from PHP and making it usable, put the following code within <code class="sh_html">&lt;script></code> tags:</p>
<pre class="sh_javascript code-full-width">
function init(){
    var p = "&lt;?php global $points; echo $points; ?>", mapCoords = null;

    p = p.split("),(");

    for(var x = 0; x < p.length; x++){
        p[x] = p[x].split(",");
        if(mapCoords == null){
            mapCoords = [new google.maps.LatLng(p[x][0], p[x][1])];
        }else {
            mapCoords.push(new google.maps.LatLng(p[x][0], p[x][1]));
        }
    }	

    newMap('canvas', mapCoords);
}

$(function(){ init(); });
</pre>
<p>So the function above begins by taking the data we just extracted using PHP and puts it into a variable called <code class="sh_javascript">p</code>, we also define one called <code class="sh_javascript">mapCoords</code>. Then we turn <code class="sh_javascript">p</code> into an array by splitting up each pair of coordinates, before jumping into a for loop. If you've not used the Google Maps API before this may look a little daunting, but it's fairly simple once you get started. First off we set up our loop to cycle though every set of coordinates in our array, and once inside we split our coordinates into longitude and latitude and store that array in <code class="sh_javascript">p</code>. Then we check to see if <code class="sh_javascript">mapCoords</code> is empty, if it is we use square brackets to create the array, otherwise that's the only difference. Then we add a new value to the <code class="sh_javascript">mapCoords</code> array - this value is a special object provided by the API for processing points on the map, and we just have to pass it the relevant values for the longitude and latitude. Once the loop is complete, we call a mysterious function named <code class="sh_javascript">newMap()</code>, and pass it the ID of our map canvas, as well as our newly created route coordinates. Lets take a look at how we implement this new function.</p>
<pre class="sh_javascript code-full-width">
function newMap(id, mapCoords){
    var centre = (new google.maps.LatLng(51.44031275716014, 0.3955078125)),
	zoomLevel = 6,
        route,
        myOptions,
        map;

    route = new google.maps.Polyline({
        path: mapCoords,
        strokeColor: "#2324e4",
        strokeOpacity: .70,
        strokeWeight: 7,
        editable: false
    });
		 &nbsp; &nbsp;
    myOptions = {
        center: centre,
        zoom: zoomLevel,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById(id), myOptions);
    route.setMap(map);
}
</pre>
<p>First up we define a few variables: the map centre - as it appears to the user (here it's somewhere between the UK and France), zoom level - 6 will zoom pretty far out (16 is street level), and the route - we'll add to this soon. Then we make use of our <code>route</code> variable - this is a line that will be drawn on our map and will represent the route between posts. There are a number of options here, but the most important is the 'path' - we'll set it equal to the coordinates passed to our function. Next up we define some options for our map and put them into <code>myOptions</code>. Here we set the centre and zoom level - as defined above, as well as the type of map we want to display - here we've created a road-map. Finally we create our map, and tell our route that it needs to draw itself on that map.</p>
<p>Up to this point the code we've written will draw a blue line on your map, so if that's all you need you're done! But if you want to add your posts to the map as markers read on!</p>
<h3>Plot Posts with Pins</h3>
<p>Now it might be the case that you don't want a route plotted, or maybe you want a route with pins indicating where you've posted from - and once you've implemented the back-end above its remarkably simple. The only difference is that because we'll want to show the user an info window we'll need to store the names, dates, and links for our posts. This just requires a simple modification of the backend:</p>
<pre class="sh_php code-full-width">
&lt;?php
    $points = '';
    $thePosts = '';

    query_posts('posts_per_page=500'); 

    while ( have_posts() ) : the_post(); 

        $points .= '(' . get_post_meta($post->ID, 'geo_latitude', true) . ',' . get_post_meta($post->ID, 'geo_longitude', true) . '),';
        $thePosts .= '(' . get_the_title() . '|' . get_permalink() . '|' . get_the_time() . '),';
    endwhile; 

    $points = substr($points, 1, -2); // Remove the initial '(' and final '),'
    $thePosts = substr($thePosts, 1, -2);
?>

&lt;div id="canvas">&lt;/div>
</pre>
<p>So the only difference here is that we've now got another variable named <code class="sh_php">$thePosts</code> - which holds the title, permalink, and time posted for each post. We'll do essentially the same to this when we get into the JavaScript, and then we can use the data on the map. Notice I've used a pipe character (|) as the delimiter as it's common to have commas in titles. So lets take a look at the <code class="sh_javascript">init()</code> function in our JavaScript:</p>
<pre class="sh_javascript code-full-width">
function init(){
    var p = "&lt;?php global $points; echo $points; ?>", mapCoords;

    p = p.split("),(");

    for(var x = 0; x < p.length; x++){
	p[x] = p[x].split(",");
	if(mapCoords == null){
		mapCoords = [new google.maps.LatLng(p[x][0], p[x][1])];
	}else {
		mapCoords.push(new google.maps.LatLng(p[x][0], p[x][1]));
	}
    }	

    /* Create posts array */
    var posts = "&lt;?php global $thePosts; echo $thePosts; ?>";
    posts = posts.split('),(');

    for(var x = 0; x < posts.length; x++){
	posts[x] = posts[x].split('|');
    }

    newMap('canvas', mapCoords, posts);
}
</pre>
<p>Here we've just added another variable and a for loop - notice this is above the <code class="sh_javascript">newMap()</code> call. We'll also alter this function to accept the <code>posts</code> variable. Now that we've got our variables set up, we can get into changing up our hefty <code class="sh_javascript">newMap()</code> function. But before we get to that we need to define a global variable that we'll use in the function, so go ahead and make a global variable like so: </p>
<pre class="sh_javascript code-full-width">
var infowindow = new google.maps.InfoWindow();
</pre>
<p>This variable will be used to display an information window when the user clicks on a point, and we can utilise the handy API once again to do this. Now we can get into the juicy function to create our map:</p>
<pre class="sh_javascript code-full-width">
function newMap(id, mapCoords, posts){
    var centre = (new google.maps.LatLng(51.44031275716014, 0.3955078125)),
	zoomLevel = 6,
        route,
        myOptions,
        map;

    route = new google.maps.Polyline({
        path: mapCoords,
        strokeColor: "#2324e4",
        strokeOpacity: .70,
        strokeWeight: 7,
        editable: false
    });

    myOptions = {
        center: centre,
        zoom: zoomLevel,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById(id), myOptions);
    route.setMap(map);

    // New code starts here

    function getInfoWindowEvent(marker, x) {
	infowindow.close()
	infowindow.setContent('&lt;div class="infowindow">&lt;a href="'+posts[x][1]+'">&lt;strong>'+posts[x][0]+'&lt;/strong>&lt;br>'+posts[x][2]+'&lt;/a>&lt;/div>');
	infowindow.open(map, marker);
    }

    var markers = [];

    for(var x = 0; x < mapCoords.length; x++){
        markers[x] = new google.maps.Marker({
	    position: mapCoords[x],
	    map: map,
	    icon: 'http://example.com/images/pin.png' // Remove this to use the default pin
	});

	google.maps.event.addListener(markers[x], 'click', (function(x) {
	    return function(){
		getInfoWindowEvent(markers[x], x);
	    }
	})(x));
    }
}
</pre>
<p>Notice the comment about half way through - I'll begin explaining the code from there onwards.</p>
<p>First up we create ourselves a function called <code class="sh_javascript">getInfoWindowEvent()</code> - this will be used to move our info-window that we defined earlier around the map and to put our post content into it each time a point is clicked. The HTML inside the <code class="sh_javascript">setContent()</code> function is entirely up to you, so feel free to play with the styling and organisation of the code. Next up we create a new array called <code class="sh_javascript">markers[]</code> - this will hold every marker for the map. Then we have a for loop to iterate over every set of map coordinates that we have. Inside the loop we first create a new marker - the options here should be fairly obvious, but the icon one is entirely optional, remove it to use the default, well-known pin icon, otherwise supply a valid URL. Once we've got our array of markers we need to add event listeners to get ready for click events. The function we use simply calls the <code class="sh_javascript">getInfoWindowEvent()</code> function with the appropriate variables, namely the marker that has been clicked, and the index of that marker.</p>
<p>As a little side-note, if you want your own, custom pin on single post pages for the map, it's simply a matter of replacing <code class="sh_html">img/wp-pin.png</code>, and for greater customisation there's also <code class="sh_html">wp_pin_shadow.png</code>.</p>
<p>And that's it! You should now have a beautiful, dynamic map displaying all your posts - perfect for travel blogs!</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/implement-the-wordpress-geolocation-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Update</title>
		<link>http://tomsbigbox.com/quick-update/</link>
		<comments>http://tomsbigbox.com/quick-update/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 18:38:19 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1362</guid>
		<description><![CDATA[So I just thought I better update the ol' blog - not posted since 2011 - yikes! I'm still here, and I've not given up on it! I'm busy working on a large-scale project at the moment, so I've been consumed with work, but I will be posting a bunch of stuff relating to the project soon - there is much to blog about in the way of in-depth tutorials...]]></description>
			<content:encoded><![CDATA[<p>So I just thought I better update the ol' blog - not posted since 2011 - yikes! I'm still here, and I've not given up on it! <img src='http://tomsbigbox.com/core/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I'm busy working on a large-scale project at the moment, so I've been consumed with work, but I will be posting a bunch of stuff relating to the project soon - there is much to blog about in the way of in-depth tutorials into all kinds of things from complex JavaScript, to even more complex PHP. I've got ideas for articles on not only the techniques, but also approaches to things like refactoring and testing code and websites as a whole. So stay tuned, and I'll be with you in the not too distant future!</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/quick-update/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create a dynamic Twitter-search feed</title>
		<link>http://tomsbigbox.com/create-a-dynamic-twitter-search-feed/</link>
		<comments>http://tomsbigbox.com/create-a-dynamic-twitter-search-feed/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 17:09:49 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[hash-tag]]></category>
		<category><![CDATA[long-polling]]></category>
		<category><![CDATA[tweets]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[twitter api]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1341</guid>
		<description><![CDATA[For years the ancient (and some would say instinctive) art of tweeting was restricted to the avian species of the sky, but recently a service has come about that allows us humans to partake in the practise. You might have heard of this little company, they call themselves Twitter, and have provided an extensive API for us web-folk to play with their service. At this point you might be wondering...]]></description>
			<content:encoded><![CDATA[<p>For years the ancient (and some would say instinctive) art of tweeting was restricted to the avian species of the sky, but recently a service has come about that allows us humans to partake in the practise. You might have heard of this little company, they call themselves Twitter, and have provided an extensive API for us web-folk to play with their service. At this point you might be wondering what the heck I'm going on about, so how's this? We'll be creating a dynamic Twitter feed based on a search term, that automatically updates without the user having to refresh the page, while ensuring we don't overload our own servers with long-polling (more on that in a mo). We'll even take a look at allowing only a certain set of users to appear in our steam, to avoid spammers. Let's get started! </p>
<p>A quick bit of background about the technical side of this before we get going though. For us to be able to update our stream constantly we need AJAX to poll (or make a request) to a server, to ask for any new tweets. Now when I first began playing around I had a page on my own server that was requested every 10 seconds, that would then grab, parse, and output any new tweets, and return it to my stream. But, and it's a big but, this approach can devastate your servers if you're not careful. If you've never delved into long-polling before you're lucky! But take it from me, having 100 users requesting a page on your server <em>every</em> 10 seconds, each, is a <em>really</em> bad idea. So in this tutorial we'll look at how we can use JavaScript to parse and output results directly from Twitter's servers.</p>
<p>To begin we'll look at how we can <abbr title="Get, Parse, and Output">GP+O</abbr> (Get, Parse, and Output) our tweets with PHP. Here's our code:</p>
<pre class="code-full-width sh_php">
function searchTwitter($search) {
    $url = 'http://search.twitter.com/search.atom?rpp=300&#038;q='.urlencode($search) ;
    $ch = curl_init($url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $xml = curl_exec ($ch);
    curl_close ($ch);

    $result = new SimpleXMLElement($xml);

    foreach ($result->entry as $entry) {
    	$author = trim($entry->author->name);
		$name = explode(' (', $entry->author->name);
        $content = trim($entry->title);
        $time = @strtotime($entry->published);
        $id = $entry->id;

        echo "&lt;li data-id=\"".str_replace('tag:search.twitter.com,2005:', '', $id)."\">
        		&lt;img src=\"http://api.twitter.com/1/users/profile_image/$name[0]?size=normal\" />
        		&lt;div class=\"content\">
        			&lt;span class=\"name\">".substr($name[1], 0, -1).":&lt;/span>&lt;br>
        			&lt;span class=\"tweet\">".$content."&lt;/span>&lt;br>
        			&lt;span class=\"time\">Posted ".gmdate('j/n/y g:i a',$time)."&lt;/span>
        		&lt;/div>
        	&lt;/li>";
    }
}

searchTwitter('myquery');
</pre>
<p>Because we're good developers we're encapsulating our code into a handy, reusable function - aren't we good?! Let's look at what we're doing here. First off we use PHP's cURL library to make a request to Twitter. There are a bunch of URL variables avalible, all of which can be found over at <a href="https://dev.twitter.com/docs/api/1/get/search" target="_blank">the Twitter API docs</a>, the only ones we're concerned with are "rpp" - results-per-page, and "q" - query. I've set the results-per-page pretty high, but the function of this variable is fairly self-explanatory, as for the query variable, notice the use of the handy <code class="sh_php">urlencode()</code> function, which will take care of encoding things like hash-tags and spaces in our queries. Phew! That the first line done! The next few lines simply request the page, and shove the resulting data into the <code class="sh_php">$xml</code> variable.</p>
<p>We then use the excellent SimpleXML parser to translate our raw XML data into a useful variable. From then onwards we use a <code class="sh_php">foreach()</code> loop to go through every tweet in our list. If you'd like to know what variables are contained in each entry, just whack a <code class="sh_php">print_r()</code> in the loop. For our purposes we only need to access a few parts of each entry. Here's a list of our variables, and what they do:</p>
<ul>
<li><code class="sh_php">$author</code>: The user's name in the format "[Full name] ([User name])"</li>
<li><code class="sh_php">$name</code>: An array of the user's name in the form [0]=>"Full name", [1]=>"Username )" - yes that's a bracket on the end</li>
<li><code class="sh_php">$content</code>: The tweet itself, all tidied up using <code class="sh_php">trim()</code></li>
<li><code class="sh_php">$time</code>: A useful representation of when the tweet was posted</li>
<li><code class="sh_php">$id</code>: The unique tweet ID - we'll use this later when we request updates</li>
</ul>
<p>We can use these variables to then output a list that looks very much like Twitter itself, with the user's profile image to the left and data to the right. Our code outputs list items with nicely formatted dates, and here's where you might want to update the code to reflect your markup. And that's it for the PHP! However, if you want to have a stream of tweets only from approved users, you would want the following, updated code:</p>
<pre class="sh_php code-full-width">
function searchTwitter($search) {
    $url = 'http://search.twitter.com/search.atom?rpp=300&#038;q='.urlencode($search) ;
    $ch = curl_init($url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $xml = curl_exec ($ch);
    curl_close ($ch);

    $result = new SimpleXMLElement($xml);

    $hidden = array();

    foreach ($result->entry as $entry) {
    	$author = trim($entry->author->name);
		$name = explode(' (', $entry->author->name);

    	if(in_array(strtolower($name[0]), array('user1', 'user2'))){
	        $content = trim($entry->title);
	        $time = @strtotime($entry->published);
	        $id = $entry->id;
	        echo "&lt;li data-id=\"".str_replace('tag:search.twitter.com,2005:', '', $id)."\">
	        		&lt;img src=\"http://api.twitter.com/1/users/profile_image/$name[0]?size=normal\" />
	        		&lt;div class=\"content\">
	        			&lt;span class=\"name\">".substr($name[1], 0, -1).":&lt;/span>&lt;br>
	        			&lt;span class=\"tweet\">".$content."&lt;/span>&lt;br>
	        			&lt;span class=\"time\">Posted ".gmdate('j/n/y g:i a',$time)."&lt;/span>
	        		&lt;/div>
	        	&lt;/li>";

        }else { array_push($hidden, $name[0]); }
    }

	echo "&lt;!-- Tweets from: ";
	for($x = 0; $x &lt; count($hidden); $x++){
		echo $hidden[$x] . ', ';
	}
	echo " have been hidden -->";
}

searchTwitter('myquery');
</pre>
<p>Notice the addition of the <code class="sh_php">$hidden</code> array - which will allow us to keep track of any blocked tweets. Stepping inside our loop you'll notice the addition of an if statement that checks to see if the entry was posted by a user in an array of approved tweeters. If it is, then it continues to output the tweet, otherwise it adds it to the array of blocked users. And just for fun, we output a HTML comment at the end to let us know if any users were blocked.</p>
<p>Now that we've got our PHP sorted, let's take a look at how we can use JavaScript to make this bad-boy dynamic! Before we write any of our own code, we need to borrow for some excellent chaps for some utility functions - if you want to format the date any differently to how Twitter returns it by default, you'll want to go and grab <a href="http://jacwright.com/projects/javascript/date_format/" target="_blank">the JavaScript Date.format code</a>, and if you want to only allow tweets from pre-approved users, you'll want a <a href="http://phpjs.org/functions/in_array:432" target="_blank">translation of PHP's in_array() function</a>. Once you've got that code, we can go straight ahead and use:</p>
<pre class="sh_javascript code-full-width">
var lastUpdate;

function update(){
	$.ajax({
		url: "http://search.twitter.com/search.json?q=myquery&#038;since_id="+lastUpdate,
		dataType: "jsonp",
		success : function(data){
			var tweets = data.results;
			tweets.reverse();
			for(var x = 0; x &lt; tweets.length; x++){
				var date = new Date(tweets[x].created_at);
				date = date.format('d/m/y g:ia');
				$('.stream').prepend('&lt;li data-id="'+tweets[x].id_str+'" class="new hidden">&lt;img src="'+tweets[x].profile_image_url+'" />&lt;div class="content">&lt;span class="name">'+tweets[x].from_user_name+'&lt;/span>&lt;br>&lt;span class="tweet">'+tweets[x].text+'&lt;/span>&lt;br>&lt;span class="time">Posted '+date+'&lt;/span>&lt;/div>&lt;/li>');
				$('.new').slideDown().removeClass('new');
			}

			if(tweets.length > 0){
				lastUpdate = tweets[tweets.length - 1].id_str;
			}

			setTimeout(function(){ update(); }, 10000);
		}
	});
}

lastUpdate = $('.stream li:first').data('id');
setTimeout(function(){update();}, 10000);
</pre>
<p>So in the code above we've created a function that makes an AJAX request to Twitter to ask for new tweets. We use the most recent tweet's ID in our request, using the parameter "since_id" - stored in the <code class="sh_javascript">lastUpdate</code> variable (this is first assigned a value at the bottom of the code, and extracts the ID from the first list item in the ".stream" list). Notice the URL features a ".json" file extension - perfect JavaScript goodness, that will allow us to play around with the data. If the request is a success we then go about adding any new tweets to our stream. </p>
<p>We first create the <code class="sh_javascript">tweets</code> variable, and reverse it - we do this because they are returned in reverse-chronological order (most recent first), and we want to output them in chronological order. We then loop through all the new tweets, formatting, prepending, and sliding-down one-by-one. Once the loop is finished we check to see if we actually had any new tweets, and if we did, we update our <code class="sh_javascript">lastUpdate</code> variable to reflect the most recent tweet in our stream. Finally we use the <code class="sh_javascript">setTimeout()</code> function to call our function again in 10 seconds - essentially mimicking real-time updates. And that's our code!</p>
<p>If you want to allow updates from only approved users, here's what the JavaScript looks like:</p>
<pre class="sh_javascript code-full-width">
var lastUpdate;
var allowedNames = ['user1','user2'];

function update(){
	$.ajax({
		url: "http://search.twitter.com/search.json?q=myquery&#038;since_id="+lastUpdate,
		dataType: "jsonp",
		success : function(data){
			var tweets = data.results;
			tweets.reverse();
			for(var x = 0; x &lt; tweets.length; x++){
				if(in_array(tweets[x].from_user, allowedNames)){
					var date = new Date(tweets[x].created_at);
					date = date.format('d/m/y g:ia');
					$('.stream').prepend('&lt;li data-id="'+tweets[x].id_str+'" class="new hidden">&lt;img src="'+tweets[x].profile_image_url+'" />&lt;div class="content">&lt;span class="name">'+tweets[x].from_user_name+'&lt;/span>&lt;br>&lt;span class="tweet">'+tweets[x].text+'&lt;/span>&lt;br>&lt;span class="time">Posted '+date+'&lt;/span>&lt;/div>&lt;/li>');
					$('.new').slideDown().removeClass('new');
				}else {
					$('.stream').prepend('&lt;!-- Blocked Tweet from: '+tweets[x].from_user+' -->');
				}
			}

			if(tweets.length > 0){
				lastUpdate = tweets[tweets.length - 1].id_str;
			}

			setTimeout(function(){ update(); }, 10000);
		}
	});
}

lastUpdate = $('.stream li:first').data('id');
setTimeout(function(){update();}, 10000);
</pre>
<p>And <em>that</em> is how to create a dynamic Twitter stream using PHP and JavaScript!</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/create-a-dynamic-twitter-search-feed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a bullet-proof contact form</title>
		<link>http://tomsbigbox.com/create-a-bullet-proof-contact-form/</link>
		<comments>http://tomsbigbox.com/create-a-bullet-proof-contact-form/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 08:15:51 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1308</guid>
		<description><![CDATA[Contact pages are usually one of the basic building blocks of any website, and while many simple feature an email address for spam bots to pick up and use, or even a handy 'mailto' link, the best ones feature a proper contact form. To make a bulletproof one we're going to need some thick glass and a riot shield PHP and JavaScript. Having those bad-boys on our side will ensure...]]></description>
			<content:encoded><![CDATA[<p>Contact pages are usually one of the basic building blocks of any website, and while many simple feature an email address for spam bots to pick up and use, or even a handy 'mailto' link, the best ones feature a proper contact form. To make a bulletproof one we're going to need <del datetime="2011-10-02T09:03:31+00:00">some thick glass and a riot shield</del> PHP and JavaScript. Having those bad-boys on our side will ensure we can create a beautiful AJAX-enabled means of contact for our users. We also need to ensure our form will work on the rare occasion that a user has JavaScript turned-off <em>*gasp*</em> - I know, it's a scary thought, but I'm sure we'll figure something out!</p>
<p>So we'll start with some simple HTML to set out our fields, the following code is what we'll be working with:</p>
<pre class="code-full-width sh_html">
&lt;form id="contact" class="right" method="post" action="mail.php">
	&lt;h3 class="hidden success">&lt;br/>Message sent!&lt;/h3>

	&lt;label>Name: &lt;span class="warning right">&lt;/span>
		&lt;input type="text" name="name" />
	&lt;/label>

	&lt;label>Email: &lt;span class="warning right">&lt;/span>
		&lt;input type="text" name="email" />
	&lt;/label>

	&lt;label>Message: &lt;span class="warning right">&lt;/span>
		&lt;textarea name="message">&lt;/textarea>
	&lt;/label>
	&lt;input class="right" type="submit" value="Send" />
&lt;/form>
</pre>
<p>Aside from the obvious, the form features a few extra elements - namely the <code>.warning</code> elements - we'll see what they're for in a moment. I also assume that you have the class of <code>.right</code> set up to float elements to the right; if now, then you'll need to float the affected elements individually in your CSS. Right, that's the HTML sorted, lets take a look at the JavaScript for this puppy.</p>
<pre class="code-full-width sh_javascript">
function validateEmail(email){
	 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	 return email.match(re);
}

$('#contact').submit(function(){
	var name, email, message, errors;

	errors = 0;
	name = $('input[name=name]').val();
	email = $('input[name=email]').val();
	message = $('textarea[name=message]').val();

	$('.warning').text('');

	if(name==''){ $('input[name=name]').siblings('.warning').text('This field is required!'); errors++; }
	if(email==''){ $('input[name=email]').siblings('.warning').text('This field is required!'); errors++; }
	if(message==''){ $('textarea[name=message]').siblings('.warning').text('This field is required!'); errors++; }
	if(!validateEmail(email)){ $('input[name=email]').siblings('.warning').text('Please enter a valid email!'); errors++; }

	if(errors==0){
		var dataString = $(this).serialize() + '&#038;js=true';

		$.ajax({
			url: 'contact-post.php',
			data: dataString,
			type: 'POST',
			success: function(data){
				$('form label, form input[type=submit]').slideUp(500, function(){
					$('form .success').hide().removeClass('hidden').slideDown(500);
				});
			}
		});
	}

	return false;
});
</pre>
<p>Now for the moment we'll ignore the <code class="sh_javascript">validateEmail()</code> function, and take a look at the form submission code. First off we set up some variables for the values in our form, this prevents us having to use longer code snippets and querying the DOM too much. Once we've got them set up we give the user the benefit of the doubt and remove any warning that may have previously been set by removing any text from the elements with the class of <code>.warning</code>. And then we validate the fields. The first three 'if' statements simply check that the user has entered a value in each field - if they haven't we tell them so, and increment our <code>errors</code> variable by 1. The last 'if' statement uses the <code>validateEmail()</code> function that we set earlier. There's no reason to worry if you don't understand how that function works, <a href="http://en.wikipedia.org/wiki/Regular_expression" target="_blank">Regular Expressions</a> are a world of their own. All we need to know, is that it tells us if the user has entered a valid email address.</p>
<p>Following our rather basic validation checks we then test to see if the form has passed - because we've been using our <code>errors</code> variable all along, if it's set to '0' we can rest assured nothing has gone wrong. If that's the case we serialise the form, and use a simple AJAX request to submit the data to our server. If our server is happy, we show our users a success message. Now all we need to do is set up our <code>contact-post.php</code> file, like so:</p>
<pre class="sh_php code-full-width">
function redirect($hash){
	if($hash!='success'){
		echo 'Invalid ' . $hash;
	}else {
		echo 'Message sent!';
		echo '&lt;meta http-equiv="refresh" content="0;url=http://example.com/">';
	}
	die();
}

if($_POST){
	if(empty($_POST['js'])){
		// Validate info here
		if(empty($_POST['name'])){ redirect('name'); }
		if(empty($_POST['email'])){ redirect('email'); }
		if(empty($_POST['message'])){ redirect('message');  }
		if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ redirect('email'); }
	}

	$sendto = 'you@example.com';

	$name = $_POST['name'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$message = $_POST['message'];

	$to = $sendto;
	$subject = "[MySiteName] Message";

	$message = "
	&lt;html>
	&lt;head>
	&lt;title>Contact form Submission&lt;/title>
	&lt;/head>
	&lt;body>
	&lt;p style='font-family:Arial, Helvetica, sans-serif; color:black;'>The following was sent by &lt;strong>".$name." (Email: ".$email."):&lt;/strong>&lt;/p>
	&lt;p style='font-family:Arial, Helvetica, sans-serif; color:black; font-size:16px;'>".nl2br(stripslashes($message))."&lt;/p>

	&lt;p style='font-family:Arial, Helvetica, sans-serif; color:black;'>(Sent on: ".gmdate('d\/m\/y').")&lt;/p>
	&lt;/body>
	&lt;/html>
	";

	$headers = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
	$headers .= 'From: ' . $name;

	mail($to,$subject,$message,$headers);

	redirect('success');
}else {
        redirect('submission - no data entered!');
}
</pre>
<p>The function at the top will be used when we want to give the user some feedback - if they entered all the correct info, they'll be given a success message, and then redirected to your site in a timely fashion, otherwise they will be told what is wrong with their submission. Then we get to the guts of the script - we first make sure there is some data to play with - if there isn't we tell the user off, and if there is we go on to see if the user has JavaScript running. You may have noticed the little variable 'js' that we added to the serialised form back in our JavaScript - that is our way of finding out if the form is being submitted via an AJAX request in JavaScript - if the variable is empty, we let PHP validate our form's content - this is essentially identical to the JS code we used previously.</p>
<p>We then set up a really basic email, and send it to the address in the <code class="sh_php">$sendto</code> variable. Finally we redirect the user to the site. As a side note, do remember that this page will only be seen by users without JavaScript turned on.</p>
<p>And that's it! You now have a swanky contact form that, for all intents and purposes, is bullet-proof, but having said that, I wouldn't actually test that theory with a gun of any kind...</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/create-a-bullet-proof-contact-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prevent broken animations in jQuery</title>
		<link>http://tomsbigbox.com/prevent-broken-animations-in-jquery/</link>
		<comments>http://tomsbigbox.com/prevent-broken-animations-in-jquery/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 09:30:56 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[animations]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[bound]]></category>
		<category><![CDATA[hover]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[stopped]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1301</guid>
		<description><![CDATA[I recently posted an article on Animating a Site's Loading using jQuery - a handy technique to spice up any site. But an issue that became apparent using this technique, along with some other animation methods, is that sometimes user interaction can prevent loading and other animations from finishing correctly, and possibly lead to pages that look broken because of it. This is the sort of thing that only user-testing...]]></description>
			<content:encoded><![CDATA[<p>I recently posted an article on <a href="http://tomsbigbox.com/animate-a-sites-loading/" target="_blank">Animating a Site's Loading</a> using jQuery - a handy technique to spice up any site. But an issue that became apparent using this technique, along with some other animation methods, is that sometimes user interaction can prevent loading and other animations from finishing correctly, and possibly lead to pages that look broken because of it. This is the sort of thing that only user-testing will usually uncover, as many web-designers simply sit back and watch the animations, before actually interacting with the site. To explain what I mean I'll show you a basic example of navigation animations - and the one that prompted this article.</p>
<pre class="sh_javascript code-full-width">
$('nav li').each(function(index){
    $(this).slideDown(200*(index+1));
});

$('nav li').hover(function(){
    $(this).stop().animate({'paddingTop':'10px'},200);
});
</pre>
<p>Now the code above works perfectly fine - it slides-down each list element in my navigation panel, and whenever a user rolls over one with their mouse the list element moves down by 10 pixels. Great, but we have a slight problem; namely, the <code class="sh_javascript">stop()</code> function. The code uses it to prevent an animation build up if a user quickly moves their mouse over multiple list elements for example, and for that it works very well. But because we are using a loading animation, if a user hovers over a list element before it has completed the the slide-down animation, the list element will be stuck in limbo, or rather, it won't finish sliding down.</p>
<p>This is caused by the fact that the <code class="sh_javascript">hover()</code> is bound to each list element at the same time that the animations are being executed. To solve this issue we need to come up with a way of waiting for the animations to complete before binding any events to our elements. One way of doing this might be to add a class to each element as it finishes it's loading, and then checking to see if the class exists before performing any events. But that approach will quickly clog up both your JavaScript, and HTML. To avoid messy code, we should instead only bind such events once the animation has completed. </p>
<p>To do that, we just make a simple alteration, like so:</p>
<pre class="sh_javascript code-full-width">
$('nav li').each(function(index){
    $(this).slideDown(200*(index+1), function(){
        $(this).hover(function(){
            $(this).stop().animate({'paddingTop':'10px'},200);
        });
    });
});
</pre>
<p>The code above uses the callback function of the <code class="sh_javascript">slideDown()</code> function to wait until the animation completes. We then bind the hover event to each element individually, and we have ourselves a much better setup to prevent our sites breaking in the hands of hover-happy users.</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/prevent-broken-animations-in-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Importing hand-drawn art into Photoshop</title>
		<link>http://tomsbigbox.com/importing-hand-drawn-art-into-photoshop/</link>
		<comments>http://tomsbigbox.com/importing-hand-drawn-art-into-photoshop/#comments</comments>
		<pubDate>Sun, 18 Sep 2011 11:57:47 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[hand-drawn art]]></category>
		<category><![CDATA[live-trace]]></category>
		<category><![CDATA[Photoshop]]></category>
		<category><![CDATA[scan]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1286</guid>
		<description><![CDATA[Using Photoshop to design websites it great - it allows for pixel-perfect designs that can then be styled to your hearts-content using all the powerful tools included in the application. But sometimes it's nice to add a human touch, and have hand-drawn art in our designs. Now I personally don't do this all too often, but the other day when a client asked for a design that I simply couldn't...]]></description>
			<content:encoded><![CDATA[<p>Using Photoshop to design websites it great - it allows for pixel-perfect designs that can then be styled to your hearts-content using all the powerful tools included in the application. But sometimes it's nice to add a human touch, and have hand-drawn art in our designs. Now I personally don't do this all too often, but the other day when a client asked for a design that I simply couldn't create in Photoshop, I reached for the good-old pencil and paper to draw a fish sat in a chair (this might seem somewhat odd out of context, but there you go), and some nice lettering.</p>
<p>So what are our options? For me I wanted to draw the art, then colour it in with Photoshop, and add it to the site, and the distinct lack of thorough tutorials I found didn't do a great deal to help, so I thought I'd document my process here. </p>
<p>I started by sitting down and actually drawing the final product, and after an hour or two of trying to make a realistic fish I scanned the bad boy in, and came up with the following:</p>
<p><a href="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-1.jpg"><img src="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-1-265x300.jpg" alt="Hand-drawn fish" title="Hand-drawn fish" width="265" height="300" class="aligncenter size-medium wp-image-1287" /></a></p>
<p>Great, I was happy with the simple design, and now I needed to 'vectorise' it. And here is where I hit my brick wall. Illustrator wouldn't play ball - I tried a billion different settings for the live trace tool, and it simply failed miserably. I concluded this was down to the opacity of the drawing, so I traced it with a 0.3mm fine-liner to make it stand out a little more. Here's what I ended up with:</p>
<p><a href="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-2.jpg"><img src="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-2-248x300.jpg" alt="Fish fineliner" title="Fish fineliner" width="248" height="300" class="aligncenter size-medium wp-image-1288" /></a></p>
<p>Not bad, but still Illustrator just looked at me blankly. And that's when I found out about <a href="http://vectormagic.com" target="_blank">VectorMagic</a> - an online tool to convert hand-drawn art to vectors. Now the service is free for the first 2 images you upload, and then there's a subscription of $8 a month, or you can buy the desktop version. So while it's not a complete solution, it works damn well! (I eventually forked out for the desktop client and I can honestly say it works every time). Now if you don't fancy being locked into a service like that the only other option is to manually trace (using the pen tool) in Photoshop - I know, this isn't really a solution, and that's why I seriously suggest you give VectorMagic a go.</p>
<p>So, having put my image through the VM process I got the following:</p>
<p><a href="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-3.jpg"><img src="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-3-204x300.jpg" alt="Traced Fish" title="Traced Fish" width="204" height="300" class="aligncenter size-medium wp-image-1289" /></a></p>
<p>Yay! It's starting to look a lot better. From there I began to colour in my creation using the fill tool, and the brush. After about 10 minutes I had:</p>
<p><a href="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-4.jpg"><img src="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-4-204x300.jpg" alt="Fish coloured-in" title="Fish coloured-in" width="204" height="300" class="aligncenter size-medium wp-image-1290" /></a></p>
<p>And then I added highlights and shadows to make it look more realistic - using a layer with the blend mode set to multiply, and a small brush using black and white for shadows and highlights respectively. The final fish took it's form:</p>
<p><a href="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-5.jpg"><img src="http://tomsbigbox.com/core/wp-content/uploads/2011/09/fish-5-204x300.jpg" alt="Fish final" title="Fish final" width="204" height="300" class="aligncenter size-medium wp-image-1291" /></a></p>
<p>And that's now my process for importing vector art.</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/importing-hand-drawn-art-into-photoshop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hide an element with jQuery whilst scrolling</title>
		<link>http://tomsbigbox.com/hide-an-element-with-jquery-whilst-scrolling/</link>
		<comments>http://tomsbigbox.com/hide-an-element-with-jquery-whilst-scrolling/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 17:03:49 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[scrolling]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1279</guid>
		<description><![CDATA[Today I came across an interesting problem that no amount of Google searches could solve. So I had to knuckle down and come up with a solution - and surprisingly it was much more simple that it had first appeared. So the problem is hiding an element when a user is scrolling - it might be a menu, a header, or a picture of my cat, Sophie. Either way we...]]></description>
			<content:encoded><![CDATA[<p>Today I came across an interesting problem that no amount of Google searches could solve. So I had to knuckle down and come up with a solution - and surprisingly it was much more simple that it had first appeared.</p>
<p>So the problem is hiding an element when a user is scrolling - it might be a menu, a header, or a picture of my cat, Sophie. Either way we can't just use the simple <code class="sh_javascript">delay()</code> function in jQuery to help us out - we need to use some raw JavaScript. To accomplish this we will create a variable into which we will place the current scroll amount of the page, we will then wait for a period of time, and check if that scroll amount has changed. If it has we will leave the element hidden, otherwise we'll show it to the user again.</p>
<p>And here's the rather simple code to do just that:</p>
<pre class="sh_javascript code-full-width">
$(document).scroll(function(){
	$('section').fadeOut();

	var scrollA = $('body').scrollTop();

	setTimeout(function(){
		if(scrollA == $('body').scrollTop()){
			$('section').fadeIn();
		}
	}, 200);
});
</pre>
<p>And there we have it! A really simple effect made easy with a tiny bit of JavaScript.</p>
<p>You can check out the fiddle over here: http://jsfiddle.net/LJVMH/</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/hide-an-element-with-jquery-whilst-scrolling/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Animate a site&#8217;s loading</title>
		<link>http://tomsbigbox.com/animate-a-sites-loading/</link>
		<comments>http://tomsbigbox.com/animate-a-sites-loading/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 08:32:51 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[animate]]></category>
		<category><![CDATA[improve site]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[speed up]]></category>
		<category><![CDATA[subtle]]></category>
		<category><![CDATA[UX]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1274</guid>
		<description><![CDATA[There are a lot of things we can do as web designers to improve the UX of a site, much of the time subtle animations can be added to add a bit of finesse to a page, and a new trend is that of adding animations to the loading of pages to make it look a bit better. So what do I mean by 'loading animations'? I'm not talking about...]]></description>
			<content:encoded><![CDATA[<p>There are a lot of things we can do as web designers to improve the UX of a site, much of the time subtle animations can be added to add a bit of finesse to a page, and a new trend is that of adding animations to the loading of pages to make it look a bit better. So what do I mean by 'loading animations'? I'm not talking about the annoying "feature" of having a site load in a flash and then display a loading spinner while it actually pulls up the content using AJAX; rather adding a tiny amount of code to gain more control over how our site loads. But before we jump in to the code we need to remember a couple of things: first the animations can't take very long, after all we're trying to improve the user experience, so having a site take 5 seconds to load in will just frustrate the user. And second, as we will be using JavaScript for this, we need to add a fallback in case it's turned off.</p>
<p><a href="http://tomsbigbox.com/examples/Loading/" target="_blank">Here's what we'll be creating (new window).</a></p>
<p>So to begin I've written up a very simple page, here's the HTML:</p>
<pre class="sh_html code-full-width">
&lt;!DOCTYPE HTML>
&lt;html>
&lt;head>
	&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	&lt;title>Animated Loading&lt;/title>
	&lt;!--[if IE]>
		&lt;script src="http://html5shim.googlecode.com/svn/trunk/html5.js">&lt;/script>
	&lt;![endif]-->
	&lt;link rel="stylesheet" href="style.css" type="text/css" />
&lt;/head>
&lt;body>
	&lt;div class="wrapper">
		&lt;header>
			&lt;h1 class="left">My Website&lt;/h1>
			&lt;nav>
				&lt;ul>
					&lt;li>Home&lt;/li>
					&lt;li>More&lt;/li>
					&lt;li>About&lt;/li>
					&lt;li>Contact&lt;/li>
				&lt;/ul>
			&lt;/nav>
		&lt;/header>
		&lt;section class="hidden">
			&lt;h2>Welcome&lt;/h2>
			&lt;p>Yay for loading animations!&lt;/p>
		&lt;/section>
		&lt;footer class="hidden">
			&lt;p>Copyright goes here&lt;/p>
		&lt;/footer>
	&lt;/div>
&lt;/body>
&lt;/html>
</pre>
<p>Notice the use of the class "hidden" - that does exactly what you might think, and to make sure it actually works, let's hook up the CSS:</p>
<pre class="sh_css code-full-width">
/* Globals */
.wrapper {
	max-width:1200px;
	display: block;
	margin:0 auto;
	padding:0 10px;
}

.hidden {
	display: none;
}

.left { float:left; }
.right { float:right; }

body {
	background: #dfdfdf;
}

/* Header */
header {
	overflow: hidden;
	position: relative;
	top:-145px;
}

h1 {
	background: #222;
	padding:50px;
	font-size: 2.5em;
	line-height: 0.4em;
	color: #fff;
}

nav {
	float: right;
}

	nav li {
		padding-top:52px;
		float: left;
		width:91px;
		text-align: center;
		height:90px;
		cursor: pointer;
		margin-right:2px;
		display: none;
	}

/* Sections */
section {
	margin-top:2%;
	padding:2%;
	background: #fff;
	overflow: hidden;
}

	section h2 {
		font-size: 1.25em;
		font-weight: bold;
	}

/* Footer */
footer {
	text-align: center;
	padding-top:10px;
}
</pre>
<p>Again, some very basic CSS to ensure it actually looks like a functional website. Notice how elements such as the 'nav li' elements are hidden using CSS (you might want to just add a class in the HTML, but if they are dynamically generated, this is your best bet).</p>
<p>If we open the page now we don't see very much, some would say it was blank, many people actually. This is what users without JavaScript enabled will see. So to remedy that we need to make use of the <code class="sh_html">&lt;noscript></code> tag in our header. Just before the closing <code class="sh_html">&lt;head></code> tag, add the following code:</p>
<pre class="sh_html code-full-width">
&lt;noscript>
	&lt;style type="text/css">
		header {
			top:0px !important;
		}

		section, footer, nav li {
			display: block !important;
		}
	&lt;/style>
&lt;/noscript>
</pre>
<p>That code will only be executed if we don't have access to JavaScript, and so will force the elements we worked so hard to hide, to show up. Good stuff, we're not breaking the internet by requiring JavaScript! And on that note, let's write some.</p>
<p>Here's the code I wrote for the example - it's very basic, and executes within 2.45 seconds (but because some animations are in parallel it feels like 1.25 seconds). Make sure you include jQuery on your page, and then whack in the following code:</p>
<pre class="sh_javascript code-full-width">
$(function(){
	/* Loading Animations */
	$('header').animate({'top':0},500, function(){
		$('nav li').each(function(index){
			$(this).slideDown(300*(index+1));
		});

		$('section, footer').fadeIn(750).removeClass('hidden');
	});

	$('a:link').click(function(){
		$('header').animate({'top':'-185px'},300);
		$('section, footer').fadeOut(300);
	});
});
</pre>
<p>The first part of the that code waits for the DOM to be fully loaded, and then animates the header to slide in. When the header has finished showing up we cycle through the navigation's list elements, sliding them down at a growing animation length (notice the '+1' to ensure we don't multiply by zero). Once that's done we fade in any other elements we had hidden, and remove the class of 'hidden' - here you could use another <code class="sh_javascript">each()</code> cycle, but in the interests of speed I just stuck with a simple fade in effect. The next block of code is for links away from the page. We look for any links, and then whenever they are clicked, we quickly animate the contents of the page out in 0.3 seconds - I think this is just a nice touch. With a bit of tweaking of things like timings and the odd animation, you can make a visitor smile before they've even read what's on your site.</p>
<p>And that's how to improve the UX of your websites with subtle loading animations.</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/animate-a-sites-loading/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reality check: Why Adobe Muse doesn&#8217;t matter.</title>
		<link>http://tomsbigbox.com/reality-check-why-adobe-muse-doesnt-matter/</link>
		<comments>http://tomsbigbox.com/reality-check-why-adobe-muse-doesnt-matter/#comments</comments>
		<pubDate>Sat, 27 Aug 2011 16:06:45 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Deep Thought]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[muse]]></category>
		<category><![CDATA[Photoshop]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1270</guid>
		<description><![CDATA[Recently a number of people in the web community, specifically developers have been complaining about Adobe Muse, a website creation tools for people without coding knowledge. And while I agree that Adobe doesn't know what the community wants, I think the reactions to this tool have been a little too big. Here are my musings (I couldn't resist that badboy). First up Muse isn't a new concept. In fact there...]]></description>
			<content:encoded><![CDATA[<p>Recently a number of people in the web community, specifically developers have been complaining about Adobe Muse, a website creation tools for people without coding knowledge. And while I agree that Adobe doesn't know what the community wants, I think the reactions to this tool have been a little too big. Here are my musings (I couldn't resist that badboy).</p>
<p>First up Muse isn't a new concept. In fact there are a billion and one tools to allow people without knowledge of coding to build websites, <a href="http://squarespace.com/" target="_blank">SquareSpace</a> is just one example. They are not aimed at people who know HTML and CSS, and so people like me don't use them. But the reason these applications are popular isn't because making websites is easy, or because everyone has a web-designer within them, it's because people usually start off with a great template. People who don't have a design background and who then use a service like Muse to build a site without a template, generally end up with a crap website. Does that bother me? Nope, it doesn't impact my trade in a bad way, in fact it's good if anything because I can say to potential clients "look at what this guy did, it doesn't look very good does it? And that' why you should work with me.".</p>
<p>Using a tool like Muse is like buying a microwave meal, it looks the part, it tastes mediocre, and it's full of crap. Although you can create websites with Muse, as developers know, the code it spits out isn't great. This impacts things like SEO, thus making the website harder to find.</p>
<p>Anyone that follows me on Twitter knows that I do sometimes complain about Photoshop. Sure there are some bugs, it's way over-priced, and the company that makes it has lost touch with the creative industry, but to be honest it does the job. I don't expect great things from Adobe, and neither should you. While they are off spending time and effort creating nasty applications like Muse (written in AIR), I'm confident <a href="http://projectmeteor.org/">others are looking for an alternative</a>.</p>
<p>So stop complaining about Muse, it wasn't built for you, it doesn't affect you, and although Adobe has changed (despite it once being about the music), it doesn't matter.</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/reality-check-why-adobe-muse-doesnt-matter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 Best Practises for jQuery Beginners</title>
		<link>http://tomsbigbox.com/10-best-practises-for-jquery-beginners/</link>
		<comments>http://tomsbigbox.com/10-best-practises-for-jquery-beginners/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 08:29:15 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[beginning jquery]]></category>
		<category><![CDATA[best practises]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=1262</guid>
		<description><![CDATA[jQuery is the one of the most popular JavaScript frameworks available; it enables developers to write code that is easy to understand but that also allows really powerful functionality. There are a boat-load of tutorials out there on jQuery, and the documentation is awesome, but in this article I'll go over the things that all beginners should know about jQuery. 1) Document.ready One thing that often catches newbies out is...]]></description>
			<content:encoded><![CDATA[<p>jQuery is the one of the most popular JavaScript frameworks available; it enables developers to write code that is easy to understand but that also allows really powerful functionality. There are a boat-load of tutorials out there on jQuery, and the documentation is awesome, but in this article I'll go over the things that all beginners should know about jQuery.</p>
<h3>1) Document.ready</h3>
<p>One thing that often catches newbies out is that their code doesn't seem to be having any effect on their page, despite all of it being correct. A major cause of this is that their jQuery code is attempting to access DOM elements before they exist. This can happen because developers place their JavaScript in the <head> of their pages, and so by the time the rest of the page has loaded jQuery doesn't know what's going on. The simple way to solve this issue is by telling jQuery to execute the code only when the DOM has finished loading, like so:</p>
<pre class="code-full-width sh_javascript">
$(document).ready(function(){
	//Code goes here
});
</pre>
<p>But be warned, there are some circumstances where you shouldn't bother wrapping code in it. For example if you're clued-up about performance and already have all your code at the bottom of your page, it's unnecessary to use it, because all the elements already exist. Other exceptions might include wanting better code reusability - say for instance placing non-jQuery functions outside of the wrapping.</p>
<h3>2) Cache elements</h3>
<p>A lot of the time people write functions like:</p>
<pre class="code-full-width sh_javascript">
$('.myelement').click(function(){
	$(this).doSomething();
	$(this).doSomethingElse();
	$(this).fadeOut(500);
});
</pre>
<p>And there isn't anything wrong with that. But if you started doing 10 things to the element once it were clicked it would start to look a little messy. To fix this it's a good idea to put the $(this) selector into a variable, like so:</p>
<pre class="code-full-width sh_javascript">
$('.myelement').click(function(){
	var el = $(this);
	el.doSomething();
	el.doSomethingElse();
	el.fadeOut(500);
});
</pre>
<p>This will give you less code to write in the long run, and using descriptive variable names will also help you keep focus on what you are manipulating. But more important than that is the performance increase - asking jQuery to return an element over and over as we did before wastes valuable performance - so caching will speed up your code!</p>
<h3>3) Chaining</h3>
<p>And the above brings me nicely to my next point - chaining. You can chain jQuery functions, meaning that you don't have to separate all those lovely functions, so the code I just wrote could actually look like:</p>
<pre class="code-full-width sh_javascript">
$('.myelement').click(function(){
	$(this).doSomething().doSomethingElse().fadeOut(500);
});
</pre>
<p>And by just chaining those functions we have condensed 3 lines of code into 1.</p>
<h3>4) AJAX</h3>
<p>One reason why a number of developers like jQuery is because it has an excellent syntax for AJAX. In Javascript making AJAX requests is cumbersome and difficult to learn, but in jQuery it's pretty easy. So if you've taken the time to fade in messages to your users using jQuery, why not go ahead and submit forms using AJAX, thus creating a more cohesive user experience. </p>
<p>This is how it's done:</p>
<pre class="code-full-width sh_javascript">
$('.myform').submit(function(){
	var dataString = $(this).serialize();

	$.ajax({
		url: 'process.php',
		data: dataString,
		type: 'POST',
		success: function(data){
			//Do something
		}
	});
});
</pre>
<p>The above code waits for the form with a class of '.myform' to be submitted, and then sends the data in the form via a POST method to 'process.php'. If the whole thing works well the success function is called and accepts some data. You would simply set up process.php to either return an error or simply say 'success', and then act accordingly - perhaps showing a success balloon or error notification.</p>
<h3>5) Using delegate()</h3>
<p>Most beginners typically start by using syntax such as:</p>
<pre class="code-full-width sh_javascript">
$('.myelement').click(function(){
	//Do something
});
</pre>
<p>Just as I did above. There isn't anything technically wrong with that code, it's just that it could be better. Newbies will often find them googeling why their click() method doesn't work on dynamically created elements (as could be done using AJAX), and the reason for this is because jQuery binds the event handlers to the elements when the page is loaded. So we have to change the way we write our code to ensure we don't run into any issues. </p>
<p>To do that we can use the handy delegate() method. For example:</p>
<pre class="code-full-width sh_javascript">
$('.myWrapper').delegate('p', 'hover', function(){
	$(this).doSomething();
});
</pre>
<p>The code above tells jQuery to listen for hover events on paragraph elements within the .wrapper element. The method allows us to add elements without worrying if event handlers will listen out for changes to them, and so it is good practise to use it in production.</p>
<h3>6) Using powerful selectors</h3>
<p>Complex selectors in jQuery allow us to tailor more specific and targeted interactions on our websites. Say for instance we want to target all paragraphs, except those with a class of 'not-me'. A great way to do this is like so:</p>
<pre class="code-full-width sh_javascript">
$('p:not(.not-me)').click(function(){
	$(this).doSomething();
});
</pre>
<p>There we used the :not() syntax to exclude elements with a class of '.not-me' - a simple, but very effective way of selecting only the elements we want. Furthermore we may only wish to select the first paragraph on our page, to do that we would use another handy selector:</p>
<pre class="code-full-width sh_javascript">
$('p:first').click(function(){
	$(this).doSomething();
});
</pre>
<p>There are a bunch of powerful selectors besides just class and ID based ones, you can take a look in the docs over at: <a href="http://api.jquery.com/category/selectors/" target="_blank">http://api.jquery.com/category/selectors/</a></p>
<h3>7) Use callback functions</h3>
<p>I often answer questions from people who want to know how they can trigger actions once other actions have finished execution. And the simple answer is by using callbacks. For those new to jQuery the term by be unfamiliar, but you've already witnessed one in the AJAX example above! A callback method is one that is called after the method in question has done it wants to do, in jQuery a lot of functions allow you to pass a callback function of your own; let's take a look at how we'd do this:</p>
<pre class="code-full-width sh_javascript">
$('.myelement').click(function(){
	$(this).fadeOut(500, function(){
		alert('Element faded out!');
	});
});
</pre>
<p>The code above listens for a click on our elements, fades it out, and then alerts the user about it. The callback function is passed as a separate parameter to our fadeOut() method, and is executed after the element is faded-out.</p>
<h3>8 ) Use a context</h3>
<p>In the interests of performance let's take a moment to think about how jQuery finds elements. When you say something like:</p>
<pre class="code-full-width sh_javascript">
$('.myelement').click(function(){  });
</pre>
<p>jQuery searches the entire DOM for it. But what if you only have things with that class within say a div with a class of '.main-content'? If so you can provide a context to jQuery (much like we did with the delegate() method above). Doing this enables jQuery to only search within that context, thus saving on performance. And here's how it would look:</p>
<pre class="code-full-width sh_javascript">
var context = $('.main-content');

$('.myelement', context).click(function(){
	$(this).doSomething();
});
</pre>
<p>The more specific (and smaller) your context, the faster your code.</p>
<h3>9) Pass objects not identifiers</h3>
<p>When writing your own functions it might seem like a good idea to do something like:</p>
<pre class="code-full-width sh_javascript">
function myfunction(id){
	$(id).doSomething();
}

myFunction(“.myElement");
</pre>
<p>But that really isn't very flexible. It's far better to actually pass the object around like so:</p>
<pre class="code-full-width sh_javascript">
function myfunction(el){
	el.doSomething();
}

myFunction($(“.myElement"));
</pre>
<h3>10) Don't depend on jQuery</h3>
<p>While the techniques above are good advice for writing solid jQuery, you certainly shouldn't depend on the thing. Never use jQuery as a replacement for CSS - not only is it more inefficient, but you are ignoring users without Javascript turned on, and it's a sure fire way to annoy users.</p>
]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/10-best-practises-for-jquery-beginners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching using disk: basic
Object Caching 604/749 objects using disk: basic

Served from: tomsbigbox.com @ 2012-05-19 09:43:34 -->
