<?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>Stephen McIntyre</title>
	<atom:link href="http://stephenmcintyre.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenmcintyre.net</link>
	<description>Young Scottish Web Designer</description>
	<lastBuildDate>Mon, 08 Mar 2010 21:05:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create Social Bookmarks with the AddThis API</title>
		<link>http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/</link>
		<comments>http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 21:05:02 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[The Web]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=361</guid>
		<description><![CDATA[One service that I had used a while back was AddThis social bookmarks. I really liked its ease of use, support for a huge range of social networks and bookmarking sites, and most importantly it had great tools for analytics.
The social bar itself however left more to be desired. It was an eyesore, appearing after [...]]]></description>
			<content:encoded><![CDATA[<p>One service that I had used a while back was <a href="http://www.addthis.com">AddThis</a> social bookmarks. I really liked its ease of use, support for a huge range of <strong>social networks</strong> and <strong>bookmarking</strong> sites, and most importantly it had great tools for <strong>analytics</strong>.</p>
<p>The social bar itself however left more to be desired. It was an eyesore, appearing after every post on my site, and made me cringe at the fact that it <strong>relied on JavaScript</strong> to function. Luckily, I found an alternative in the form of their API docs.<span id="more-361"></span></p>
<p>You can see this for yourself in the <a href="#share">social bar</a> below this post. Check out the links as you hover over the images, they&#8217;re constructed exactly as I will show you here.</p>
<h3>Finding the Link</h3>
<p>The trick here is as simple as tweaking a URL.</p>
<p><code>http://api.addthis.com/oexchange/0.8/offer?url=<strong>URL</strong></code></p>
<p>This is the most basic option of the API. It sends us to a page in which we can <strong>choose</strong> where we want to share the link. It&#8217;s good if you have a simple &#8220;Share This&#8221; button, but not if we want different links to specific social networks. Plus, we won&#8217;t get that analytics data.</p>
<p>In the above example, just replace <code>URL</code> with your page&#8217;s location.</p>
<p><a href="http://api.addthis.com/oexchange/0.8/offer?url=http://stephenmcintyre.net">http://api.addthis.com/oexchange/0.8/offer?url=<strong>http://stephenmcintyre.net</strong></a></p>
<p>Now we&#8217;ll add a title and user name. For this we add another two URL parameters. Just replace URL, title and user name like before.</p>
<p><a href="http://api.addthis.com/oexchange/0.8/offer?url=http://stephenmcintyre.net&#038;title=Stephen+McIntyre&#038;username=StephenMcIntyre">http://api.addthis.com/oexchange/0.8/offer?url=<strong>http://stephenmcintyre.net</strong>&amp;title=<strong>Stephen+McIntyre</strong>&amp;username=<strong>StephenMcIntyre</strong></a></p>
<p>And lastly, where we want to specify where we are going to take the user we need to add <code>forward/SERVICE/</code> into our link.</p>
<p><a href="http://api.addthis.com/oexchange/0.8/forward/facebook/offer?url=http://stephenmcintyre.net&#038;title=Stephen+McIntyre&#038;username=StephenMcIntyre">http://api.addthis.com/oexchange/0.8/forward/<strong>facebook</strong>/offer?url=<strong>http://stephenmcintyre.net</strong>&amp;title=<strong>Stephen+McIntyre</strong>&amp;username=<strong>StephenMcIntyre</strong></a></p>
<p>The link above will take you straight to Facebook, formatting the data to easily post the page up onto your wall.</p>
<h3>A Simple Example</h3>
<p>So, for a full implementation of this API we can use a list of hyperlinks like in my social bar, and styled how you like them.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/twitter/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Twitter<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/facebook/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Facebook<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/digg/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Digg<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/delicious/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Delicious<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></td></tr></table></div>

<p>For a full list of service tags to use in the <code>SERVICE</code> section of the URL, check out the <a href="http://www.addthis.com/services/list">service codes</a> page on the AddThis docs. You can copy the short codes from each row, there&#8217;s loads to choose from.</p>
<p>More information on the <a href="http://www.addthis.com/help/sharing-api">sharing endpoints</a> can also be found on there, which will help if you&#8217;re looking to dig further into the API.</p>
<h3>One Last Thing</h3>
<p>So as you can see, the links can get pretty long. If you&#8217;re inputting these manually, even a copy-paste job can be tedious, especially if you plan to go about dotting them all over your website.</p>
<p>The ideal situation then would be to generate these automatically, which can be done through PHP with some <a href="http://php.net/urlencode">urlencode</a> magic.</p>
<p>Also, if you&#8217;re using WordPress, then fetching the link and title is easy with <code>get_permalink()</code> and <code>$post-&gt;post_title</code> respectively.</p>
<p>I&#8217;ll be releasing a tool that utilises the AddThis API very soon, and adding some tutorials on here as to how it&#8217;s done, including some clever PHP.</p>
<p>Stay posted. <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Parsing the Kongregate Badge Feed with PHP</title>
		<link>http://stephenmcintyre.net/blog/kongregate-badge-feed-php/</link>
		<comments>http://stephenmcintyre.net/blog/kongregate-badge-feed-php/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 22:38:29 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[The Web]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=350</guid>
		<description><![CDATA[Back in September of last year, Jim Greer of Kongregate.com posted an article in the forums about a new badge data service served in JSON. This was great news for developers, we could finally grab data from our user accounts without having to do a web scrape on our user page.
However, this forum post seems [...]]]></description>
			<content:encoded><![CDATA[<p>Back in September of last year, <a href="http://www.kongregate.com/accounts/jimgreer">Jim Greer of Kongregate.com</a> posted an article in the forums about a new badge data service served in JSON. This was great news for developers, we could finally grab data from our user accounts without having to do a <a href="http://wikipedia.org/wiki/Web_scraping">web scrape</a> on our user page.</p>
<p>However, this <a href="http://www.kongregate.com/forums/4-programming/topics/58167-new-json-feed-of-badge-info">forum post</a> seems to be the only official documentation on what is a very interesting data feed, so it is with this reason that I decided to create this beginner&#8217;s tutorial on how to use it.<span id="more-350"></span></p>
<p>Since Kongregate is a website that hosts Flash games, this data would primarily be used to collect data into such games &#8211; like showing user badges embedded into a Flash dashboard. In this tutorial, I will take a different approach and show you how to collect that data in PHP and build your own activity stream.</p>
<h3>All Badges Feed</h3>
<p>Located at <code>http://www.kongregate.com/badges.json</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Collect 100 squiggles and pwn 10 spiders&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;users_count&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">285194</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Running Start&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;games&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.kongregate.com/games/DrNeroCF/fancy-pants-adventure-world-2&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Fancy Pants Adventure: World 2&quot;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;icon_url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://cdn2.kongregate.com/badge_icons/0000/0367/world2easy.png?1242937128&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;created_at&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008/01/24 20:26:15 -0800&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;points&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;difficulty&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;easy&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">184</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is an example of an item from the list of game badges in the feed. It contains more than enough information to help make your own application. Each element is labeled so that you can index and call each item when you need to. Further down I&#8217;ll show you how to sort out the data in PHP.</p>
<h3>User Badges Feed</h3>
<p>Can be found at <code>http://www.kongregate.com/accounts/USERNAME/badges.json</code>, replacing USERNAME with your account login name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;badge_id&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">184</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;created_at&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008/06/23 06:20:42 -0700&quot;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A much simpler looking JSON string. A user&#8217;s badge feed only contains a list of badge IDs and the date and time that the user achieved them. All other data can be found on the global badge feed above, we just match up the IDs.</p>
<h3>Grabbing the Data</h3>
<p>Last week I showed you <a href="/blog/fetch-content-using-php/">how to fetch page content using PHP</a>. We can reuse the <code>get_page_content</code> method here to collect our information from Kongregate. Be sure to add this method above all other code in your PHP file, before adding the content from the box below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$all_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$all_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/accounts/USERNAME/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This sets up two arrays (<code>$all_badges</code> and <code>$user_badges</code>) that we can use to loop through and match IDs.</p>
<h3>Displaying the Content</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$badge</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'badge_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span>
        <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'games'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' Badge'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/a&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;span&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/span&gt;'</span><span style="color: #339933;">.</span>
        <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For each user badge in the <code>$user_badges</code> array we loop through our global badges to find a match and print an output of badge, link, and description to the page.</p>
<p>The break and unset in this code is useful for reducing memory usage on the script.</p>
<p>The break exits the current loop when the badges have been matched &#8211; since we won&#8217;t need to keep looking once we have found what we are looking for.</p>
<p>The unset removes the item from our list of all badges completely, since once it has been found once we won&#8217;t ever need to match it again since we can only have one of each badge.</p>
<h3>Finishing Up</h3>
<p>For a full copy-paste version of the code, feel free to skip to the end of this article, or view a <a href="/examples/kong-badges/">demo page</a> of this script.</p>
<p>Note: the demo page is a rendered example of the end result and does not update. For markup purposes only.</p>
<p>Also note that reading the JSON data directly from the server in this way is not recommended. For better use of this data, please store and manage the information within your own database setup.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$all_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$all_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://www.kongregate.com/accounts/USERNAME/badges.json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_badges</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ol&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$badge</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$all_badges</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'badge_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span>
        <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'games'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' Badge'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/a&gt;'</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;span&gt;'</span><span style="color: #339933;">.</span>
            <span style="color: #0000ff;">' : '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$global</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
          <span style="color: #0000ff;">'&lt;/span&gt;'</span><span style="color: #339933;">.</span>
        <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$badge</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ol&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/kongregate-badge-feed-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Fetch Page Content Using PHP</title>
		<link>http://stephenmcintyre.net/blog/fetch-content-using-php/</link>
		<comments>http://stephenmcintyre.net/blog/fetch-content-using-php/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 15:45:20 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=342</guid>
		<description><![CDATA[The web has so many useful services now, many offering their own API that you can draw data from and create your own mashups with. Websites like Twitter, Facebook, Flickr, and Kongregate are just a few.
Their data output is most commonly formatted in RSS (for feed readers), and JSON (a lighter form than XML used [...]]]></description>
			<content:encoded><![CDATA[<p>The web has so many useful services now, many offering their own API that you can draw data from and create your own <a href="http://wikipedia.org/wiki/Mashup_%28web_application_hybrid%29">mashups</a> with. Websites like Twitter, Facebook, Flickr, and Kongregate are just a few.</p>
<p>Their data output is most commonly formatted in <a href="http://wikipedia.org/wiki/RSS">RSS</a> (for feed readers), and <a href="http://wikipedia.org/wiki/JSON">JSON</a> (a lighter form than XML used a lot for AJAX). In this post I&#8217;ll show you how to use the <a href="http://php.net/curl">cURL</a> library to collect data from any public web page. <span id="more-342"></span></p>
<p>First, I&#8217;ve set up a function that will take in the URL you want to use as a parameter and returns the content of the page. This will make it easier later on, particularly if you want to call more than one page in a single script (like feeding Twitter and Facebook status updates into one stream).</p>
<h3>Making a Connection</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_page_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 2: </strong> Initialises a cURL connection so we can start using it.</p>
<p><strong>Line 4: </strong> Sets the URL.<br />
<strong>Line 5: </strong> Sets headers to false, so that we don&#8217;t collect extra header data that we won&#8217;t use.<br />
<strong>Line 6: </strong> Sets the return transfer to true. Without setting this, the script will output the content directly to the page, which we don&#8217;t want.</p>
<p><strong>Line 8: </strong> Execute the connection using the options from lines 4-6 and store it into a holding variable. If we hadn&#8217;t set our return transfer to true, this would return empty.</p>
<p><strong>Lines 10-12: </strong> Close connection to the URL and return the holding variable.</p>
<p>So, for example, we can collect JSON formatted data from a user&#8217;s latest Twitter updates:</p>
<h3>Decoding the Data</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$twitter_content</span> <span style="color: #339933;">=</span> get_page_content<span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;http://api.twitter.com/1/statuses/user_timeline.json?screen_name=_bigSteve&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$twitter_content</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Where <a href="http://php.net/json_decode">json_decode</a> with its second parameter as true puts our data into an associative array.</p>
<p>We can then display this data as we usually would, looping through the elements:</p>
<h3>Displaying the Text</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;ol&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">.</span>
    <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_COMPAT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
    <span style="color: #0000ff;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/ol&gt;&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The <a href="http://php.net/htmlspecialchars">htmlspecialchars</a> method in there is very useful for escaping special characters into their HTML entities, so should be used here since we are sending output to the web page.</p>
<p>After the text we want to output, I&#8217;ve found the other two parameters to be useful in most cases. You can change these if you get some strange characters.</p>
<p>So when you run the script on your own website, it will display an ordered list of Twitter updates onto the page. You can take the info above and convert it to use data from other APIs, using the <code>get_page_content</code> method we created in particular.</p>
<p>If you&#8217;re looking to parse XML documents like RSS feeds, then <a href="http://php.net/manual/book.simplexml.php">SimpleXML</a> is a great library for that. When given the option though, it&#8217;s almost always best to go with JSON, so have a poke around for it in the API docs before you set off.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/fetch-content-using-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Beginner&#8217;s Guide to Google AJAX Feed API</title>
		<link>http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/</link>
		<comments>http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 21:49:06 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=328</guid>
		<description><![CDATA[For those that don&#8217;t know, the Google AJAX Feed API is a JavaScript tool that you can use to handle your AJAX requests, and is particularly useful for reading in feed data from external websites. After searching for all kinds of plug-ins and tools, I landed on Google&#8217;s answer, and found exactly what I needed [...]]]></description>
			<content:encoded><![CDATA[<p>For those that don&#8217;t know, the <a href="http://code.google.com/apis/ajaxfeeds/">Google AJAX Feed API</a> is a JavaScript tool that you can use to handle your AJAX requests, and is particularly useful for reading in feed data from external websites. After searching for all kinds of plug-ins and tools, I landed on Google&#8217;s answer, and found exactly what I needed &#8211; a JavaScript-only feed parsing script.</p>
<p>This tutorial will provide you with a very basic script to start using this great tool and covers what you need to get started.</p>
<p><span id="more-328"></span></p>
<p>First off, check out the <a href="/examples/gaf-api/">Feed API demo</a> that you will be creating.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;ul id=&quot;container&quot;&gt;&lt;/ul&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/jsapi?key=YOUR_API_KEY&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> undefined<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> items <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;container&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> Initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> feed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">feeds</span>.<span style="color: #660066;">Feed</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://twitter.com/statuses/user_timeline/41298392.rss&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	feed.<span style="color: #660066;">setNumEntries</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	feed.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>result.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			items <span style="color: #339933;">=</span> result.<span style="color: #660066;">feed</span>.<span style="color: #660066;">entries</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;feeds&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
google.<span style="color: #660066;">setOnLoadCallback</span><span style="color: #009900;">&#40;</span>Initialize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> ipop <span style="color: #339933;">=</span> items.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;li&gt;'</span> <span style="color: #339933;">+</span> ipop.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
		container.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> text<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>If it seems overwhelming, don&#8217;t worry. I&#8217;ll go over it bit by bit, it&#8217;s really easy to pick up.</p>
<h3>Initialize</h3>
<p>This is a custom method that takes in some data using the code from the first <code>&lt;script&gt;</code> tag.</p>
<p>The first line inside of this function declaration stores feed data into the <code>feed</code> object. Setting the parameter in the <code>Feed</code> method call will change what RSS data you use.</p>
<p>Line two sets how many items you want it to display, and the final section tells the script what to do once the data has been loaded into the variable. In this case, we want it to check that there are no errors and store our list of entries into the <code>items</code> variable.</p>
<h3>load and setOnLoadCallback</h3>
<p>The <code>load</code> method tells the Google API that we want to use the Feed functions, so we give it the parameters &#8220;feed&#8221; and &#8220;1&#8243;. The &#8220;1&#8243; part is only used for collecting version numbers and isn&#8217;t too important.</p>
<p><code>setOnLoadCallback</code> calls our <code>Initialize</code> method from earlier and processes it.</p>
<h3>The fancy setInterval stuff</h3>
<p>This section is just for eye candy and isn&#8217;t actually essential to using the API. It&#8217;s used to make the feed items display a second after each other in a list.</p>
<p>To start off we check if the item can be used, by checking if it isn&#8217;t undefined. We then remove an item from our list of items with <code>pop</code> and store this into <code>ipop</code>. The <code>title</code> attribute of this is then used as the text for the list item and added to our <code>container</code> list.</p>
<h3>Finishing up</h3>
<p>There we go, hopefully as simple as I said it would be.</p>
<p>Just remember that before you use this script, you should sign up for an <a href="http://code.google.com/apis/ajaxfeeds/signup.html">API key</a> and replace YOUR_API_KEY in the script tag with the key you&#8217;re given.</p>
<p>What I really like about this script is that it&#8217;s all handled (on our end at least) by one HTML document, and Google&#8217;s speedy servers handle the rest of it for us.</p>
<p>This is not a service to be abused however, as it has huge possibilities and there are millions of ways that applications could be built using it. With the issue of bandwidth and server load costs affecting a lot of amateur developers with big ideas, this is one less thing to worry about.</p>
<h3>Note</h3>
<p>The code above may only work if you have a properly formatted HTML document (i.e., all the header, doctype and body stuff). If you do have problems, try copying the code from the demo&#8217;s page source.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using jQuery To Create Parallax Scrolling Backgrounds</title>
		<link>http://stephenmcintyre.net/blog/jquery-parallax-scrolling/</link>
		<comments>http://stephenmcintyre.net/blog/jquery-parallax-scrolling/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 22:43:00 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=309</guid>
		<description><![CDATA[Just a quick demonstration today on using JavaScript and CSS techniques to create scrolling backgrounds. Here we layer backgrounds on top of each other and change their horizontal position to move them past the viewing area. It also uses different multipliers to move the closer objects quicker, and create a parallax effect.


After you&#8217;ve checked out [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick demonstration today on using JavaScript and CSS techniques to create scrolling backgrounds. Here we layer backgrounds on top of each other and change their horizontal position to move them past the viewing area. It also uses different multipliers to move the closer objects quicker, and create a <strong>parallax</strong> effect.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/02/parallax.jpg"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/02/parallax-300x108.jpg" alt="" title="parallax" width="300" height="108" class="aligncenter size-medium wp-image-321" /></a></p>
<p><span id="more-309"></span></p>
<p>After you&#8217;ve checked out the <a href="/examples/parallax/">parallax scrolling demo</a> you can head back here to see how it&#8217;s written up.</p>
<p>I used jQuery for rapid prototyping, but naturally you can use regular JavaScript if you know the tricks to do so.</p>
<h3>The Styles</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#landscape</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#grass</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#verge</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#window</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">460px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">332px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #3333ff;">:repeat-</span>x<span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#landscape</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'landscape.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#grass</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'grass.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#verge</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'verge.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#window</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'window.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>The first section sets height, width, background position and background repeat of the <code>&lt;div&gt;</code> elements that hold our backgrounds.</p>
<p>The other ID-specific sections set the background image you want to use for each part.</p>
<p>All CSS can be tweaked to suit your own project or how you see fit.</p>
<h3>The Structure</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;landscape&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;grass&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;verge&quot;</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>All just divs inside divs here. Now, the interesting part.</p>
<h3>The Scripts</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> xAdd <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> scrollInterval <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		xAdd<span style="color: #339933;">++;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>xAdd <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">920</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			xAdd <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#landscape'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-position'</span><span style="color: #339933;">,</span>xAdd <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px 100%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#grass'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-position'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>xAdd <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px 100%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#verge'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-position'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>xAdd <span style="color: #339933;">*</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px 100%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>First line imports our jQuery file and does the usual running of it in the next script tag.</p>
<p>We then set <code>xAdd = 0</code> &#8211; this will act as a <strong>counter</strong> to add to each background&#8217;s <strong>x-position</strong> and move the backgrounds horizontally behind the window.</p>
<p>Use the <code>scrollInterval</code> variable and <code>setInterval</code> method to set a function that calls every 10ms here. It&#8217;s a type of <strong>timer</strong> that will increment our <code>xAdd</code> every time it is called (10ms).</p>
<p>The if statement is a sort of <strong>garbage collector</strong>, resetting our number to 0 if it&#8217;s more than or equal to 920 (the maximum width of our images). It won&#8217;t break if you remove it, but it stops the number becoming unnecessarily high. Always good to keep these things in mind while you code.</p>
<p>The following three lines with the $ signs at the beginning allow jQuery to <strong>select</strong> our three div elements with the backgrounds in them. It then uses the <code>.css()</code> method to change their background position and creates the scrolling effect you see, since this is called every time the interval is run. There are also some multipliers on the xAdd variable in each one as mentioned earlier to speed up the <strong>closest</strong> objects.</p>
<p>So yeah, have fun with it. Just a little idea I had. Perhaps you can make better use of it than a simulation of a train journey daydream.</p>
<p>I dunno, surprise me. Let&#8217;s see what can be made of parallax scrolling in web pages. Interested to try this in a page header&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/jquery-parallax-scrolling/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Open Windows in HTML with Standards Compliance</title>
		<link>http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/</link>
		<comments>http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 21:43:17 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=299</guid>
		<description><![CDATA[When opening new windows from your web page that link to external files, a common practice is to open them in a new window as not to deter the user from your site.
One method to do this in the past was to use target="_blank" that tells the browser to open the link&#8217;s href attribute in [...]]]></description>
			<content:encoded><![CDATA[<p>When opening new windows from your web page that link to external files, a common practice is to open them in a new window as not to deter the user from your site.</p>
<p>One method to do this in the past was to use <code>target="_blank"</code> that tells the browser to open the link&#8217;s <code>href</code> attribute in a new window. This, however, is deprecated in the Strict Doctype of <a href="http://w3.org">W3C web standards</a> so we should be trying to avoid using it whenever we can, and in this post I&#8217;ll show you exactly how it&#8217;s done.</p>
<p><span id="more-299"></span></p>
<p>Instead of using the <code>target</code> attribute, we can use <code>rel</code> to define the <code>&lt;a&gt;</code> tags in our HTML that we want to open in a new page. Since these are usually external pages, we will use the <code>external</code> keyword.</p>
<p><a href="/examples/replace-target-blank/">Raw JavaScript Example</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Open a &lt;a href=&quot;new-window.html&quot; rel=&quot;external&quot;&gt;New Window&lt;/a&gt; with W3C valid syntax
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> links <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'a'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> links.<span style="color: #660066;">length</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> rels <span style="color: #339933;">=</span> links<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">rel</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> rels.<span style="color: #660066;">length</span> <span style="color: #339933;">;</span> j<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> rels<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'external'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			links<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
				window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Line 1:</strong> This is where we give the link a <code>rel</code> attribute<br />
<strong>Lines 5-13:</strong> The script collects all <code>&lt;a&gt;</code> tags and loops through them, splitting up each space separated term and checking if they equal &#8220;<code>external</code>&#8221;<br />
<strong>Lines 15-17:</strong> Sets the <code>onclick</code> attribute of the link to allow <code>window.open</code> to run and open a new window when the user clicks, containing the <code>href</code> value.<br />
<strong>Line 19:</strong> Returns <code>false</code> to override the link&#8217;s default function, stopping it from opening a page of its own in the same window.</p>
<p>This can be run on any page that you have set your external links on and acts as a nicer, search-bot and standards friendly alternative.</p>
<p><strong>jQuery Version</strong></p>
<p>If your page is already using <a href="http://jquery.com">jQuery</a> there is an even easier way to implement the code from above.</p>
<p><a href="/examples/replace-target-blank/jquery-alt.html">jQuery Example</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Open a &lt;a href=&quot;new-window.html&quot; rel=&quot;external&quot;&gt;New Window&lt;/a&gt; with W3C valid syntax
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'a[rel~=external]'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Much simpler.</p>
<p>I won&#8217;t go through the code on the jQuery example, but if you use the library, you should understand the selectors and methods I&#8217;ve used above.</p>
<p>The clever part is on line 7, where the selector finds all <code>&lt;a&gt;</code> tags with a <code>rel</code> attribute containing the &#8220;<code>external</code>&#8221; keyword, even within a space-separated list of words.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Let Your Database Do The Work For You</title>
		<link>http://stephenmcintyre.net/blog/let-your-database-do-it/</link>
		<comments>http://stephenmcintyre.net/blog/let-your-database-do-it/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 17:00:14 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=149</guid>
		<description><![CDATA[In the below examples I&#8217;ll use data from a fictional database table named &#8220;users&#8221;. We&#8217;re looking to return the 10 most recently added users with a date of birth later than January 1st 1970.

Bad practice
One way I&#8217;ve seen it done before is like this:

$query = mysql_query&#40; &#34;SELECT * FROM `users`&#34; &#41;;
while&#40; $row = mysql_fetch_assoc&#40; $query [...]]]></description>
			<content:encoded><![CDATA[<p>In the below examples I&#8217;ll use data from a fictional database table named &#8220;users&#8221;. We&#8217;re looking to return the 10 most recently added users with a date of birth later than January 1st 1970.<br />
<span id="more-149"></span></p>
<h2>Bad practice</h2>
<p>One way I&#8217;ve seen it done before is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;SELECT * FROM `users`&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dob</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$joined</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$dob</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$joined</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">krsort</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> SORT_NUMERIC <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Name: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Join Date: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Date of Birth: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Good practice</h2>
<p>The alternative uses some SQL techniques to do all of that data sorting within the database query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dob_limit</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;SELECT `name`,`joined`,`dob` FROM `users` WHERE `dob` &amp;gt;= <span style="color: #006699; font-weight: bold;">$dob_limit</span> ORDER BY `joined` DESC LIMIT 10&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Name: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Join Date: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'joined'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Date of Birth: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dob'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, the latter script should look far simpler, is easier to write, and works much quicker and efficiently.</p>
<p>Note also that in the second script the fields after the <code>SELECT</code> statement have been <strong>specified</strong> rather than leaving a wild-card asterisk (<code>*</code>). The asterisk returns <strong>all</strong> data in the row that the SQL statement found a match in, and the more data we have in a row the more data could become redundant as we extract it.</p>
<p>For example, consider that the <code>users</code> table had extra fields containing first name, surname, website URL, small personal biography, home town, favourite film, favourite food, and so on. For each row you extract, all seven of those become <strong>unused</strong> pieces of data in the memory on your server and possibly even the computers of your users. Multiply that seven by each row in your database, then by how many times the query will be called, and things start to add up pretty quickly.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2009/12/db-users.jpg"><img class="aligncenter size-full wp-image-224" title="db-users" src="http://stephenmcintyre.net/wp-content/uploads/2009/12/db-users.jpg" alt="" width="300" height="201" /></a></p>
<p>The point is then that if you start with a <strong>clear idea</strong> on what you actually need in order to use your script, you can be specific in what data you need to collect from your database. Doing so decreases your page load times and lowers your bandwidth, usually with dramatic results.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/let-your-database-do-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto-Post Your Blog to Twitter and Facebook</title>
		<link>http://stephenmcintyre.net/blog/auto-post-your-blog-to-twitter-and-facebook/</link>
		<comments>http://stephenmcintyre.net/blog/auto-post-your-blog-to-twitter-and-facebook/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 09:00:34 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[The Web]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=270</guid>
		<description><![CDATA[Okay, so you&#8217;ve gone through those guides on setting up your new weblog, and followed all the advice on using social media to leverage your site into becoming über-successful overnight. The problem now is that those two elements are still detached. You&#8217;re posting all that relevant content on your website but nobody is around to [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so you&#8217;ve gone through those guides on setting up your new weblog, and followed all the advice on using social media to leverage your site into becoming über-successful overnight. The problem now is that those two elements are still <strong>detached</strong>. You&#8217;re posting all that relevant content on your website but nobody is around to click those links that direct to all that beautiful material.</p>
<p>This is not an article on improving your follower count or the &#8220;top 10 ways to drive traffic with <a href="http://wikipedia.org/wiki/Search_engine_optimization">SEO</a>&#8220;, but it will definitely help in a different way. Today, I&#8217;ll show you how to <strong>automatically</strong> post your blog articles straight to your Twitter and Facebook accounts using your <a href="http://wikipedia.org/wiki/RSS">RSS</a> feed as a stream.</p>
<p>For those of you writing <a href="http://search.twitter.com/search?q=%23daily365">#daily365</a> articles, or have signed up for <a href="http://project52.info">Project52</a>, this will be ideal. Let the computers do the work for you.</p>
<p>The service I use for this is called <a href="http://twitterfeed.com">TwitterFeed</a>, which you&#8217;ll need an account for. It&#8217;s all <strong>super easy</strong> and I&#8217;ll explain it after the jump. So anyway, here we go.<span id="more-270"></span></p>
<h3>Logging In</h3>
<p>If you like, you can sign up to their service with the usual form by clicking &#8220;sign up&#8221; at the top of the page. If you&#8217;re already signed in to Google, Blogger, WordPress.com, or OpenID though it&#8217;s as simple as clicking the picture you like the look of.</p>
<p>Find the &#8220;Sign In with OpenID&#8221; at the bottom of the homepage near the big &#8220;Login&#8221; button if you&#8217;re doing it this way. The box below will show up (with some other options for accounts) and run some fancy AJAX.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_1.png"><img class="aligncenter size-full wp-image-272" title="twfeed_1" src="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_1.png" alt="" width="300" height="205" /></a></p>
<h3>Setting Up</h3>
<p>Once that&#8217;s done your dashboard will show up with a page like this.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_2.png"><img class="aligncenter size-full wp-image-274" title="twfeed_2" src="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_2.png" alt="" width="530" height="273" /></a></p>
<p><strong>Feed Name:</strong> If you&#8217;re only using one blog this won&#8217;t matter as it&#8217;s only to tell the difference between the feeds you&#8217;re using within your own account. Others won&#8217;t see this.</p>
<p><strong>RSS Feed URL:</strong> For example, <a href="http://stephenmcintyre.net/feed">http://stephenmcintyre.net/feed</a>. This will be different for your own setup.</p>
<p>If you use a WordPress.com blog, the URL will be</p>
<pre>http://BLOGNAME.wordpress.com/feed</pre>
<p>Or if you use Blogger</p>
<pre>http://BLOGNAME.blogspot.com/feeds/posts/default?alt=rss</pre>
<p>Make sure to use the feed testing button before you continue.</p>
<p><strong>Active:</strong> Keep this ticked for now, but it&#8217;s a switch that allows you to stop the service reading it later on.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_3.png"><img class="aligncenter size-full wp-image-276" title="twfeed_3" src="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_3.png" alt="" width="530" height="351" /></a></p>
<p><strong>Update Frequency:</strong> Even if you don&#8217;t post 5 times every 30 minutes, it&#8217;s probably best you set it to this, at least the half-hour part. It means that your posts will be sent to your social networks within half an hour of them being posted, keeping your friends and followers up to date.</p>
<p><strong>Post Content:</strong> I chose to just post the title, but it&#8217;s up to you. Description adds in an excerpt from the beginning of your article.</p>
<p><strong>Post Link:</strong> Something you definitely want to set up. Any service will do, but with <a href="http://bit.ly">bit.ly</a> you can generate analytics on the click-throughs and track its retweets. Great tool. You can use your username and API key here from your bit.ly account.</p>
<p><strong>Post Sorting:</strong> &#8220;pubDate&#8221; puts your latest posts first and &#8220;guid&#8221; sorts them alphabetically. You want to use pubDate for this.</p>
<p><strong>Post Suffix and Prefix:</strong> Statements to add to the start and end of your tweets and shares. Makes it easier for your readers to tell the difference between your blog posts and other links.</p>
<p><strong>Keyword Filter:</strong> Haven&#8217;t needed to exclude any posts from being sent so never used this. From what I understand you put pre-defined keywords into your posts to allow them to be sent by TwitterFeed. Handy if you have multiple types of content being fed into one RSS feed.</p>
<p>When you&#8217;re done you can click the &#8220;Continue to Step 2&#8243; button.</p>
<h3>Sync Up</h3>
<p>On the next page you can choose the accounts that you want to allow access to. In this example I&#8217;ll use Twitter, but others are just as simple.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_4.png"><img class="aligncenter size-full wp-image-279" title="twfeed_4" src="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_4.png" alt="" width="530" height="261" /></a></p>
<p>Using their OAuth service, connecting to Twitter is as simple as clicking the blue button above, which prompts this screen.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_6.png"><img class="aligncenter size-full wp-image-281" title="twfeed_6" src="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_6.png" alt="" width="540" height="259" /></a></p>
<p>If a login screen comes up, you can enter your details from there. Otherwise, clicking on the green &#8220;Allow&#8221; button will synchronise both accounts for you, taking you back to the previous menu.</p>
<p>If you&#8217;re using Twitter for driving a Google Analytics campaign, you can type some info in the UTM tags to track it. Otherwise you can skip it and click &#8220;Create Service&#8221;.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_5.png"><img class="aligncenter size-full wp-image-280" title="twfeed_5" src="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_5.png" alt="" width="530" height="176" /></a></p>
<h3>Done</h3>
<p>So there you go, everything&#8217;s set. Now you can sit back and watch those viewers flock to your site, now with no effort on your part!*</p>
<p><a style="text-decoration: none;" href="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_7.png"><img class="aligncenter size-full wp-image-282" title="twfeed_7" src="http://stephenmcintyre.net/wp-content/uploads/2010/01/twfeed_7.png" alt="" width="540" height="225" /></a></p>
<p>*Well you&#8217;ve still to write that content, mind</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/auto-post-your-blog-to-twitter-and-facebook/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The New Improved Tweet Cloud And Why You Should Use It</title>
		<link>http://stephenmcintyre.net/my-work/new-tweet-cloud/</link>
		<comments>http://stephenmcintyre.net/my-work/new-tweet-cloud/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 16:00:29 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[My Work]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=235</guid>
		<description><![CDATA[Actually, this one&#8217;s long overdue. My Tweet Cloud plugin for WordPress has recently passed over the 500 download mark and, as of last week, is now on its fifth release (v 1.4).
It&#8217;s had some significant improvements over its lifetime, and has quite frankly surprised me at how popular it has become. Especially from a script [...]]]></description>
			<content:encoded><![CDATA[<p>Actually, this one&#8217;s long overdue. My <a href="http://wordpress.org/extend/plugins/tweet-cloud/">Tweet Cloud plugin</a> for WordPress has recently passed over the <strong>500</strong> download mark and, as of last week, is now on its <strong>fifth</strong> release (v 1.4).</p>
<p>It&#8217;s had some significant improvements over its lifetime, and has quite frankly surprised me at how popular it has become. Especially from a script <a href="http://stephenmcintyre.net/my-work/tweet-cloud-my-wordpress-plugin/">written in a night</a> with nothing more than a simple idea behind it.<span id="more-235"></span></p>
<h3>The Tweaks</h3>
<p>Having been using the Tweet Cloud on my own blog since its first release I&#8217;ve been able to actively check how it shapes up and changed anything that seemed iffy in the later releases. It&#8217;s a great way to properly get to grips with these as you end up becoming an <strong>end-user</strong> as well as a programmer, solving problems from both sides.</p>
<p>I played about with it and gave it more options and a cleverer way of handling words. Word linking was added to improve design and usability, and the plugin was simplified to make it easier for developers like you to add to your sidebar.</p>
<p>I don&#8217;t actually know that you&#8217;re a developer, but I&#8217;m settling with it as a safe assumption since you&#8217;re reading the Tweaks section.</p>
<p>As usual, you can find all <strong>release notes</strong> and such over at the plugin&#8217;s <a href="http://dev.stephenmcintyre.net/tweet-cloud/">development page</a>.</p>
<h3>Where It&#8217;s Heading</h3>
<p>Unfortunately the plugin isn&#8217;t widgetised and doesn&#8217;t use WordPress hooks for any kind of database stuff yet, so that&#8217;s definitely<strong> a direction I&#8217;d like it to go</strong>. Particularly since I was emailed by blogger <a href="http://operamouth.wordpress.com/">Amy Armstrong</a> on how to make it work on her WordPress.com-hosted pages. The problem is, the site does not allow you to edit any of your theme code, so she couldn&#8217;t get into the script to change it. Widgetising would definitely open this up to the people that want it, and who am I to deny you lovely people that privilege?</p>
<p>I also had an interesting conversation with <a href="http://www.petrilopia.net/">Petri Lopia</a> via email, who found problems with the plugin&#8217;s text parsing. He also suggested some of the things that were developed in the latest release, like support for hashtags, mentions, and excluded words. Luckily though they were already in the development pipeline and I could work on them quite easily. It&#8217;s great to know that the users of this plugin follow my line of thought.</p>
<p>Cheers, Amy and Petri.</p>
<h3><strong>I Leave You With This</strong></h3>
<p>The great thing about an open-source project like this is exactly that &#8211; <strong>it&#8217;s open</strong>. If you like the look of it, join myself and the 500 others and give it a go. If you want to know how it works, have a poke around with the code. Find anything weird or interesting or generally just want to fire some questions out? Post a comment or send an email &#8211; it only takes a <strong>minute</strong> to write one, and could save you <strong>hours</strong> of headaches. Trust me, it happens to me too often.</p>
<p>So be like Amy and Petri. Become a part of the web development community and <strong>get involved</strong>. You&#8217;ll be glad you did.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/my-work/new-tweet-cloud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The PHP Include</title>
		<link>http://stephenmcintyre.net/blog/the-php-include/</link>
		<comments>http://stephenmcintyre.net/blog/the-php-include/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 11:00:05 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=158</guid>
		<description><![CDATA[At the point that your site becomes more complex, you look to ways of simplifying your page code to give yourself less headaches. Ultimately you&#8217;re looking for a way to separate that header that you&#8217;ve copy-pasted so many times to different static web pages, into a page that all your other pages can call, and [...]]]></description>
			<content:encoded><![CDATA[<p>At the point that your site becomes more complex, you look to ways of simplifying your page code to give yourself less headaches. Ultimately you&#8217;re looking for a way to separate that header that you&#8217;ve copy-pasted so many times to different static web pages, into a page that all your other pages can call, and will stay the same.</p>
<p>If like me you hadn&#8217;t heard about PHP until this point where you are looking to add in other scripts to your web pages, then now is a very good time to learn about PHP includes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><span id="more-158"></span></p>
<p>One thing to note before I begin is that if you&#8217;re ever told to use a <code>frame</code> or <code>iframe</code> tag for this kind of thing, don&#8217;t. There&#8217;s plenty of articles on why this is, but the main reasons I don&#8217;t use them is that it doesn&#8217;t play nicely with the other scripts on the page (like JavaScript and CSS) and are a pain for error checking in web development.</p>
<p>PHP displays the page as if it had been generated from a single HTML script, and this after all is what we&#8217;re looking for.</p>
<p>Using a PHP include is as simple as pasting in the code before the jump. All you do is replace the &#8216;header.html&#8217; with a relative path to the file you want added in.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'path/to/menu.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'../footer.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Just remember to enclose them in the standard <code>&lt;?php</code> and <code>?&gt;</code> tags. Oh yeah, and you have to save whatever files that use them with a &#8216;.php&#8217; extension instead of &#8216;.html&#8217; so that it can read those special tags and run the code inside them.</p>
<p><strong>File: index.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;container&quot;&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'path/to/menu.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;p&gt;
  Some dummy content text goes here to fill space.
&lt;/p&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'../footer.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;/div&gt;</pre></td></tr></table></div>

<p><strong>File: header.html</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>My First PHP Page<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Hello World<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span></pre></td></tr></table></div>

<p><strong>File: menu.html</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Gallery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></td></tr></table></div>

<p><strong>File: footer.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;p&gt;
  &lt;em&gt;
    Generated on 
    <span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'jS M Y \a\t g:i a'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;/em&gt;
&lt;/p&gt;</pre></td></tr></table></div>

<p>In &#8216;footer.php&#8217;, <a href="http://php.net/date">date</a> is a PHP method like <code>include</code> that generates a string of text from the time that you run it. The fancy letters in the brackets just make it return the right information you want it to.</p>
<p>The <a href="http://php.net/echo">echo</a> part takes the info from <code>date()</code> and writes it to the screen, so your end result is something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">Generated on 13th Feb 2009 at 23:31</pre></div></div>

<p>See it in action by <a href="/examples/php-include/files/index.php">viewing the full page</a>.</p>
<p><strong>Further reading</strong></p>
<p>As you can see, when used wisely PHP includes can be very useful and efficient for your web development and helps organise all your files. Other methods you can use are <code>require</code>, <code>include_once</code>, and <code>require_once</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'header.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'path/to/menu.html'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'../footer.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>include:</strong> Searches for the file in brackets. If found, adds data into script, otherwise ignores and continues.</p>
<p><strong>require:</strong> Similar to include, but if no file is found exits the script and displays an error. Useful for adding in files necessary to how your script will run.</p>
<p><strong>include_once and require_once:</strong> Will only add data in once. If there are multiple calls of these methods to the same file in one script, only the first call will be used. Useful for things like <a href="http://php.net/manual/en/language.oop5.php">PHP classes</a>.</p>
<p>These are used and written in almost exactly the same way as the <code>include</code> examples. At the time you need to consider using one of these, you will be able to tell from the list above which one will best suit your project.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/the-php-include/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
