<?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>Webmaster-Source &#187; Coding</title>
	<atom:link href="http://www.webmaster-source.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webmaster-source.com</link>
	<description>Useful Resources For Webmasters</description>
	<lastBuildDate>Thu, 09 Feb 2012 11:19:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Git: The Simple Guide</title>
		<link>http://www.webmaster-source.com/2012/01/19/git-the-simple-guide/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=git-the-simple-guide</link>
		<comments>http://www.webmaster-source.com/2012/01/19/git-the-simple-guide/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 11:10:58 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4491</guid>
		<description><![CDATA[Have you been trying to wrap your head around Git, the version control software that everyone is talking about? It works a little differently than tools like Subversion, so you&#8217;ll have to unlearn some habits if you&#8217;re familiar with other version control packages. A fancy new one-page website has been circulating Twitter and Reddit of [...]]]></description>
			<content:encoded><![CDATA[<p>Have you been trying to wrap your head around Git, the version control software that everyone is talking about? It works a little differently than tools like Subversion, so you&#8217;ll have to unlearn some habits if you&#8217;re familiar with other version control packages.</p>
<p>A fancy new one-page website has been circulating Twitter and Reddit of late, serving as an easy into to Git. <a href="http://rogerdudler.github.com/git-guide/">Git: The Simple Guide</a> helps you install the software, create a repository on your local computer or check out an existing remote one, and start using Git.</p>
<p><a href="http://rogerdudler.github.com/git-guide/"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4492 imgborder" title="Git: The Simple Guide" src="http://media.webmaster-source.com/wp-content/uploads/2012/01/git-the-simple-guide.png" alt="" width="560" height="205" /></a></p>
<p>It&#8217;s a good tutorial, and the design looks great. I like the differently-colored segments with the large text and rounded styling on the <code>&lt;code&gt;</code> elements. (The page is actually hosted on <a href="http://pages.github.com/">GitHub Pages</a>, which seems fitting, given the subject.)</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4491&amp;md5=347a99324f5fb46340367fefa7b00954" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2012/01/19/git-the-simple-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternatives to GitHub</title>
		<link>http://www.webmaster-source.com/2012/01/11/alternatives-to-github/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=alternatives-to-github</link>
		<comments>http://www.webmaster-source.com/2012/01/11/alternatives-to-github/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 11:42:45 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[Hosting]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4480</guid>
		<description><![CDATA[GitHub is great for open source projects, but the lack of free private repositories can be limiting for projects that you would rather not be shared with the entire world. While it does make sense to pay for a tool if you use it extensively for commercial purposes, you may not be ready to if [...]]]></description>
			<content:encoded><![CDATA[<p><a href="https://github.com/">GitHub</a> is great for open source projects, but the lack of free private repositories can be limiting for projects that you would rather not be shared with the entire world.</p>
<p>While it does make sense to pay for a tool if you use it extensively for commercial purposes, you may not be ready to if you&#8217;re an indie developer just beginning a project that won&#8217;t hit the shelves for awhile. Also, if you&#8217;re using it for versioning and deploying websites, you may not want to have all of your source be public.</p>
<p>GitHub&#8217;s business model is primarily the sale of <a href="https://github.com/plans">premium accounts</a> with varying numbers of allowed private repositories, starting at $7/month for five private repositories and up to one collaborator. It&#8217;s not a bad deal at all, as GitHub is by and large open source projects, but you may still want to find a better offer. Fortunately, there are a couple of solid competitors.</p>
<h3>BitBucket</h3>
<p><a href="https://bitbucket.org/"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright size-full wp-image-4481" title="BitBucket" src="http://media.webmaster-source.com/wp-content/uploads/2012/01/bitbucket.png" alt="" width="140" height="42" />BitBucket</a> is very much like GitHub, functionally. It has the same sort of social collaboration tools, and most of the same features. But that&#8217;s where the differences end. Their <a href="https://bitbucket.org/plans">pricing scheme</a> is to give you all of the repositories you could possibly want, both public and private, while charging to add extra collaborators to your private repositories. You get five for free, and can upgrade to ten for $10/month, twenty-five for $20/month, etc.. Oh, and they support both Git and Mercurial.</p>
<p>I&#8217;ve been using BitBucket lately for some of my projects, and it&#8217;s been great so far.</p>
<h3>Beanstalk</h3>
<p><a href="http://beanstalkapp.com/"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright size-full wp-image-4482 imgborder" title="Beanstalk" src="http://media.webmaster-source.com/wp-content/uploads/2012/01/beanstalkapp.png" alt="" width="191" height="65" />Beanstalk</a> only does private repositories, offering plans limited by disk space and the number of repositories, as well as the number of authorized users. Aside from their 100MB single-repository trial plan, their offerings start at $15/month for 3GB of space, 10 repositories and 5 users. One of their unique selling points is their integration with tools like Basecamp and Lighthouse. They also have quite a few corporate customers.</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4480&amp;md5=a4e5f61d77cc4fd7a3cdf61857263e1d" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2012/01/11/alternatives-to-github/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSFiddle: A Playground for Web Developers</title>
		<link>http://www.webmaster-source.com/2012/01/05/jsfiddle-a-playground-for-web-developers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jsfiddle-a-playground-for-web-developers</link>
		<comments>http://www.webmaster-source.com/2012/01/05/jsfiddle-a-playground-for-web-developers/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 12:01:33 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[(x)html]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4420</guid>
		<description><![CDATA[JSFiddle is a sort of interactive pastebin site that I&#8217;ve been finding useful lately. It features three panes for entering HTML, CSS and JavaScript, and a fourth where the resulting output is rendered. If you save the workspace, it generates a URL like http://jsfiddle.net/fLP64/ and will even save each revision as you update it. You [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jsfiddle.net/">JSFiddle</a> is a sort of interactive <a href="http://en.wikipedia.org/wiki/Pastebin">pastebin</a> site that I&#8217;ve been finding useful lately. It features three panes for entering HTML, CSS and JavaScript, and a fourth where the resulting output is rendered. If you save the workspace, it generates a URL like <code>http://jsfiddle.net/fLP64/</code> and will even save each revision as you update it. You can share the URL to show off the result, and the other user can play with the code as well.</p>
<p>This is great for two scenarios. You can use it to experiment with a bit of CSS or JavaScript for brainstorming purposes, or you can use it if you need to ask someone for help. (It&#8217;s a lot easier to figure out what&#8217;s going wrong with someone&#8217;s code or markup if you can jump right in and start messing with the code.) Chris Coyier (of <a href="http://css-tricks.com/">CSS-Tricks</a>) uses it all the time to show off CSS experiments that will likely be used in future posts of his.</p>
<p style="text-align: center;"><a href="http://jsfiddle.net/fLP64/"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4421 imgborder" title="JSFiddle" src="http://media.webmaster-source.com/wp-content/uploads/jsfiddle.png" alt="" width="600" height="288" /></a></p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4420&amp;md5=5c398a27554e8dde0da6ae45ede21b7d" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2012/01/05/jsfiddle-a-playground-for-web-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5 Data Attributes</title>
		<link>http://www.webmaster-source.com/2011/11/21/html5-data-attributes/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=html5-data-attributes</link>
		<comments>http://www.webmaster-source.com/2011/11/21/html5-data-attributes/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 11:31:38 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[(x)html]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4403</guid>
		<description><![CDATA[Have you ever looked at the HTML snippet for Twitter&#8217;s Tweet button and wondered what those data-something="loremipsum" attributes were? &#60;a href=&#34;https://twitter.com/share&#34; data-count=&#34;vertical&#34; data-via=&#34;redwall_hp&#34;&#62;Tweet&#60;/a&#62; &#60;script type=&#34;text/javascript&#34; src=&#34;//platform.twitter.com/widgets.js&#34;&#62;&#60;/script&#62; I did too, and after a bit of Google searching, I found a post by John Resig (the creator of jQuery) that explains that they&#8217;re something called data attributes. [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever looked at the HTML snippet for Twitter&#8217;s <a href="https://twitter.com/about/resources/tweetbutton">Tweet button</a> and wondered what those <code>data-something="loremipsum"</code> attributes were?</p>
<pre class="brush: xml;">
&lt;a href=&quot;https://twitter.com/share&quot; data-count=&quot;vertical&quot; data-via=&quot;redwall_hp&quot;&gt;Tweet&lt;/a&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;//platform.twitter.com/widgets.js&quot;&gt;&lt;/script&gt;
</pre>
<p>I did too, and after a bit of Google searching, I found a post by John Resig (the creator of jQuery) that explains that they&#8217;re something <a href="http://ejohn.org/blog/html-5-data-attributes/">called data attributes</a>.</p>
<p>A new feature in HTML5, they provide a way to embed data in an element and access it from a script. (An accompanying JavaScript API makes it very easy to retrieve the data.)</p>
<p><a href="http://ejohn.org/blog/html-5-data-attributes/">HTML 5 data- Attributes</a> [John Resig]</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4403&amp;md5=c3c04756642d69cc3b56fbddd3dfdc1a" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2011/11/21/html5-data-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Google-Hosted jQuery With a Local Fallback</title>
		<link>http://www.webmaster-source.com/2011/11/07/using-google-hosted-jquery-with-a-local-fallback/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-google-hosted-jquery-with-a-local-fallback</link>
		<comments>http://www.webmaster-source.com/2011/11/07/using-google-hosted-jquery-with-a-local-fallback/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 11:43:09 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[CDN]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4384</guid>
		<description><![CDATA[Referencing commonly-used JavaScript libraries, like jQuery, stored on Google&#8217;s CDN is a good way to speed up your site. Many popular websites do so, which means the chances of a user having jQuery sitting in their browser cache already is pretty high. It doesn&#8217;t make a whole lot of sense to download it all over [...]]]></description>
			<content:encoded><![CDATA[<p>Referencing commonly-used JavaScript libraries, like jQuery, stored on Google&#8217;s CDN is a good way to speed up your site. Many popular websites do so, which means the chances of a user having jQuery sitting in their browser cache already is pretty high. It doesn&#8217;t make a whole lot of sense to download it all over again for another website, does it? Using the copy on the <a href="http://code.google.com/apis/libraries/">Google Libraries</a> CDN just makes a lot of sense.</p>
<p>But what if the servers Google has hosting it went down for some reason? Given Google&#8217;s track record, it&#8217;s not likely to be an issue, but it&#8217;s a good point. Fortunately, you can easily reference a backup. You can have a copy of jQuery on your server, and use a little bit of JavaScript to load it only if the Google one doesn&#8217;t load for some reason.</p>
<p>This little snippet, found in <a href="http://html5boilerplate.com/">HTML5 Boilerplate</a>, will do just that:</p>
<pre class="brush: xml;">
&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;window.jQuery || document.write('&lt;script src=&quot;js/libs/jquery-1.6.4.min.js&quot;&gt;&lt;\/script&gt;')&lt;/script&gt;
</pre>
<p>This should <a href="http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/">even work with WordPress</a>, if you put the second script line right before the &lt;/head&gt; tag in your theme.</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4384&amp;md5=cb345bbcf03ca9a095fbad0fe271e34e" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2011/11/07/using-google-hosted-jquery-with-a-local-fallback/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building an iPhone App to Parse the Twitter API with NSXMLParser</title>
		<link>http://www.webmaster-source.com/2011/10/24/building-an-iphone-app-to-parse-the-twitter-api-with-nsxmlparser/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-an-iphone-app-to-parse-the-twitter-api-with-nsxmlparser</link>
		<comments>http://www.webmaster-source.com/2011/10/24/building-an-iphone-app-to-parse-the-twitter-api-with-nsxmlparser/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 11:20:58 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[Xcode]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4362</guid>
		<description><![CDATA[iOS has a simple event-based XML parser built in, which makes it fairly easy to do less involved parsing operations without having to load up a third-party framework. This tutorial will show you how to build a simple iPhone application that will download an XML feed from Twitter containing a user&#8217;s tweets, and then display [...]]]></description>
			<content:encoded><![CDATA[<p>iOS has a simple event-based XML parser built in, which makes it fairly easy to do less involved parsing operations without having to load up a third-party framework. This tutorial will show you how to build a simple iPhone application that will download an XML feed from Twitter containing a user&#8217;s tweets, and then display them with a pretty UI. (You could easily adapt this to parse other XML documents, such as RSS feeds.)</p>
<p style="text-align: center;"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4364 imgborder" title="Displaying data from a Twitter XML feed in iOS" src="http://media.webmaster-source.com/wp-content/uploads/nsxmlparser-example-fantasyfolder.jpg" alt="" width="600" height="306" /></p>
<h3><span id="more-4362"></span>Getting Started</h3>
<p>First, create a new View-based application. Give it a memorable name like &#8220;TwitterXML.&#8221;</p>
<p style="text-align: center;"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4365 imgborder" title="Creating a view-based application in Xcode" src="http://media.webmaster-source.com/wp-content/uploads/nsxmlparser-view-based-application.png" alt="" width="600" height="471" /></p>
<p>Now that you have a clean slate to work off, let&#8217;s rename some classes. I find Xcode&#8217;s default naming scheme a bit silly, with the way it prepends the project name to each file. I think the Application Delegate should be called, simply, <em>AppDelegate.m</em> instead of the needlessly long <em>TwitterXMLAppDelegate.m.</em> However, you can&#8217;t just rename the file to whatever your preference is, as that would break things.</p>
<p>You can rename a class project-wide, file and all, by using the Refactoring tool. You can call it up by right-clicking on the class name in the implementation file and choosing &#8220;Refactoring&#8221; from the resulting menu.</p>
<p><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4366" title="Renaming classes with the Refactor tool" src="http://media.webmaster-source.com/wp-content/uploads/nsxmlparser-renaming-classes-with-refactor-tool.png" alt="" width="600" height="352" /></p>
<p>This renaming business is entirely optional as far as this tutorial goes, but it&#8217;s worth know how to do. Imagine if you made a typo in a class name and didn&#8217;t realize it until after you had already referenced it in a few places. It&#8217;s nice to have an automated fix.</p>
<h3>Setting Up the Header File</h3>
<p>Most of our code is going to go in the View controller, named <em>TweetViewController</em> in my case. Switch to the corresponding .h file and we can start setting up properties and whatnot.</p>
<p>First, we need to implement the NSXMLParserDelegate protocol so our class can respond to NSXMLParser delegate methods. This is easily done by adding <em>NSXMLParserDelegate</em> to the <em>@interface</em> line, like so:</p>
<pre class="brush: cpp;">
@interface TweetViewController : UIViewController &lt;NSXMLParserDelegate&gt; {
</pre>
<p>Now we need to declare some variables and other objects in the interface block. We need a string to hold the name of the Twitter user whose profile we will be accessing, a mutable array to hold the statuses we&#8217;ve pulled from the parser and a few that are used to hold data temporarily during the parsing process. Also, we need a few IBOutlets so we can update the View once we finish reading the XML data.</p>
<pre class="brush: cpp;">
@interface TweetViewController : UIViewController &lt;NSXMLParserDelegate&gt; {
NSString *twitterUser;
NSMutableArray *statuses;
NSString *currentElement;
NSMutableDictionary *currentElementData;
NSMutableString *currentElementString;
IBOutlet UIImageView *backgroundImage;
IBOutlet UILabel *tweetLabel;
IBOutlet UIImageView *avatar;
}
</pre>
<p>Of course, we need to make these objects into properties. This means adding a few property declarations after the interface block ends.</p>
<pre class="brush: cpp;">
@property (nonatomic, retain) NSString *twitterUser;
@property (nonatomic, retain) NSMutableArray *statuses;
@property (nonatomic, retain) NSString *currentElement;
@property (nonatomic, retain) NSMutableDictionary *currentElementData;
@property (nonatomic, retain) NSMutableString *currentElementString;
@property (nonatomic, retain) UIImageView *backgroundImage;
@property (nonatomic, retain) UILabel *tweetLabel;
@property (nonatomic, retain) UIImageView *avatar;
</pre>
<p>And then you need to synthesize them in the .m file by adding the following line right after the @implementation line:</p>
<pre class="brush: cpp;">
@synthesize twitterUser, statuses, currentElement, currentElementData, currentElementString, backgroundImage, tweetLabel, avatar;
</pre>
<p>Now that that&#8217;s out of the way, we can get to the interesting part.</p>
<h3>Setting Up the Parser</h3>
<p>The first method in the View controller is &lt;em&gt;viewDidLoad&lt;/em&gt;, which fires as soon as the View as loaded. (Subtle, isn&#8217;t it?) We will be putting our initialization stuff in there. Basically, we just need to ready our properties, set the Twitter username and start the parser.</p>
<pre class="brush: cpp;">
- (void)viewDidLoad {
[super viewDidLoad];
statuses = [[NSMutableArray alloc] init];
currentElement = [[NSString alloc] init];
currentElementData = [[NSMutableDictionary alloc] init];
currentElementString = [[NSMutableString alloc] init];
twitterUser = [NSString stringWithString:@&quot;collis&quot;];
[self parseXMLForUser:twitterUser];
}
</pre>
<p>After the first arrays and dictionaries are initialized, the <em>twitterUser</em> string is set to the username of the Twitter account we want the app to pull the latest statuses from. I&#8217;m using <a href="http://twitter.com/#!/collis">Collis</a>, one of the co-founders of <a href="http://envato.com/">Envato</a>, as an example. You could put any user you want there, so long as they have a cool-looking background on their profile!</p>
<p>The last line calls the <em>parseXMLForUser:</em> method and passes the <em>twitterUser</em> string along with it. We will work on that part next.</p>
<p>The <em>parseXMLForUser:</em> method is responsible for setting up the parser, as well as building the Twitter API URL.</p>
<pre class="brush: cpp;">
- (void)parseXMLForUser:(NSString *)user {

//Build the Twitter API URL by combining the user with the rest of the URL
NSString *urlString = [NSString stringWithFormat:@&quot;http://twitter.com/statuses/user_timeline/%@.xml?count=3&quot;, user];
NSURL *url = [NSURL URLWithString:urlString];

//Create an instance of NSXMLParser and download the XML data from the URL
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

//Set this class as its own delegate so we can process NSXMLParser callbacks
[parser setDelegate:self];

//Disable namespace support and other things we don't really need
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];

[parser parse]; //Go go gadget XML parser...

[parser release];

}
</pre>
<p>The first part of the method should look familiar to anyone who has worked with C or Java before. It takes our <em>user</em> argument (which contains the text of <em>twitterUser</em>) and splices it into the URL string, just before the <em>.xml</em> part. Cocoa expects URLs to be of the NSURL object type, we create a new one of those and pass it <em>urlString</em>.</p>
<p>After that is done, we create a new instance of the NSXMLParser class and nickname it &#8220;parser.&#8221; We also pass it the new URL object, which it will use to download the contents it finds there at runtime. Next we set the parser&#8217;s delegate to <em>self</em>, or the current class. The next three lines turn off some features we don&#8217;t really need. Finally, we kick the parser into action and leave a <em>[parser release]</em> command to clean up after it&#8217;s done.</p>
<p>That was simple, wasn&#8217;t it? Sadly, that was only the beginning. In order for the parser to, well, <em>parse</em> we still need to implement the delegate methods for NSXMLParser. And we need to make a spiffy UI.</p>
<h3>Building the Parser</h3>
<p>NSXMLParser is what is called an event-based parser. This means it loops around, searching a document for anything that looks like an XML tag. When it finds one, it raises an event. Basically it says &#8220;I found an opening tag named &#8216;something&#8217;&#8221; and leaves you to deal with it. The parser does the same thing with ending tags and the text between them. We have to implement delegate methods to handle these events and save the data they find.</p>
<p>Let&#8217;s start with a couple of simple ones.</p>
<pre class="brush: cpp;">
- (void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@&quot;The XML document is now being parsed.&quot;);
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@&quot;Parse error: %d&quot;, [parseError code]);
}
</pre>
<p>The first method fires when NSXMLParser starts to parse the document. For this application, there isn&#8217;t really anything that we need to do at that point. Putting an NSLog there is great for debugging, though. (If your app is crashing, it&#8217;s helpful to know whether it&#8217;s getting to that step or not.) I&#8217;m sure you can guess what the <em>parseErrorOccurred</em> method does. (It logs an error code if the XML document is malformed or if, for some other reason, the parser could not process it.)</p>
<p>Moving on, we have a method that is called when the parser finds an opening XML tag.</p>
<pre class="brush: cpp;">
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

//Store the name of the element currently being parsed.
currentElement = [elementName copy];

//Create an empty mutable string to hold the contents of elements
currentElementString = [NSMutableString stringWithString:@&quot;&quot;];

//Empty the dictionary if we're parsing a new status element
if ([elementName isEqualToString:@&quot;status&quot;]) {
[currentElementData removeAllObjects];
}

}
</pre>
<p>This one is a bit more complicated. When it&#8217;s called, its arguments are populated with information that the parser found out about the element currently being parsed. It&#8217;s name is what we care about, primarily. Using the properties we created earlier, the method keeps track of the element currently being parsed (we need to know its name in other methods) and whatever is <em>inside</em> the element (between the opening and closing tag). The conditional statement at the end empties our dictionary every time the parser moves on to a new &lt;status&gt; element, as we will have already copied its contents to the statuses array.</p>
<p>The next delegate method takes any characters found inside an XML element and stores it in the <em>currentElementString</em> property for later.</p>
<pre class="brush: cpp;">
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//Take the string inside an element (e.g. &lt;tag&gt;string&lt;/tag&gt;) and save it in a property
[currentElementString appendString:string];
}
</pre>
<p>And finally, the penultimate method. This one contains the real meat of the parser. It is called whenever NSXMLParser comes across a closing XML tag. And so, it serves as a good place to put most of the data-saving logic.</p>
<pre class="brush: cpp;">
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

//If we've hit the &lt;/status&gt; tag, store the data in the statuses array
if ([elementName isEqualToString:@&quot;status&quot;]) {
[statuses addObject:[currentElementData copy]];
}

//Trim any extra spaces and newline characters from around currentElementString
NSString *string = [currentElementString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

//Store the status data in the currentElementData dictionary
if ([currentElement isEqualToString:@&quot;created_at&quot;]) {
[currentElementData setObject:string forKey:@&quot;created_at&quot;];
} else if ([currentElement isEqualToString:@&quot;text&quot;]) {
[currentElementData setObject:string forKey:@&quot;text&quot;];
} else if ([currentElement isEqualToString:@&quot;retweeted&quot;]) {
[currentElementData setObject:string forKey:@&quot;retweeted&quot;];
} else if ([currentElement isEqualToString:@&quot;id&quot;]) {
[currentElementData setObject:string forKey:@&quot;id&quot;];
} else if ([currentElement isEqualToString:@&quot;profile_image_url&quot;]) {
[currentElementData setObject:string forKey:@&quot;profile_image_url&quot;];
} else if ([currentElement isEqualToString:@&quot;profile_background_image_url&quot;]) {
[currentElementData setObject:string forKey:@&quot;profile_background_image_url&quot;];
} else if ([currentElement isEqualToString:@&quot;profile_link_color&quot;]) {
[currentElementData setObject:string forKey:@&quot;profile_link_color&quot;];
}

}
</pre>
<p>The first code chunk saves the contents of the <em>currentElementData</em> dictionary to the <em>statuses</em> array if, and only if, the ending tag being processed currently is &lt;/status&gt;. If you remember from before, <em>currentElementData</em> will be emptied the next time the <em>didStartElement</em> method is called. Otherwise, the block will be skipped and the application will handle the tasks it needs to run for child elements of &lt;status&gt;.</p>
<p>After stripping out extraneous spaces and newline characters from either side of <em>currentElementString</em>, so we don&#8217;t end up with weird output, we have a rather long if/else if block. This checks whether the element being parsed is one we want to save (e.g. &#8220;text&#8221; or &#8220;profile_image_url&#8221;) and if it is, it adds it to the element data dictionary.</p>
<p>The code may seem a bit strange at first, but it should make more sense after you become more familiar with it.</p>
<p>And now, for the last delegate method. This one fires when the document has finished parsing. This is the place to launch any operations we want to be started after we have our data. As you can see below, logging the <em>statuses</em> array to the console and then calling a method to display that data is what we will be doing here.</p>
<pre class="brush: cpp;">
- (void)parserDidEndDocument:(NSXMLParser *)parser {
//Document has been parsed. It's time to fire some new methods off!
NSLog(@&quot;%@&quot;, statuses);
[self updateView];
}
</pre>
<h3>The View</h3>
<p>After all that code, let&#8217;s work on the interface. Double-click the <em>TweetViewController.xib</em> file (or whatever your View XIB is called) in the Xcode sidebar to open it in Interface Builder. Now that your screen is sufficiently cluttered with windows, you want to drag a UIImageView from the Library window into your View canvas. Make sure that it is sized to fit the whole available area.</p>
<p>Of course, the Image View won&#8217;t be much use to us unless we link it with the controller. Right-click on the File&#8217;s Owner icon and drag the little rubberband/wire thing from the <em>backgroundImage</em> Outlet over to the UIImageView and drop it. The File&#8217;s Owner overlay window should update to show the Image View as being connected to <em>backgroundImage</em>.</p>
<p style="text-align: center;"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4367 imgborder" title="Adding a UIImageView in Interface Builder" src="http://media.webmaster-source.com/wp-content/uploads/nsxmlparser-ib-adding-uiimageview.png" alt="" width="600" height="440" /></p>
<p>I think this app would be better if it used a horizontal orientation, don&#8217;t you? Click the little arrow icon in the upper right corner of the View canvas. Interface Builder should automagically resize the Image View inside it to still fill the View. Save the XIB file out and switch back to Xcode. Now we have to configure the application to use a landscape orientation instead of the default portrait one.</p>
<p>Inside your controller class there should be a method called <em>shouldAutorotateToInterfaceOrientation</em>. It&#8217;s commented out by default. Uncomment it and change the interfaceOrientation to <em>UIInterfaceOrientationLandscapeLeft</em>. It should look like this:</p>
<pre class="brush: cpp;">
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
</pre>
<p>Back in Interface Builder, drop in a new UIImageView. We won&#8217;t be hooking this one up to an IBOutlet. Instead, we will set the image to the word bubble image I made (it&#8217;s in the project file), resize the view to be the same point-width as the image (375&#215;208) and position it neatly over the background image view. I also lowered the opacity a bit, just because I liked the effect.</p>
<p>Now we need a way to display the contents of the latest tweet. So drag a UILabel onto the View canvas and resize it to fit nicely over the word bubble graphic. Turn the &#8220;# Lines&#8221; setting up to five or so, set the font size to something that looks legible and change the &#8220;Line Breaks&#8221; option to &#8220;Word Wrap. Then wire it up to the <em>tweetLabel</em> IBOutlet, like you did with the UIImageView.</p>
<p><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4368" title="Adding the Label" src="http://media.webmaster-source.com/wp-content/uploads/nsxmlparser-adding-the-label.png" alt="" width="600" height="421" /></p>
<p>Ready to tie everything together? Switch back to Xcode and add one last method to the controller class.</p>
<pre class="brush: cpp;">
- (void)updateView {

//Select the latest tweet
NSDictionary *latestTweet = [statuses objectAtIndex:0];

//Set the tweet label
[tweetLabel setText:[latestTweet objectForKey:@&quot;text&quot;]];

//Set the background image after downloading it.
NSString *urlString = [latestTweet objectForKey:@&quot;profile_background_image_url&quot;];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *background = [[UIImage alloc] initWithData:data];
[backgroundImage setImage:background];
backgroundImage.contentMode = UIViewContentModeScaleAspectFill;

}
</pre>
<p>While it looks nearly as intimidating as the NSXMLParser <em>didEndElement</em> delegate method, it&#8217;s actually quite a bit simpler. The first line gets the newest tweet from the <em>statuses</em> array, the =<em>[tweetLabel setText:...]</em>= line updates the UILabel with the text of the message, and the last part changes the background image behind the word bubble to be the same as the Twitter user&#8217;s profile background.</p>
<p>That last part needs the most explanation. Before we can display the image (which is what the <em>setImage</em> line does) we have to download it first. Taking the <em>urlString</em>, which of course is a string containing the web address where the image can be found, we convert it to a NSURL object, which is named <em>url</em>. We create a new NSData object and use it&#8217;s <em>dataWithContentsOfURL</em> method to download the image. (Cocoa requires that URLs used with it&#8217;s objects be of the NSURL class.) Next we initialize a UIImage object with the NSData object and set it as the image in the UIImageView named backgroundImage. Oh, and we set the content mode to <em>UIViewContentModeScaleAspectFill</em> so it&#8217;s not squished funny.</p>
<p>Now if you build and run the app, you should get something like this:</p>
<p style="text-align: center;"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4369 imgborder" title="A styled tweet in the iOS app" src="http://media.webmaster-source.com/wp-content/uploads/nsxmlparser-first-demo-tweet-on-bg.png" alt="" width="600" height="394" /></p>
<p>Before we free up our allocated memory and finish the app up, let&#8217;s add one more thing: an avatar field! Switch back to Interface Builder and add a new UIImageView. Resize it to 52&#215;52 or so and wire it up to the &#8220;avatar&#8221; IBOutlet. Re-using the code from the background image bit, we can quickly modify it for the avatar.</p>
<pre class="brush: cpp;">
//Set the avatar image after downloading it.
NSString *avatarUrlString = [latestTweet objectForKey:@&quot;profile_image_url&quot;];
NSURL *avatarUrl = [NSURL URLWithString:avatarUrlString];
NSData *avatarData = [NSData dataWithContentsOfURL:avatarUrl];
UIImage *avatarImage = [[UIImage alloc] initWithData:avatarData];
[avatar setImage:avatarImage];
avatar.contentMode = UIViewContentModeScaleAspectFill;
</pre>
<p>That goes in the <em>updateView</em> method, after everything else.</p>
<p>Now, before we can say the application is finished, there is one thing that needs to be done. Any memory we specifically allocated should be released. It&#8217;s not a huge deal in a single-view app, as it will be forcefully freed up on exit, but it&#8217;s a good habit to get into. (In more complicated applications, you can expect to see frequent crashes if you don&#8217;t release objects when you&#8217;re done with them.) It&#8217;s easy to do. For every object we explicitely <em>alloc</em> or <em>retain</em>, we have to <em>release</em> somewhere. The <em>dealloc</em> method is called when the application quits in this case, so we put most of our <em>release</em> statements there.</p>
<p>You can learn more about iOS memory management in <a href="http://mobile.tutsplus.com/freebies/qa-sessions/qa-session-3-ios-memory-management-and-best-practices/">this screencast</a>.</p>
<pre class="brush: cpp;">
- (void)dealloc {
[twitterUser release];
[statuses release];
[currentElement release];
[currentElementData release];
[currentElementString release];
[backgroundImage release];
[tweetLabel release];
[avatar release];
[super dealloc];
}
</pre>
<p>And we&#8217;re done!</p>
<p style="text-align: center;"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4370 imgborder" title="And we're done!" src="http://media.webmaster-source.com/wp-content/uploads/nsxmlparser-final.png" alt="" width="505" height="342" /></p>
<h3>Additional Challenge</h3>
<p>Want to add to this sample application? Try making the following change: Use an <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html">NSTimer</a> to cycle through the items in the <em>statuses</em> array and update the View accordingly. Most of the groundwork has been laid for you already.</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4362&amp;md5=57eb5b15ba69221091c8d8f342c8a023" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2011/10/24/building-an-iphone-app-to-parse-the-twitter-api-with-nsxmlparser/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>If PHP Were British</title>
		<link>http://www.webmaster-source.com/2011/08/29/if-php-were-british/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=if-php-were-british</link>
		<comments>http://www.webmaster-source.com/2011/08/29/if-php-were-british/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 11:49:23 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4267</guid>
		<description><![CDATA[I was browsing a popular social media site the other day, when I came across a link entitled &#8220;If PHP Were British.&#8221; I started laughing out loud (for real) just a few paragraphs in. When Rasmus Lerdorf first put PHP together, he &#8211; quite sensibly, despite his heritage &#8211; chose not to write it in [...]]]></description>
			<content:encoded><![CDATA[<p>I was browsing a popular social media site the other day, when I came across a link entitled <a href="http://www.addedbytes.com/blog/if-php-were-british/">&#8220;If PHP Were British.&#8221;</a> I started laughing out loud (for real) just a few paragraphs in.</p>
<blockquote><p>When <a href="http://toys.lerdorf.com/">Rasmus Lerdorf</a> first put <a href="http://www.php.net/">PHP</a> together, he &#8211; quite sensibly, despite his heritage &#8211; chose not to write it in Greenlandic or Danish. Good job too &#8211; that would have been rather unpleasant to work with. He opted instead, being in Canada at the time, for the local tongue. No, not French &#8211; that bastard dialect of the Queen&#8217;s English commonly referred to as &#8220;US English&#8221;.</p>
<p>PHP developers in Britain have been grumpy about this ever since. What was he thinking? And more importantly, how do we undo this travesty?</p></blockquote>
<p>The rest of the post is a series of suggested &#8220;improvements&#8221; to PHP, which give it a more &#8220;British&#8221; feel. I won&#8217;t spoil them for you, as a few of them a hilarious.</p>
<p><a href="http://www.addedbytes.com/blog/if-php-were-british/">If PHP Were British</a> [Added Bytes]</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4267&amp;md5=af4357b04bb4a9785236a58b80e0e541" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2011/08/29/if-php-were-british/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Your Feet Wet with PDO and Migrating Old MySQL Code</title>
		<link>http://www.webmaster-source.com/2011/08/05/getting-your-feet-wet-with-pdo-and-migrating-old-mysql-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-your-feet-wet-with-pdo-and-migrating-old-mysql-code</link>
		<comments>http://www.webmaster-source.com/2011/08/05/getting-your-feet-wet-with-pdo-and-migrating-old-mysql-code/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 12:33:16 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4213</guid>
		<description><![CDATA[You may have heard that the old MySQL extension for PHP is going to eventually be deprecated in favor of the newer (and potentially more secure) MySQLi and PDO extensions. You&#8217;re going to need to update your old code sooner or later, so why not make it sooner? I&#8217;m going to recommend PDO over MySQLi [...]]]></description>
			<content:encoded><![CDATA[<p>You may have heard that the old MySQL extension for PHP is going to <a href="http://www.webmaster-source.com/2011/07/25/php-to-deprecate-mysql-extension-in-favor-of-mysqli-and-pdo/">eventually be deprecated</a> in favor of the newer (and potentially more secure) MySQLi and PDO extensions. You&#8217;re going to need to update your old code sooner or later, so why not make it sooner?</p>
<p>I&#8217;m going to recommend <a href="http://php.net/manual/en/book.pdo.php">PDO</a> over <a href="http://www.php.net/manual/en/book.mysqli.php">MySQLi</a> simply because it&#8217;s available on more systems, and it&#8217;s syntax may be a little bit easier to learn for newbies. PDO has been bundled with the main PHP distribution since PHP 5.1, and has been in PECL even longer, while MySQLi has only been included since 5.3. Whichever you use is up to personal preference and project requirements of course, but I will be sticking with PDO for the duration of this tutorial.<span id="more-4213"></span></p>
<p>Suppose you have a simple bit of PHP that executes a MySQL query and ouputs a list of items to screen. It might look something like this:</p>
<pre class="brush: php;">
    $db = mysql_connect(&quot;localhost&quot;, &quot;username&quot;, &quot;password&quot;);
    mysql_select_db(&quot;database&quot;, $db);

    $string = mysql_real_escape_string($string, $db);
    $query = &quot;SELECT * FROM my_table WHERE item_cat='&quot;.$string.&quot;' ORDER BY item_date DESC LIMIT 5&quot;;
    $result = mysql_query($query);

    if ( mysql_num_rows($result) &gt; 0 ) {
        while ( $row = mysql_fetch_assoc($result) ) {
            echo $row['item_title'] . '&lt;br /&gt;';
        }
    }
</pre>
<p><a href="http://xkcd.com/327/"><img style=' float: right; padding: 4px; margin: 0 0 2px 7px;'  class="alignright size-full wp-image-4214" title="XKCD: Bobby Tables" src="http://media.webmaster-source.com/wp-content/uploads/xkcd-bobby-tables.png" alt="" width="124" height="147" /></a>You&#8217;re escaping any input from a third party with mysql_real_escape_string() to prevent injection attacks, I hope. <a href="http://en.wikipedia.org/wiki/Sql_injection">SQL injection</a> is one of the most common ways data is stolen or destroyed by attackers, and it&#8217;s also fairly easy to discourage. Escaping input, while not necessarily 100% effective, should prevent most injection attacks. (And it&#8217;s pretty much your only choice with the old MySQL extension.) A better solution is parameterized statements, which we&#8217;ll get to later.</p>
<p>If your server has PDO installed (which is probably the case if you have PHP 5.1 or greater), migrating is fairly easy. The basic principles are the same, though the syntax differs just a bit. It&#8217;s actually a bit cleaner and more object-oriented.</p>
<p>The example above would look something like this:</p>
<pre class="brush: php;">
$db = new pdo(&quot;mysql:host=localhost;dbname=database_name&quot;, &quot;username&quot;, &quot;password&quot;);
$string = $db-&gt;quote($string);
$query = &quot;SELECT * FROM my_table WHERE item_cat=$string ORDER BY item_date DESC LIMIT 5&quot;;
$result = $db-&gt;query($query);

if ($result != false) {
    while ( $row = $result-&gt;fetch(PDO::FETCH_ASSOC) ) {
        echo $row['item_title'] . '&lt;br /&gt;';
    }
}

$result  = null;
</pre>
<p>Doesn&#8217;t that look nicer? Feel free to take a moment to appreciate the object-oriented goodness.</p>
<p>The biggest difference is probably the connection line. The &#8220;DSN&#8221; syntax used to connect to the database might look a bit strange at first, but it&#8217;s an important part of PDO. Since PDO can connect to other types of databases besides MySQL (e.g. PostgreSQL and SQLite), it uses a fairly standard connection string that specifies the server type besides the database name.</p>
<p>Escaping strings works essentially the same, but the syntax is slightly different. You need to remember to not put quotes around the variable in your SQL string, as the PDO::quote() method will do it for you. If you compare the SQL statements in the two examples, you&#8217;ll see the lack of quotations in the PDO example.</p>
<p>One gotcha to be aware of with PDO is that you need to set your $result variable to <em>null</em> if you intend to reuse it later on in the same script. You can end up with some weird results if you don&#8217;t. So just get in the habit of setting it to <em>null</em> or using unset() on it.</p>
<p>Now how about those <a href="http://www.php.net/manual/en/pdo.prepare.php">parameterized statement</a> things? They&#8217;re a way of ensuring that your code will be immune to SQL injection. Instead of mashing PHP strings together and passing the resulting query to the database engine, you keep the query and the potentially dangerous data separate. Placeholders are put in the query, and the data assigned to those placeholders is sent along with it.</p>
<pre class="brush: php;">
$db = new pdo(&quot;mysql:host=localhost;dbname=database_name&quot;, &quot;username&quot;, &quot;password&quot;);

$sql = &quot;SELECT * FROM my_table WHERE item_cat= :mystring ORDER BY item_date DESC LIMIT 5&quot;;
$statement = $db-&gt;prepare($sql);

$statement-&gt;execute(array(
    ':mystring' =&gt; $my_string
));

$result = $statement-&gt;fetchAll();

if ($result != false) {
    while ( $row = $result-&gt;fetch(PDO::FETCH_ASSOC) ) {
        echo $row['item_title'] . '&lt;br /&gt;';
    }
}

$result  = null;
</pre>
<p>Parameterized queries may have some performance issues on MySQL versions prior to 5.1, but they shouldn&#8217;t have any significant disadvantages on more modern systems. Security-wise, they&#8217;re considered to be better than simply escaping strings.</p>
<p>I hope this little guide has been sufficient to get you started with PDO. You will probably want to c<a href="http://php.net/manual/en/book.pdo.php">heck out the documentation</a> to see what else it can do. While you don&#8217;t necessarily need to go out and update your old code right away—the PHP devs aren&#8217;t going to drop support for the old ext/mysql extension for quite a long time, if they ever get rid of it completely—but you should definitely familiarize yourself with the newer techniques and use them in new projects.</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4213&amp;md5=d948b5fc6e8c3b64add2f21635c78e23" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2011/08/05/getting-your-feet-wet-with-pdo-and-migrating-old-mysql-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google +1 Content Unlocker with jQuery</title>
		<link>http://www.webmaster-source.com/2011/08/03/google-1-content-unlocker-with-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-1-content-unlocker-with-jquery</link>
		<comments>http://www.webmaster-source.com/2011/08/03/google-1-content-unlocker-with-jquery/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 12:26:52 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Google Plus]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Social Media]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4160</guid>
		<description><![CDATA[You&#8217;ve probably seen sites like MacHeist and Make Use Of run promotions where you can unlock a software license or enter a drawing for an iPad by posting a message to Twitter or Facebook and then entering your email address into a form that magically appears after. It has also become an increasingly common practice [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve probably seen sites like <a href="http://macheist.com/">MacHeist</a> and <a href="http://www.makeuseof.com/">Make Use Of</a> run promotions where you can unlock a software license or enter a drawing for an iPad by posting a message to Twitter or Facebook and then entering your email address into a form that magically appears after. It has also become an increasingly common practice for recording artists to give away free music downloads in exchange for Liking them on Facebook.</p>
<p>There&#8217;s probably sufficient documentation on how to implement this sort of thing with Twitter, so let&#8217;s try something a bit different. How about those new Google <a href="http://www.google.com/intl/en/webmasters/+1/button/index.html">+1 buttons</a>?</p>
<p>The +1 button has a convenient JavaScript callback that could be used for all sorts of fun things, such as un-hiding content on the page.</p>
<p>First, you need to put the button markup on your page, as well as the content you wish to hide until said button is clicked. The DIV holding the content should of course be hidden by default. Be sure to set the callback parameter of the button.</p>
<pre class="brush: xml;">
&lt;div id=&quot;plusonebutton&quot;&gt;
 &lt;g:plusone callback=&quot;plusone_callback&quot; href=&quot;http://www.webmaster-source.com&quot;&gt;&lt;/g:plusone&gt;
&lt;/div&gt;

&lt;div id=&quot;plusonecontent&quot; style=&quot;display:none&quot;&gt;
 &lt;p&gt;&lt;strong&gt;The user has clicked the +1 button.&lt;/strong&gt; Here's some content that was not previously visible. Lorem ipsum dolor sit amet.&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>Next, you want to include the +1 script and jQuery in your page header. After you&#8217;ve taken care of that, you need to write your callback function. The function fires when the button is clicked (assuming the name matches the one specified in the button parameter) and passes a JSON object containing the button state, among other things.</p>
<pre class="brush: jscript;">
&lt;script type=&quot;text/javascript&quot; src=&quot;https://apis.google.com/js/plusone.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
 function plusone_callback(response) {
 if (response.state == 'on') {
 $(&quot;#plusonecontent&quot;).fadeIn();
 $(&quot;#plusonebutton&quot;).fadeOut();
 }
 }
&lt;/script&gt;
</pre>
<p>The callback should check the JSON object to see whether the state is set to &#8220;on&#8221; rather than &#8220;off,&#8221; and replace the button with your hidden content if that is the case.</p>
<p>You can see <a href="http://www.webmaster-source.com/static/demos/plusonegate.php">a demo of the finished page here</a>.</p>
<p>I don&#8217;t know whether Google frowns upon this sort of thing or not, but it would be a great way to provide a surprise bonus (e.g. an ebook or song download) when someone likes one of your pages. So, just don&#8217;t use it for anything spammy or deceptive and you should probably be fine.</p>
<p><strong>Update:</strong> It has come to my attention that this could be used in many crappy ways, such as those irritating people who force you to Like a video on Facebook before they let you watch it. (Which is completely against the spirit of the concept, as well as social media.) Don&#8217;t do stuff like that. Seriously.</p>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4160&amp;md5=ba59162b2a9878fd5941ebb0f149a6ff" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2011/08/03/google-1-content-unlocker-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microjs: Micro-Frameworks and Micro-Libraries</title>
		<link>http://www.webmaster-source.com/2011/08/01/microjs-micro-frameworks-and-micro-libraries/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=microjs-micro-frameworks-and-micro-libraries</link>
		<comments>http://www.webmaster-source.com/2011/08/01/microjs-micro-frameworks-and-micro-libraries/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 11:38:08 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4064</guid>
		<description><![CDATA[Sometimes you don&#8217;t need a huge JavaScript library like jQuery or Prototype. Sometimes all you need is a small subset, like AJAX functions and templating. If a project doesn&#8217;t need all 31kb worth of jQuery, why don&#8217;t you try something a bit lighter? Microjs.com is a directory of &#8220;micro-libraries,&#8221; single-purpose JavaScript libraries with a small [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you don&#8217;t need a huge JavaScript library like jQuery or Prototype. Sometimes all you need is a small subset, like AJAX functions and templating. If a project doesn&#8217;t need all 31kb worth of jQuery, why don&#8217;t you try something a bit lighter?</p>
<p><a href="http://microjs.com/">Microjs.com</a> is a directory of &#8220;micro-libraries,&#8221; single-purpose JavaScript libraries with a small footprint. You can load up one or two for only a fraction of the file size of a larger framework. Need to make AJAX requests? Try <a href="http://code.google.com/p/microajax/">microajax</a>. Templating? Use <a href="http://twigkit.github.com/tempo/">Tempo</a>.</p>
<blockquote><p>Sure, we all love our favorite monolithic frameworks, and sometimes we   even use them fully. But how often do we reach for the ride-on John   Deere tractor with air conditioning and six-speaker sound system, when   a judiciously applied pocketknife would do the trick better, faster,   slicker?</p></blockquote>
 <p><a href="http://www.webmaster-source.com/?flattrss_redirect&amp;id=4064&amp;md5=383135b6e37254e6608e3552c9972ae8" title="Flattr" target="_blank"><img src="http://www.webmaster-source.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.webmaster-source.com/2011/08/01/microjs-micro-frameworks-and-micro-libraries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: www.webmaster-source.com @ 2012-02-09 14:23:11 by W3 Total Cache -->
