<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" version="2.0">

<channel>
	<title>ProgTuts</title>
	
	<link>http://progtuts.info</link>
	<description>High Quality Tutorials for Programmers</description>
	<pubDate>Fri, 01 May 2009 23:19:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/progtuts" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">progtuts</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Javascript: JQuery Flip</title>
		<link>http://progtuts.info/241/javascript-jquery-flip/</link>
		<comments>http://progtuts.info/241/javascript-jquery-flip/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 23:08:32 +0000</pubDate>
		<dc:creator>jamie</dc:creator>
		
		<category><![CDATA[AJAX Tutorials]]></category>

		<category><![CDATA[HTML Tutorials]]></category>

		<category><![CDATA[Javascript Tutorials]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=241</guid>
		<description><![CDATA[Last week I started a series of tutorials about using some javascript tools in your website and I’m continuing this week with Flip, a jQuery plugin that gives the effect of a piece of paper flipping to show alternative content on the other side.

What will we use it for?
It’s quite neat as it can be [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I started a series of tutorials about using some javascript tools in your website and I’m continuing this week with <a href="http://lab.smashup.it/flip/">Flip</a>, a jQuery plugin that gives the effect of a piece of paper flipping to show alternative content on the other side.<br />
<span id="more-241"></span></p>
<h2>What will we use it for?</h2>
<p>It’s quite neat as it can be used for a whole variety of tasks – anything that you would like to change the content of with a neat transition.<br />
For this tutorial though I’m going to use it to make an effect that I thought of a while ago – to deal with responses from an AJAX form submission.</p>
<h2>How to do it</h2>
<p>Unlike most of my other tutorials on ProgTuts I’m going to use <a href="http://jquery.com/">jQuery</a> to handle the AJAX side of things because we need to have that package included to use the Flip extension anyway.  The form I’m going to use is going to be really very simple – it will just send a name and age and the corresponding action page will just display them.  Of course, this tutorial isn’t about making an AJAX form; it’s about making the Flip style the transaction between form and action page.</p>
<p>There are a variety of procedures to put together to make a nice effect but the one I’m going to go with is this:<br />
Send Form -> Show Loading -> FLIP -> Show results</p>
<p>For a more detailed form that would need more time in the action page (such as a login form) I would probably do a flip between sending the form and showing the loading image – to avoid a messy set of transitions call the flip to the loading image before sending the AJAX call.</p>
<h2>What code is needed?</h2>
<p>Not all that much!<br />
First we need to make sure all our javascript is loaded, as well as our stylesheet.  The stylesheet is pretty flexible but can be easily messed up so I’ll mention that a wee bit later, but for the moment we’ll just link it all in the head section of the page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; title=&quot;Style&quot;&gt;
&lt;script src=”http://www.google.com/jsapi” /&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
  // Load jQuery
  google.load(&quot;jquery&quot;, &quot;1&quot;);	
&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-ui.js” /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.flip.js” /&gt;</pre></div></div>

<p>Note here that I’m using the Google javascript API to load jQuery – this is an incredibly useful tool so I recommend you read into it and use it!  It’s also important to make sure the jquery.flip file is linked last.</p>
<p>Next, in the body section of the page we need our code for the form; as I said I’m just going to make a very simple form and just set the action attribute to call a javascript function.  We will store this inside a div which we will call flipBox – this is the div that will actually be flipped by the jQuery plugin.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;flipBox&quot; class=&quot;flipBox&quot;&gt;
	&lt;form id=&quot;flip&quot; class=&quot;flip&quot; name=&quot;flip&quot; action=&quot;javascript: send();&quot;&gt;
		&lt;label for=&quot;name&quot;&gt;Name: &lt;/label&gt;&lt;br /&gt;
		&lt;input type=&quot;text&quot; id=&quot;name&quot; class=&quot;textbox&quot; name=&quot;name&quot; /&gt;&lt;br /&gt;
		&lt;label for=&quot;age&quot;&gt;Age: &lt;/label&gt;&lt;br /&gt;
		&lt;input type=&quot;text&quot; id=&quot;age&quot; class=&quot;textbox&quot; name=&quot;age&quot; /&gt;&lt;br /&gt;
		&lt;input type=&quot;submit&quot; id=&quot;submit&quot; name=&quot;submit&quot; class=&quot;button&quot; value=&quot;Send&quot; /&gt;
	&lt;/form&gt;
&lt;/div&gt;</pre></div></div>

<p>The last thing we need on this page is the actual javascript!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><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;">function</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
			url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;action.php&quot;</span><span style="color: #339933;">,</span>
			data<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#flip&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			beforeSend<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>
				<span style="color: #006600; font-style: italic;">// Display something when the AJAX is loading.  This could be an image or just ext...</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Loading...&quot;</span>;
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> response <span style="color: #339933;">=</span> html || <span style="color: #3366CC;">&quot;No response text&quot;</span>;					
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#flipBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">flip</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
					direction<span style="color: #339933;">:</span> <span style="color: #3366CC;">'lr'</span><span style="color: #339933;">,</span>
					color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#00FF33'</span><span style="color: #339933;">,</span>
					speed<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span>
					content<span style="color: #339933;">:</span> response
				<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		        error<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>
				<span style="color: #006600; font-style: italic;">// Display something when the AJAX fails.  This could be an image or just text...</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#flipBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">flip</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> direction<span style="color: #339933;">:</span> <span style="color: #3366CC;">'tb'</span><span style="color: #339933;">,</span> color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#FF0000'</span><span style="color: #339933;">,</span> content<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Failed to load page correctly!&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; 
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The code here is fairly standard for a javascript library ajax call so I won’t go into too much detail, but the important part comes in the success section:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#flipBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">flip</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	direction<span style="color: #339933;">:</span> <span style="color: #3366CC;">'lr'</span><span style="color: #339933;">,</span>
	color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#00FF33'</span><span style="color: #339933;">,</span>
	speed<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span>
	content<span style="color: #339933;">:</span> response
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This is the section that is needed to actually flip our div.<br />
It’s quite straightforward – “#flipBox” is our div containing the form, so we’re calling the flip() function on it and giving that function a few arguments:</p>
<ul>
<li>direction: ‘lr’ – flip from left to right (alternatives are rl, tb, bt…try no prizes for working out what they mean…)</li>
<li>color: ‘#00FF33’ – change the colour of the div to the given colour.  This is a nice was to show success or failure and just a general status change.</li>
<li>speed: 400 – specifies how many milliseconds it takes to carry out the flip.</li>
<li>content: response – sets the content of the div to the response variable we made the line above this section.</li>
</ul>
<p>You can see that I&#8217;ve also made a different style of flip if there is an error in the AJAX call.<br />
And that’s pretty much it.  Easy!<br />
There are some other attributes etc. available which you can explore on the Flip homepage but those are the most important imho.</p>
<p>The final thing we need to do is make our page for the AJAX function to call – some simple PHP for that one:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">?&gt;</span>
		The name supplied is: <span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'name'</span><span style="color: #009900;">&#93;</span>;?<span style="color: #339933;">&gt;</span>&lt;br /&gt;
		The age supplied is: <span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'age'</span><span style="color: #009900;">&#93;</span>;?<span style="color: #339933;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">?&gt;</span>
		You didn't fill in the form....
		<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Obviously not complex, but neither is this example <img src='http://progtuts.info/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h2>Styling</h2>
<p>As promised, I’ll try and describe a little bit about the styling.<br />
The code I’ve used is this:</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #cc00cc;">#flipBox</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;">500px</span>;
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span>;
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> 0 <span style="color: #993333;">auto</span>;
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#999999</span>;
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span>;
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span>;
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
	<span style="color: #cc00cc;">#flipBox</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">15px</span>;
	<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>In real terms you won’t need much more than that.  I haven’t tried it with floated layouts but I would guess it doesn’t work – maybe if anyone tries it they can post a comment up telling me of it’s success or failure.<br />
The one thing that I’ve found doesn’t work is the min-height or min-width attributes – this messes up the transition of it flipping so I recommend you avoid it.</p>
<p>I’ll add anything I find about the styling to this section, but as I said before it is pretty flexible, just try to avoid restricting sizes and it should work perfectly.</p>
<h2>Conclusion</h2>
<p>Dynamic changes are commonplace in websites these days, so make them visually effective!  The only thing I’d say is that personally I’d hate it if my entire webpage flipped.  It might be appealing the first 2 or 3 times but would get old fast!  Try to only do it with small sections which are changing on user input (such as a form!).  Also try and keep the speed pretty fast; I don’t like waiting and neither do most of your site visitors.<br />
For such a simple little piece of additional code, using the jQuery Flip plugin really is a nice way of adding a little extra to your forms.</p>
<p>As always, leave any comments and I’ll try and reply and if you have any discussion or whatever, head on over to the forum and share your thoughts and skills.</p>
<p><a class="demobutton" target="_blank" href="http://progtuts.info/downloads/JQueryFlip/">View the Demo</a> <a href="http://progtuts.info/wp-content/plugins/download-monitor/download.php?id=13" class="demobutton">Download the ZIP file</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/211/ajax-and-google-integrated-search/" title="AJAX and Google Integrated Search">AJAX and Google Integrated Search</a></li>
<li><a href="http://progtuts.info/232/javascript-jquery-tabs/" title="Javascript: JQuery Tabs">Javascript: JQuery Tabs</a></li>
<li><a href="http://progtuts.info/186/create-a-parallax-website-header/" title="Create a Parallax Website Header">Create a Parallax Website Header</a></li>
<li><a href="http://progtuts.info/142/ajax-image-gallery/" title="AJAX Image Gallery">AJAX Image Gallery</a></li>
<li><a href="http://progtuts.info/128/creating-a-cms-website-layout-using-ext-js/" title="Creating a CMS Website Layout using Ext JS">Creating a CMS Website Layout using Ext JS</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=7iMIQXk1xY4:ZwO2VezIZ_g:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=7iMIQXk1xY4:ZwO2VezIZ_g:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=7iMIQXk1xY4:ZwO2VezIZ_g:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=7iMIQXk1xY4:ZwO2VezIZ_g:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=7iMIQXk1xY4:ZwO2VezIZ_g:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=7iMIQXk1xY4:ZwO2VezIZ_g:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=7iMIQXk1xY4:ZwO2VezIZ_g:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=7iMIQXk1xY4:ZwO2VezIZ_g:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/241/javascript-jquery-flip/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Plain Text Feedburner (Google Version) Subscriber Count [Updated]</title>
		<link>http://progtuts.info/237/plain-text-feedburner-subscriber-count/</link>
		<comments>http://progtuts.info/237/plain-text-feedburner-subscriber-count/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 11:21:46 +0000</pubDate>
		<dc:creator>gilbitron</dc:creator>
		
		<category><![CDATA[PHP Tutorials]]></category>

		<category><![CDATA[curl]]></category>

		<category><![CDATA[feedburner]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=237</guid>
		<description><![CDATA[This is a short tutorial explaining how to get the raw number of subscribers from your Feedburner feed to display on your web page.

Intoduction
To do this simple trick we are going to use three things:

Feedburner Awareness API (which you don&#8217;t need to activate for this)
cURL (should be enabled on most servers)
PHP 5 (to read the [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short tutorial explaining how to get the raw number of subscribers from your <a href="http://www.feedburner.com/fb/a/home">Feedburner</a> feed to display on your web page.<br />
<span id="more-237"></span></p>
<h2>Intoduction</h2>
<p>To do this simple trick we are going to use three things:</p>
<ul>
<li><a href="http://www.feedburner.com/fb/a/home">Feedburner</a> Awareness API (which you don&#8217;t need to activate for this)</li>
<li><a href="http://curl.haxx.se/">cURL</a> (should be enabled on most servers)</li>
<li><a href="http://uk2.php.net/manual/en/function.simplexml-element-construct.php">PHP 5</a> (to read the XML we need a PHP5+ function)</li>
</ul>
<p>I&#8217;m assuming here you have a Feedburner account with an active feed.</p>
<h2>The PHP</h2>
<p>So onto the php.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$feeduri</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;progtuts&quot;</span>; <span style="color: #666666; font-style: italic;">//The bit that comes after http://feeds.feedburner.com/</span>
<span style="color: #666666; font-style: italic;">//This is the old Feedburner url</span>
<span style="color: #666666; font-style: italic;">//$url = &quot;http://api.feedburner.com/awareness/1.0/GetFeedData?uri=&quot;. $feeduri;</span>
<span style="color: #666666; font-style: italic;">//This is the new Google Feedburner url</span>
<span style="color: #000088;">$url</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=&quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$feeduri</span>;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> curl_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">//Use cURL</span>
curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">//Return data rather then echo it</span>
curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</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: #666666; font-style: italic;">//Pass in our url</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> curl_exec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span>;
curl_close<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">//Read the returned XML</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entry</span><span style="color: #009900;">&#91;</span><span style="">'circulation'</span><span style="color: #009900;">&#93;</span>; <span style="color: #666666; font-style: italic;">//Get our subscriber count</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Thats it. Very simple. All we are doing here is using cURL to retrieve the XML output by the Feedburner API and convert it into a php variable. You can now use $count however you want.</p>
<h2>Conclusion</h2>
<p>To see an example of this view the demo below. I hope you find this little trick useful.</p>
<p><a href="http://progtuts.info/downloads/FeedburnerCount/" target="_blank" class="demobutton">View the Demo</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/211/ajax-and-google-integrated-search/" title="AJAX and Google Integrated Search">AJAX and Google Integrated Search</a></li>
<li><a href="http://progtuts.info/178/php-automatically-create-thumbnail-images/" title="PHP Automatically Create Thumbnail Images">PHP Automatically Create Thumbnail Images</a></li>
<li><a href="http://progtuts.info/142/ajax-image-gallery/" title="AJAX Image Gallery">AJAX Image Gallery</a></li>
<li><a href="http://progtuts.info/92/login-system/" title="PHP Login System">PHP Login System</a></li>
<li><a href="http://progtuts.info/55/php-optimization-tips/" title="PHP Optimization Tips">PHP Optimization Tips</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=wEBoV0JBUIk:kpaGRJ48FF8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=wEBoV0JBUIk:kpaGRJ48FF8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=wEBoV0JBUIk:kpaGRJ48FF8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=wEBoV0JBUIk:kpaGRJ48FF8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=wEBoV0JBUIk:kpaGRJ48FF8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=wEBoV0JBUIk:kpaGRJ48FF8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=wEBoV0JBUIk:kpaGRJ48FF8:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=wEBoV0JBUIk:kpaGRJ48FF8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/237/plain-text-feedburner-subscriber-count/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Javascript: JQuery Tabs</title>
		<link>http://progtuts.info/232/javascript-jquery-tabs/</link>
		<comments>http://progtuts.info/232/javascript-jquery-tabs/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 17:59:34 +0000</pubDate>
		<dc:creator>jamie</dc:creator>
		
		<category><![CDATA[HTML Tutorials]]></category>

		<category><![CDATA[Javascript Tutorials]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=232</guid>
		<description><![CDATA[In modern web applications you often need to make something visually appealing and without using something like Flash, the best thing for this is Javascript.  There are a variety of frameworks available these days that enable a huge amount of visually impressive tricks – from animating content to organising it.  This is the [...]]]></description>
			<content:encoded><![CDATA[<p>In modern web applications you often need to make something visually appealing and without using something like Flash, the best thing for this is Javascript.  There are a variety of frameworks available these days that enable a huge amount of visually impressive tricks – from animating content to organising it.  This is the first of a series of articles showing how to use these frameworks to make your application a better place – Tabs using JQuery.<br />
<span id="more-232"></span></p>
<h2>What will we need?</h2>
<p>Chances are most of you are reading this using a tabbed browser so you know how useful such a thing is, and fortunately a tabbed section on your webpage is just a few simple lines of code away using the hugely popular <a title="jquery" href="http://jquery.com/" target="_blank">JQuery</a> framework.</p>
<p>Fortunately, this is a simple one – we just need the <a title="jquery framework" href="http://docs.jquery.com/Downloading_jQuery" target="_blank">JQuery framework file</a> and the <a title="jquery ui" href="http://ui.jquery.com/download" target="_blank">JQuery UI javascript files</a>.  The UI downloader allows you to choose the features you want, so for this part I have just downloaded the tabs section.  You also need some knowledge of CSS so you can style the tabs to look right.</p>
<p>Now we have what we need, let’s get started.</p>
<h2>How to begin</h2>
<p>The first thing we need to import the javascript files into your HTML file.  The following code should be in the  section of your html file.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-ui.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-tabs.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>The next thing to do is be able to notify the script when the document is loaded; the page elements need to exist in order to change them into tabbed form.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script&gt;
$(document).ready(function()
{
	$(&quot;#tabs &gt; ul&quot;).tabs(); // Note ‘#tabs’ refers to the div container storing the tab information.
});
&lt;/script&gt;</pre></div></div>

<p>Quite simply, this will be fired once the rest of the file has loaded up.  Note that I am using the $(id) notation to select items from the document which is functionality provided by jquery.  This is the only piece of javascript we will be using – it really is that easy!</p>
<p>We’re now ready to start adding content.</p>
<h2>How to make the tabs</h2>
<p>The tabs will be held within a div container, so we need to make that – note that it has the same name as we specified in our javascript in the previous section (in this case we used ‘#tabs’ so our div will have the id ‘tabs’).  We list our tab headers and actions in an unordered list element.</p>
<p>Here’s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;tabs&quot; class=&quot;tabs&quot;&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;target&quot;&gt;&lt;span&gt;One&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;target&quot;&gt;&lt;span&gt;Two&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;</pre></div></div>

<p>Now, there are various ways to put data into each tab.  Firstly, we can insert data from another div on the page – to do this we just make the link point to the id of the div.  An alternative method is to use AJAX to put the contents of another file into a tab – to do this we just make the link point to the file and JQuery will do the rest, it’s really really easy!</p>
<p>Here’s our updated example, with one link pointing to a div containing some content on the same page, and the other pointing to another page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;tabs&quot; class=&quot;tabs&quot;&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;#example&quot;&gt;&lt;span&gt;Example&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;another_file.html&quot;&gt;&lt;span&gt;Another File&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
        &lt;div id=&quot;example&quot;&gt;
        Some Example Text
        &lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>Note here that I’ve made the example div within the tabs div.</p>
<p>Our other file (another_file.html) can contain anything including Javascript:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;This is text in another file&lt;/p&gt;</pre></div></div>

<p>That’s all the HTML we need, it’s that simple and once you’ve put it all down, pretty intuitive too!</p>
<h2>Styling the tabs</h2>
<p>It might be a little confusing, but we don’t actually specify the css titles for the page elements, but here are the ones we need to style, and exactly what they do.  The easiest way to learn how the styles affect the layout is probably to download the sample code, run it and then change the styles to see what changes on screen – this is where you’ll need your CSS skills!<br />
As a bit of a helper, here is the way the code will actually be displayed in the browser, courtesy of the <a href="http://docs.jquery.com/UI/Tabs/Theming" target="_blank">JQuery UI Documentation</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul class=&quot;ui-tabs-nav&quot;&gt;
	&lt;li class=&quot;ui-tabs-selected&quot;&gt;&lt;a href=&quot;#fragment-1&quot;&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li class=&quot;ui-tabs-unselect&quot;&gt;&lt;a href=&quot;#fragment-2&quot;&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;li class=&quot;ui-tabs-disabled&quot;&gt;&lt;a href=&quot;#fragment-3&quot;&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&quot;fragment-1&quot; class=&quot;ui-tabs-panel&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;fragment-2&quot; class=&quot;ui-tabs-panel ui-tabs-hide&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;fragment-3&quot; class=&quot;ui-tabs-panel ui-tabs-hide&quot;&gt;&lt;/div&gt;</pre></div></div>

<ul>
<li>li – The main reason we need to style this element is to tell it to be vertical (tab headers stacked on top of each other) or horizontal (tab headers side by side).</li>
<li>.ui-tabs-nav – This is the section containing the tab headers – it is the full width or height of the entire tabbed pane so I would tend to find it best to simple style this to have the same background as the main page.</li>
<li>.ui-tabs-selected – This is (as you would expect) the class that styles the currently selected tab header.</li>
<li>.ui-tabs-unselect – This doesn’t actually do much – I don’t even bother applying styling to it.  Leave a comment if you have a good use for it.</li>
<li>.ui-tabs-disabled – This styles a disabled tab header.</li>
<li>.ui-tabs-panel – This is the actual panel which displays the contents of the tab section.</li>
<li>.ui-tabs-hide – This is to style the tabs that aren’t shown at the time – the minimum it must contain is ‘display: none;’.</li>
</ul>
<p>Okay, so that’s the basic elements and if you only style those then I assure you it won’t look very good.  Each tab header is a link, therefore we should style the links – this lets us style unselected tabs differently from selected tabs.  To do this, we style the .ui-tabs-nav a element.  This will be overridden by the .ui-tabs-selected element meaning we can show them both differently.  That may all sound a little overly-confusing and cryptic, but in reality it’s straightforward – have a look at the sample code (style.css) and it should make sense.  I’ve styled the demo to include a few effects I think look good and commented those to point them out.</p>
<h2>Conclusion</h2>
<p>JQuery tabs are very useful things to have on a webpage that contains a lot of content that needs organised.  There is a fair amount more that you can do with them than I have shown here (the FAQ section in the Documentation is an excellent place to start) but hopefully I’ve given you the push forward to get you into producing your own effective interfaces!<br />
JQuery really is an excellent framework and has a lot of really excellent applications so if you&#8217;re serious about adding javascript to your web applications I highly recommend you have a look through the documentation.</p>
<p><a class="demobutton" target="_blank" href="http://progtuts.info/downloads/JQueryTabs/">View the Demo</a> <a href="http://progtuts.info/wp-content/plugins/download-monitor/download.php?id=12" class="demobutton">Download the ZIP file</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/211/ajax-and-google-integrated-search/" title="AJAX and Google Integrated Search">AJAX and Google Integrated Search</a></li>
<li><a href="http://progtuts.info/186/create-a-parallax-website-header/" title="Create a Parallax Website Header">Create a Parallax Website Header</a></li>
<li><a href="http://progtuts.info/142/ajax-image-gallery/" title="AJAX Image Gallery">AJAX Image Gallery</a></li>
<li><a href="http://progtuts.info/128/creating-a-cms-website-layout-using-ext-js/" title="Creating a CMS Website Layout using Ext JS">Creating a CMS Website Layout using Ext JS</a></li>
<li><a href="http://progtuts.info/241/javascript-jquery-flip/" title="Javascript: JQuery Flip">Javascript: JQuery Flip</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=4Ul3JAjUdKc:-cA65fLn9y0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=4Ul3JAjUdKc:-cA65fLn9y0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=4Ul3JAjUdKc:-cA65fLn9y0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=4Ul3JAjUdKc:-cA65fLn9y0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=4Ul3JAjUdKc:-cA65fLn9y0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=4Ul3JAjUdKc:-cA65fLn9y0:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=4Ul3JAjUdKc:-cA65fLn9y0:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=4Ul3JAjUdKc:-cA65fLn9y0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/232/javascript-jquery-tabs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Write For ProgTuts</title>
		<link>http://progtuts.info/226/write-for-progtuts/</link>
		<comments>http://progtuts.info/226/write-for-progtuts/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 08:30:13 +0000</pubDate>
		<dc:creator>gilbitron</dc:creator>
		
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=226</guid>
		<description><![CDATA[As you may be aware running a site like this can take a lot of work. We are continually striving to provide you with high quality programming tutorials. However at the moment there is only two of us writing for ProgTuts and we are both very busy. We contribute time when we can but its [...]]]></description>
			<content:encoded><![CDATA[<p>As you may be aware running a site like this can take a lot of work. We are continually striving to provide you with high quality programming tutorials. However at the moment there is only two of us writing for ProgTuts and we are both very busy. We contribute time when we can but its not always possible.</p>
<p><img src="http://progtuts.info/wp-content/uploads/2008/10/1033453_34447262.jpg" alt="Write for ProgTuts" title="Write for ProgTuts" width="500" height="116" style="margin:0 auto;" /></p>
<p>&#8220;What does this have to do with me?&#8221; you might be asking. Well we would like to encourage you to <strong>contribute your own tutorials</strong> to ProgTuts. There are several advantages to doing this. Not only can you contribute to the community and get your work shown on the site, but you will also get a lot of exposure and link to your site (remember we get thousands of views per month).</p>
<p>So if you are interested in helping us make this site fantastic and want your own work to appear on a high profile website such as ProgTuts, then <a href="http://progtuts.info/contribute/">head over to our contribute page</a> for all the information you need. We hope to be hearing from you soon.<br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/182/introducing-progtuts-new-look/" title="Introducing ProgTuts New Look">Introducing ProgTuts New Look</a></li>
<li><a href="http://progtuts.info/99/forum-launch/" title="Forum Launch!">Forum Launch!</a></li>
<li><a href="http://progtuts.info/51/news-and-updates/" title="News and Updates">News and Updates</a></li>
<li><a href="http://progtuts.info/6/tutorial-suggestionsrequests/" title="Tutorial Suggestions/Requests">Tutorial Suggestions/Requests</a></li>
<li><a href="http://progtuts.info/5/launch/" title="Launch!">Launch!</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=jM_ICe9_GJ0:fh_Ri_9y62g:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=jM_ICe9_GJ0:fh_Ri_9y62g:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jM_ICe9_GJ0:fh_Ri_9y62g:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=jM_ICe9_GJ0:fh_Ri_9y62g:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jM_ICe9_GJ0:fh_Ri_9y62g:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=jM_ICe9_GJ0:fh_Ri_9y62g:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jM_ICe9_GJ0:fh_Ri_9y62g:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jM_ICe9_GJ0:fh_Ri_9y62g:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/226/write-for-progtuts/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AJAX and Google Integrated Search</title>
		<link>http://progtuts.info/211/ajax-and-google-integrated-search/</link>
		<comments>http://progtuts.info/211/ajax-and-google-integrated-search/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 09:26:53 +0000</pubDate>
		<dc:creator>gilbitron</dc:creator>
		
		<category><![CDATA[AJAX Tutorials]]></category>

		<category><![CDATA[CSS Tutorials]]></category>

		<category><![CDATA[HTML Tutorials]]></category>

		<category><![CDATA[Javascript Tutorials]]></category>

		<category><![CDATA[PHP Tutorials]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=211</guid>
		<description><![CDATA[In this tutorial I will show you how to create an AJAX search box that can be used on your website or blog that also has an integrated Google search built in. We will be using HTML, CSS, PHP and jQuery to create our search box.

Preperation
There is quite a lot of code in this tutorial [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I will show you how to create an AJAX search box that can be used on your website or blog that also has an integrated Google search built in. We will be using HTML, CSS, PHP and jQuery to create our search box.<br />
<span id="more-211"></span></p>
<h2>Preperation</h2>
<p>There is quite a lot of code in this tutorial so I am only going to cover the essential bits. To see the full code either <a target="_blank" href="http://progtuts.info/downloads/AJAXGoogleSearch/">go to the demo</a> and &#8220;view source&#8221; to see how it was done or <a href="http://progtuts.info/wp-content/plugins/download-monitor/download.php?id=11">download the ZIP file</a> with the source code. Anyway lets get started. First off some preperation.</p>
<ul>
<li>Download the latest version of the <a href="http://jquery.com/" target="_blank">jQuery javascript library</a>.</li>
<li>Since we are integrating an AJAX Google search you will need to sign up for a <a href="http://code.google.com/apis/ajaxsearch/signup.html" target="_blank">Google AJAX Search API Key</a>.</li>
</ul>
<p>Don&#8217;t worry thats all we need. </p>
<h2>1) The HTML</h2>
<p>Now in the HTML head include our new javascript files. The jQuery file and a file imported from Google to power the Google search.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery-1.2.6.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com/jsapi?key=YOURAPIKEY&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Remember to place your API key in there. The main content of the HTML file should be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict 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;searchbox&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Want to find something?<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search...&quot;</span> autocomplete<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;off&quot;</span> <span style="color: #66cc66;">/</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;resultbox&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none;&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;siteheader&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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sitesearch&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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;googleheader&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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;googlesearch&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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;footer&quot;</span>&gt;</span>
    Copyright <span style="color: #ddbb00;">&amp;copy;</span> 2008 My Company Inc
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>So what do we have here. Basically an input box (note no form tags) with an id and a value and the &#8220;autocomplete&#8221; parameter set to &#8220;off&#8221;. This is to stop the standard autocomplete box showing when you type stuff into the box. We then have a &#8220;resultbox&#8221; div (which is initially hidden) with 4 other divs. As you can see each type of search has 2 divs: a header and body div. If you were wanting to add other types of search here (like a yahoo search for instance) you can just add more divs as needed. And last but not least a footer (which is not required but I liked it). Not too complex huh.</p>
<h2>2) The Javascript</h2>
<p>Now this is where the fun starts. In your page head just after you have included the two javascript files add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Javascript&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #006600; font-style: italic;">//&lt;![CDATA[</span>
google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Search...&quot;</span><span style="color: #009900;">&#41;</span>;
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span>; 
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;Search...&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keyup</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>; 
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#footer&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span>;
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.siteheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;images/loading.gif&quot; width=&quot;16&quot; height=&quot;16&quot; style=&quot;margin-right:8px; vertical-align:-15%;&quot; /&gt;Loading Results from Site...'</span><span style="color: #009900;">&#41;</span>;
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.googleheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;images/loading.gif&quot; width=&quot;16&quot; height=&quot;16&quot; style=&quot;margin-right:8px; vertical-align:-15%;&quot; /&gt;Loading Results from Google...'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #009966; font-style: italic;">/* === Site Search === */</span>
		$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> 
			method<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;get&quot;</span><span style="color: #339933;">,</span>
			url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sitesearch.php&quot;</span><span style="color: #339933;">,</span>
			success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.siteheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Results from Site:'</span><span style="color: #009900;">&#41;</span>;
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sitesearch&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>; 
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #009966; font-style: italic;">/* === Google Search === */</span>
		<span style="color: #003366; font-weight: bold;">var</span> sc <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GSearchControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GwebSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GvideoSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GblogSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GnewsSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> drawOptions <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GdrawOptions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		drawOptions.<span style="color: #660066;">setDrawMode</span><span style="color: #009900;">&#40;</span>GSearchControl.<span style="color: #660066;">DRAW_MODE_TABBED</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> foo;
		sc.<span style="color: #660066;">setSearchCompleteCallback</span><span style="color: #009900;">&#40;</span>foo<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>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.googleheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Results from Google:'</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		sc.<span style="color: #660066;">setNoResultsString</span><span style="color: #009900;">&#40;</span>GSearchControl.<span style="color: #660066;">NO_RESULTS_DEFAULT_STRING</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	    sc.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;googlesearch&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> drawOptions<span style="color: #009900;">&#41;</span>;
		sc.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009966; font-style: italic;">/* === End Search === */</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">700</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>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#footer&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>;
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">//]]&gt;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>&#8220;Wow&#8221; I hear you saying. Don&#8217;t worry I will explain what all of this does, but before I do I recommend you read through the code a few times and get a feel for it as it will help you understand it when I explain it.</p>
<p>Ok lets start off simple. The first line loads the Google search.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The next bit of code controls how the search box itself works. Basically if the search is blurred (ie you are not using it) and there is nothing in the box then it makes the value of the search box &#8220;Search&#8230;&#8221; and hides the result box. Simply resetting it. If you focus the search box, however, and the value is &#8220;Search&#8230;&#8221; it hides the word &#8220;Search&#8230;&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Search...&quot;</span><span style="color: #009900;">&#41;</span>;
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span>; 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;Search...&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The next part is defining the keyup event. What we are doing here is every time you release a key we are going to update the search. &#8220;Why not keydown?&#8221; I hear you ask. Well if you use keydown it will search the last value you entered and not the current one. Try it out to see what I mean.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keyup</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">//...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>So a few things need to happen once we have triggered the keyup event. Frist we need to display the resultbox div. Second, in this example, I hide the footer but if you don&#8217;t have the footer then miss this bit out. Third we set the HTML in our siteheader and google header divs. Basically this is just a loading image with some loading text.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>; 
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#footer&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span>;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.siteheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;images/loading.gif&quot; width=&quot;16&quot; height=&quot;16&quot; style=&quot;margin-right:8px; vertical-align:-15%;&quot; /&gt;Loading Results from Site...'</span><span style="color: #009900;">&#41;</span>;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.googleheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;images/loading.gif&quot; width=&quot;16&quot; height=&quot;16&quot; style=&quot;margin-right:8px; vertical-align:-15%;&quot; /&gt;Loading Results from Google...'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Next we are going to actually do our site search. This is simply an AJAX request (using jQuery of course) to a file called &#8220;sitesearch.php&#8221;. In this example our sitesearch.php file returns dummy data but in the real world this would search your website database and return the results. I&#8217;m not going into this in this tutorial as that is a tutorial in itself. Once the request is complete we update the siteheader and sitesearch divs with the appropriate content.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* === Site Search === */</span>
$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> 
    method<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;get&quot;</span><span style="color: #339933;">,</span>
    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sitesearch.php&quot;</span><span style="color: #339933;">,</span>
    success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.siteheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Results from Site:'</span><span style="color: #009900;">&#41;</span>;
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sitesearch&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>; 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The next thing we need to do is the actual Google search. I wont go into this in too much detail as you can read <a href="http://code.google.com/apis/ajaxsearch/documentation/">Google&#8217;s own documentation</a> to see what options there are. </p>
<ul>
<li>Create a new &#8220;GSearchControl&#8221;.</li>
<li>Add different types of searches to the control. Its up to you what you want to include here.</li>
<li>Set the &#8220;drawOptions&#8221; to Tabbed.</li>
<li>Set our &#8220;searchCompleteCallback&#8221;. In here we just set the googleheader div to show appropriate content.</li>
<li>Set the &#8220;NoResultsString&#8221; to default and set which element to draw our results to (the googlesearch div).</li>
<li>Finally execute the search using the value from the search box</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* === Google Search === */</span>
<span style="color: #003366; font-weight: bold;">var</span> sc <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GSearchControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GwebSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GvideoSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GblogSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
sc.<span style="color: #660066;">addSearcher</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GnewsSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> drawOptions <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GdrawOptions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
drawOptions.<span style="color: #660066;">setDrawMode</span><span style="color: #009900;">&#40;</span>GSearchControl.<span style="color: #660066;">DRAW_MODE_TABBED</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> foo;
sc.<span style="color: #660066;">setSearchCompleteCallback</span><span style="color: #009900;">&#40;</span>foo<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>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.googleheader&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Results from Google:'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
sc.<span style="color: #660066;">setNoResultsString</span><span style="color: #009900;">&#40;</span>GSearchControl.<span style="color: #660066;">NO_RESULTS_DEFAULT_STRING</span><span style="color: #009900;">&#41;</span>;
&nbsp;
sc.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;googlesearch&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> drawOptions<span style="color: #009900;">&#41;</span>;
sc.<span style="color: #660066;">execute</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The final part of our javascript code is some cleaning up. Simply if you delete all the characters in the search box we will close the result box (using some jQuery animation) and show the footer.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#resultbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">700</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>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#footer&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And thats our javascript. I&#8217;ll let you take a break before we move on.</p>
<h2>3) The CSS</h2>
<p>I&#8217;m not going to say to much about the CSS as most of it is just standard styling. There is nothing fancy happening. There are a few style hacks for the google search but that&#8217;s about it. The main part of the CSS file looks like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #cc00cc;">#searchbox</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span>0 <span style="color: #993333;">auto</span>;
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/back.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">top</span>; 
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">700px</span>; 
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span>; 	
	<span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">100px</span>;
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">40px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">40px</span>;
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span>;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#resultbox</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#999</span>;
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span>;
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">15px</span>;
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">620px</span>;
	<span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span>;
	<span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span>;
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000</span>;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span>0 <span style="color: #993333;">auto</span>;
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">700px</span>; 
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#999</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span><span style="color: #933;">40px</span>;
	<span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">-25px</span>;
<span style="color: #00AA00;">&#125;</span>	
&nbsp;
input <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span>Georgia;
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span>;
	<span style="color: #000000; font-weight: bold;">font-style</span><span style="color: #00AA00;">:</span><span style="color: #993333;">italic</span>;
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span>;
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span>;
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">630px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
input<span style="color: #3333ff;">:focus</span><span style="color: #00AA00;">,</span>
textarea<span style="color: #3333ff;">:focus</span><span style="color: #00AA00;">,</span>
select<span style="color: #3333ff;">:focus </span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#999</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#googlesearch</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#sitesearch</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.googleheader</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.siteheader</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">18px</span>;
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span>Georgia;
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span>;
	<span style="color: #000000; font-weight: bold;">font-style</span><span style="color: #00AA00;">:</span><span style="color: #993333;">italic</span>;
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span> <span style="color: #933;">0px</span>;
	<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span>;
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#666</span>;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.siteheader</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Google Style Hacks */</span>
<span style="color: #6666ff;">.gsc-search-box</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.gsc-configLabelCell</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span>;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.gsc-control</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;"><span style="color: #cc66cc;">100</span>%</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.gsc-tabHeader</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">3px</span> <span style="color: #933;">8px</span> <span style="color: #933;">5px</span> <span style="color: #933;">8px</span>;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.gsc-tabhActive</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#666</span> !important;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.gsc-tabhInactive</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Link Colour */</span>
<span style="color: #cc00cc;">#resultbox</span> a<span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.gs-title</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.gs-title</span> b <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">13px</span>;
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span>;
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#2253AA</span> !important;
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.gs-visibleUrl</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.gs-visibleUrl-short</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#666</span> !important;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To see the full CSS file download the source code.</p>
<h2>Conclusion</h2>
<p>And that is us. We have created an AJAX search with an Integrated Google Search to really help our users find what they are looking for. As always this is very flexible and this tutorial is only an example of what you can do. Let me know if you come up with any creative alternatives and post them in the comments.</p>
<p><a class="demobutton" target="_blank" href="http://progtuts.info/downloads/AJAXGoogleSearch/">View the Demo</a> <a class="demobutton" href="http://progtuts.info/wp-content/plugins/download-monitor/download.php?id=11">Download the ZIP file</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/142/ajax-image-gallery/" title="AJAX Image Gallery">AJAX Image Gallery</a></li>
<li><a href="http://progtuts.info/128/creating-a-cms-website-layout-using-ext-js/" title="Creating a CMS Website Layout using Ext JS">Creating a CMS Website Layout using Ext JS</a></li>
<li><a href="http://progtuts.info/232/javascript-jquery-tabs/" title="Javascript: JQuery Tabs">Javascript: JQuery Tabs</a></li>
<li><a href="http://progtuts.info/186/create-a-parallax-website-header/" title="Create a Parallax Website Header">Create a Parallax Website Header</a></li>
<li><a href="http://progtuts.info/7/create-an-email-form-using-ajax/" title="Create an Email Form using AJAX">Create an Email Form using AJAX</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=YbjLHqVR38M:9GIjqd1fDkI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=YbjLHqVR38M:9GIjqd1fDkI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=YbjLHqVR38M:9GIjqd1fDkI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=YbjLHqVR38M:9GIjqd1fDkI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=YbjLHqVR38M:9GIjqd1fDkI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=YbjLHqVR38M:9GIjqd1fDkI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=YbjLHqVR38M:9GIjqd1fDkI:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=YbjLHqVR38M:9GIjqd1fDkI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/211/ajax-and-google-integrated-search/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Anatomy of a .htaccess File - Hints and Tips</title>
		<link>http://progtuts.info/198/the-anatomy-of-a-htaccess-file-hints-and-tips/</link>
		<comments>http://progtuts.info/198/the-anatomy-of-a-htaccess-file-hints-and-tips/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 09:54:39 +0000</pubDate>
		<dc:creator>gilbitron</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<category><![CDATA[.htaccess]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=198</guid>
		<description><![CDATA[Have you ever wondered how to create a &#8220;nice&#8221; or &#8220;clean&#8221; url such as the one for this page? Or wondered how to set a custom 404 page when users look for a page that isn&#8217;t there? Maybe you are trying to work out how to password protect directories on a web server. In this [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered how to create a &#8220;nice&#8221; or &#8220;clean&#8221; url such as the one for this page? Or wondered how to set a custom 404 page when users look for a page that isn&#8217;t there? Maybe you are trying to work out how to password protect directories on a web server. In this tutorial I&#8217;m going to tell you how to achieve all these things using a simple .htaccess file.<br />
<span id="more-198"></span></p>
<h2>A what file?</h2>
<p>A .htacces file. A .htaccess file is used by Apache (ie not Windows hosting) to define certain rules for the directory you place it in and all sub directories. Some things you should know about a .htaccess file are:</p>
<ul>
<li>It is relatively powerful. You can really mess up a website if you do something wrong with this file.</li>
<li>Due to the fact it is a file extension and doesn&#8217;t have a file name you can&#8217;t create one directly in Windows. In this case I would advise creating a text file (such as htaccess.txt), uploading it to the server and then using your ftp, or other, program to rename the file to .htaccess.</li>
<li>Some ftp programs hide these files on the server. Usually they have an option to &#8220;show hidden files&#8221;. Make sure this is ticked so you can see the file.</li>
</ul>
<h2>Custom Error Pages</h2>
<p>So lets get to it. Have you ever been on a website and got that annoying &#8220;Page Cannot be Found&#8221; message, otherwise known as a 404 error? Well you can change that and all other error pages by doing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">ErrorDocument <span style="color: #0000dd;">400</span> <span style="color: #339933;">/</span>errors<span style="color: #339933;">/</span>badrequest.<span style="color: #202020;">html</span>
ErrorDocument <span style="color: #0000dd;">401</span> <span style="color: #339933;">/</span>errors<span style="color: #339933;">/</span>authreqd.<span style="color: #202020;">html</span>
ErrorDocument <span style="color: #0000dd;">403</span> <span style="color: #339933;">/</span>errors<span style="color: #339933;">/</span>forbid.<span style="color: #202020;">html</span>
ErrorDocument <span style="color: #0000dd;">404</span> <span style="color: #339933;">/</span>errors<span style="color: #339933;">/</span>notfound.<span style="color: #202020;">html</span>
ErrorDocument <span style="color: #0000dd;">500</span> <span style="color: #339933;">/</span>errors<span style="color: #339933;">/</span>serverr.<span style="color: #202020;">html</span>
<span style="color: #339933;"># Or you can do:</span>
ErrorDocument <span style="color: #0000dd;">404</span> http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//progtuts.info/en/404.html</span>
<span style="color: #339933;"># Or even...</span>
ErrorDocument <span style="color: #0000dd;">401</span> <span style="color: #ff0000;">&quot;Hold the phone! You're not a &lt;a href=&quot;</span>member.<span style="color: #202020;">html</span><span style="color: #ff0000;">&quot;&gt;member&lt;/a&gt;. You can't see this page&quot;</span></pre></div></div>

<p>Pretty simple eh!</p>
<h2>Password Protect Directories</h2>
<p>This is another common thing people do. If you want to restrict access to directories but don&#8217;t want to faff about making your own HTML form authentication then you can use .htaccess combined with a .htpasswd file.</p>
<p><strong>.htpasswd</strong></p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">anmadno41<span style="color: #339933;">:</span>cFwiw18lMik6I</pre></div></div>

<p>This is quite simple. Just as username and password. The password is <strong>encrypted</strong>, however, but don&#8217;t worry as there is a <a href="http://www.tools.dynamicdrive.com/password/" target="_blank">neat online tool</a> to help with this.</p>
<p><strong>.htaccess</strong></p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">AuthName <span style="color: #ff0000;">&quot;Restricted Area&quot;</span> 
AuthType Basic 
AuthUserFile <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>mysite<span style="color: #339933;">/</span>.<span style="color: #202020;">htpasswd</span> 
AuthGroupFile <span style="color: #339933;">/</span>dev<span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">null</span> 
require valid<span style="color: #339933;">-</span>user</pre></div></div>

<p>Again this isn&#8217;t too hard. AuthName can be anything you want. Its just a name given to the restricted area. AuthType Basic just means we are using basic HTTP authentication. AuthUserFile is the full server path to your .htpasswd file, note this is not a url. The final line just means that any valid user can access the directory. You could change this to <strong>require user anmadno41</strong> to limit it to certain users.</p>
<h2>Redirects</h2>
<p>Redirects with .htaccess are pretty simple.</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">Redirect <span style="color: #339933;">/</span>old<span style="color: #339933;">/</span>oldfile.<span style="color: #202020;">html</span> http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//yoursite.com/new/newfile.html</span></pre></div></div>

<p>Here is a cool trick though. Say you want to work on a new deisgn for your website but don&#8217;t want visitors to see it. You could just password protect it as above, or you could redirect visitors to an update page explaining you are updating the website while still allowing you to access the files.</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">order deny<span style="color: #339933;">,</span>allow
deny from all
allow from 12.34.56.78
&nbsp;
ErrorDocument <span style="color: #0000dd;">403</span> <span style="color: #339933;">/</span>maintenance.<span style="color: #202020;">html</span>
&nbsp;
<span style="color: #339933;">&lt;</span>Files maintenance.<span style="color: #202020;">html</span><span style="color: #339933;">&gt;</span>
allow from all
<span style="color: #339933;">&lt;/</span>Files<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This would redirect all visitors to maintenance.html while allowing your IP to access the files. Remember to change <strong>12.34.56.78</strong> and <strong>maintenance.html</strong> to your own IP and file.</p>
<h2>Protecting Files and Directories</h2>
<p>Say you want to restrict access to certain files (such as logs and dat files) that normally could be downloaded by accessing them through the browser. You can do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>Files ~ <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>(log|dat)$&quot;</span><span style="color: #339933;">&gt;</span>
deny from all
<span style="color: #339933;">&lt;/</span>Files<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Or what if you don&#8217;t have empty index.html pages in your directories but want to stop people for browsing your directories.</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">Options All <span style="color: #339933;">-</span>Indexes
<span style="color: #339933;"># Or maybe only hide certain files:</span>
IndexIgnore <span style="color: #339933;">*</span>.<span style="color: #202020;">gif</span> <span style="color: #339933;">*</span>.<span style="color: #202020;">jpg</span></pre></div></div>

<h2>Force Downloading of Files</h2>
<p>Here is a cool trick. Have you ever had a problem visitors streaming your media files instead of downloading them. You can force them to download the files by changing the MIME types to <strong>octet-stream</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">AddType application<span style="color: #339933;">/</span>octet<span style="color: #339933;">-</span>stream .<span style="color: #202020;">avi</span> 
AddType application<span style="color: #339933;">/</span>octet<span style="color: #339933;">-</span>stream .<span style="color: #202020;">mpg</span>
AddType application<span style="color: #339933;">/</span>octet<span style="color: #339933;">-</span>stream .<span style="color: #202020;">mov</span>
AddType application<span style="color: #339933;">/</span>octet<span style="color: #339933;">-</span>stream .<span style="color: #202020;">mp3</span>
AddType application<span style="color: #339933;">/</span>octet<span style="color: #339933;">-</span>stream .<span style="color: #202020;">wma</span>
AddType application<span style="color: #339933;">/</span>octet<span style="color: #339933;">-</span>stream .<span style="color: #202020;">pdf</span></pre></div></div>

<h2>Preventing Hot Linking of Images</h2>
<p>Say you run a site with loads of images and you don&#8217;t want people &#8220;hot-linking&#8221; to your images and stealing your bandwidth. You can use a simple bit of code to prevent that:</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">RewriteEngine on
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_REFERER<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!^</span>$
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_REFERER<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!^</span>http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//(www\.)?mysite.com/.*$ [NC]</span>
RewriteRule \.<span style="color: #009900;">&#40;</span>gif|jpg|png<span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">-</span> <span style="color: #009900;">&#91;</span>F<span style="color: #009900;">&#93;</span></pre></div></div>

<p>Remember to change <strong>mysite.com</strong> to your own website. Note that this technique can also be used to protect hot linking of other files such as Javascript and CSS files by adding the file extensions to the rewrite rule (eg <strong>RewriteRule \.(gif|jpg|png|js|css)$ - [F]</strong>).</p>
<h2>Nice/Clean Url&#8217;s</h2>
<p>So you&#8217;ve got this url that looks something like<br />
<strong>http://www.mysite.com/index.php?page=blog</strong><br />
and you want it to look more like<br />
<strong>http://www.mysite.com/blog/</strong><br />
So how do we do that?</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">RewriteEngine On
RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #0000dd;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span>$ index.<span style="color: #202020;">php</span><span style="color: #339933;">?</span>page<span style="color: #339933;">=</span>$<span style="color: #0000dd;">1</span>
RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z0<span style="color: #339933;">-</span><span style="color: #0000dd;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>$ index.<span style="color: #202020;">php</span><span style="color: #339933;">?</span>page<span style="color: #339933;">=</span>$<span style="color: #0000dd;">1</span></pre></div></div>

<p>Using the .htaccess file and some <a href="http://www.regular-expressions.info/">regular expressions</a> we can make a clean url. I&#8217;m not going to explain regular expressions here as that is a whole other topic but what this code basically does it take the last part of the clean url (<strong>/blog/</strong>) and redirect it to <strong>index.php?page=blog</strong> without actually showing the messy url. Great!</p>
<h2>Conclusion</h2>
<p>So there you have it. The anatomy of a .htaccess file. Hopefully this will help all you webmasters, as it helped me, to discover the advantages of using a .htaccess file on your website. For further reading why not check out:<br />
<a href="http://www.javascriptkit.com/howto/htaccess.shtml">Comprehensive Guide to .htaccess</a><br />
<a href="http://corz.org/serv/tricks/htaccess.php">.htaccess Tips and Tricks</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li>No Related Posts</li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=yOFZLJ0tzm4:q6qjyCSfbOE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=yOFZLJ0tzm4:q6qjyCSfbOE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=yOFZLJ0tzm4:q6qjyCSfbOE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=yOFZLJ0tzm4:q6qjyCSfbOE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=yOFZLJ0tzm4:q6qjyCSfbOE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=yOFZLJ0tzm4:q6qjyCSfbOE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=yOFZLJ0tzm4:q6qjyCSfbOE:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=yOFZLJ0tzm4:q6qjyCSfbOE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/198/the-anatomy-of-a-htaccess-file-hints-and-tips/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Simple Graphics with Java</title>
		<link>http://progtuts.info/192/simple-graphics-with-java/</link>
		<comments>http://progtuts.info/192/simple-graphics-with-java/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 12:49:51 +0000</pubDate>
		<dc:creator>jamie</dc:creator>
		
		<category><![CDATA[Java Tutorials]]></category>

		<category><![CDATA[Graphics]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=192</guid>
		<description><![CDATA[Creating graphics with programming languages can seem a little strange; how do these commands we write possibly become that nice picture on the screen?  How can that nice picture be animated?
Today I’m going to show you a really simple 2D example of how to do it in Java.

Do we need special packages?
None that don’t [...]]]></description>
			<content:encoded><![CDATA[<p>Creating graphics with programming languages can seem a little strange; how do these commands we write possibly become that nice picture on the screen?  How can that nice picture be animated?<br />
Today I’m going to show you a really simple 2D example of how to do it in Java.<br />
<span id="more-192"></span></p>
<h2>Do we need special packages?</h2>
<p>None that don’t already come in the Java build that you are using.  We will be drawing the image onto the Graphics object in a JPanel.  The Graphics object is from the AWT library and the JPanel is from the Swing library – simply import java.awt.*; and import javax.swing.*;<br />
To make our lives simple we are going to take advantage of the Object Oriented nature of Java so that we are Manipulating objects and then drawing them to screen – this helps to separate logic from view and if you’ve already read my <a href="http://progtuts.info/46/model-view-controller-with-java/" target="_blank">MVC with Java tutorial</a>, you’ll realise that means we are going to do this using the Model View Controller architecture.  If you don’t know what that is or haven’t really used it before, go and have a read now because I will assume in this tutorial that you are an MVC whizz-kid.</p>
<p>So, let’s get started!</p>
<h2>What are we making?</h2>
<p>A really simple example of animated 2D graphics is a Mouse-Trail application – a fading coloured line follows the mouse wherever it goes.  This example is really simple and is only intended to get you familiar with the idea, and get you thinking about what you can create yourself.  It is created by ticking a timer and at each tick taking the old mouse position and the new mouse position and creating a line between them.  The fading part is done by then manipulating that line object – no need for fancy tricks or filters!<br />
We are going to have quite a few classes, but most of them are only really to fulfil the MVC requirement which may seem a bit like overkill for this example, but will soon come into its own for a more detailed project.</p>
<ul>
<li>Model.java – This is the model, it stores details of all the lines in the drawing and sends notifications to the observers.</li>
<li>View.java – This is the UI – in this case incredibly simple as it just makes a frame and puts a customised JPanel (canvas.java) in it.  It is an observer to Model and will repaint the canvas.  It also deals with the Timer.</li>
<li>Controller.java – Unlike the MVC tutorial we actually have something for the controller to do here; it deals with working out the position of the mouse and telling the model.</li>
<li>Canvas.java – This is where things are drawn!  It extends JPanel and re-writes the repaint methods.</li>
<li>Line.java – This is a line object, which stores where it should be stored and the transparency level.</li>
</ul>
<h2>Show us the code!</h2>
<p>I’ll start off with the Line class because it is the building block that we need.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> x1, y1, x2, y2;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> transparency;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Color</span> colour;
<span style="color: #000000; font-weight: bold;">private</span> Model model;</pre></div></div>

<p>These are the variables we will need.  We store the start and end points as integers because this is what the Graphics object will draw a line with – floats and doubles etc. are useless to it.<br />
The value of the transparency is stored as a float as it will be a ‘percentage’ of 1.  I’ve also included a Color variable which will allow you to alter the colour that the line is drawn in.  Finally, there is a Model variable so that the Line knows where it is being stored – this will also allow it to remove itself from the Model once it has full transparency (i.e. can’t be seen!).</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Line</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> nx1, <span style="color: #000066; font-weight: bold;">int</span> ny1, <span style="color: #000066; font-weight: bold;">int</span> nx2, <span style="color: #000066; font-weight: bold;">int</span> ny2, <span style="color: #003399;">Color</span> c, Model m<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	x1 <span style="color: #339933;">=</span> nx1;
	y1 <span style="color: #339933;">=</span> ny1;
	x2 <span style="color: #339933;">=</span> nx2;
	y2 <span style="color: #339933;">=</span> ny2;
	transparency <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;
	colour <span style="color: #339933;">=</span> c;
	model <span style="color: #339933;">=</span> m;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the constructor – nothing fancy, just setting all the variables to those given and setting the transparency to 1.<br />
The rest of this class is given up to Get and Set methods which I won’t bother writing into the tutorial, but obviously they are in the sample code at the bottom of the page.  The only one which is altered from the standard return method is the setTransparency method.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTransparency<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> transparency<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">transparency</span> <span style="color: #339933;">=</span> transparency;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">transparency</span><span style="color: #339933;">&lt;=</span>0<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		model.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method sets the value and then checks if the transparency has gone equal to or below 0.  If it has, it removes itself from the model containing it.</p>
<p>Next we will look at the Canvas class because it is what draws the lines.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">View</span> parent;</pre></div></div>

<p>This is the only variable we need for this class because it is very simple.  If you are going to implement some custom Double Buffering, you will need to have a few more variables but that is outside of the scope of this tutorial.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Canvas</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	parent <span style="color: #339933;">=</span> v;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is only a very simple constructor, just calling the super constructor and setting our only variable to the one given.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #003399;">Graphics</span> g<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>	
	<span style="color: #003399;">Graphics2D</span> canvas <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Graphics2D</span><span style="color: #009900;">&#41;</span>g;
	canvas.<span style="color: #006633;">setRenderingHint</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">RenderingHints</span>.<span style="color: #006633;">KEY_ANTIALIASING</span>, <span style="color: #003399;">RenderingHints</span>.<span style="color: #006633;">VALUE_ANTIALIAS_ON</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #003399;">Dimension</span> d <span style="color: #339933;">=</span> getSize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	g.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">black</span><span style="color: #009900;">&#41;</span>;
	g.<span style="color: #006633;">fillRect</span><span style="color: #009900;">&#40;</span>0, 0, d.<span style="color: #006633;">width</span>, d.<span style="color: #006633;">height</span><span style="color: #009900;">&#41;</span>;
	Vector<span style="color: #339933;">&lt;</span>Line<span style="color: #339933;">&gt;</span> lines <span style="color: #339933;">=</span> parent.<span style="color: #006633;">getLines</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span>0; i<span style="color: #339933;">&lt;</span>lines.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		canvas.<span style="color: #006633;">setComposite</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">AlphaComposite</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">AlphaComposite</span>.<span style="color: #006633;">SRC_OVER</span>, lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTransparency</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		canvas.<span style="color: #006633;">setColor</span><span style="color: #009900;">&#40;</span>lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getColour</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		canvas.<span style="color: #006633;">setStroke</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BasicStroke</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #003399;">BasicStroke</span>.<span style="color: #006633;">CAP_ROUND</span>, <span style="color: #003399;">BasicStroke</span>.<span style="color: #006633;">JOIN_ROUND</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		canvas.<span style="color: #006633;">drawLine</span><span style="color: #009900;">&#40;</span>lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getX1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getY1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getX2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getY2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setTransparency</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>lines.<span style="color: #006633;">elementAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTransparency</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">0.02</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the all-new paint method which will over-write the native one.  Its job is very simple – draw a big black rectangle over the whole screen, then draw each line in the Model.  We cast the Graphics object to a Graphics2D object to allow us to use Transparency and alter the stroke (thickness and style).  We then make the changes we want to and then use the drawline method to draw it!  Finally, we change the transparency of the line by a pre-set amount.</p>
<p>Next up we’ll make the Model class so that the View and the Controller will have something to communicate with.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> Vector<span style="color: #339933;">&lt;</span>Line<span style="color: #339933;">&gt;</span> lines;</pre></div></div>

<p>This is the only variable in the Model and is just a container to store the Line objects.  I have used a Vector but you can use anything you like as long as the size isn’t too constrictive – an array is bad because it really needs the size to be specified at the beginning.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> Model<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	lines <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&lt;</span>Line<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A simple constructor is all that is needed, and it just initializes our container for Lines.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setChanged</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">notifyObservers</span><span style="color: #009900;">&#40;</span>lines<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the update method that is required for the class to fulfil its purpose as an Observable in the MVC architecture.  It just sends the lines container to any observers.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> remove<span style="color: #009900;">&#40;</span><span style="color: #003399;">Line</span> l<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>lines.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		lines.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> add<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x1, <span style="color: #000066; font-weight: bold;">int</span> y1, <span style="color: #000066; font-weight: bold;">int</span> x2, <span style="color: #000066; font-weight: bold;">int</span> y2, <span style="color: #003399;">Color</span> c<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	lines.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Line</span><span style="color: #009900;">&#40;</span>x1, y1, x2, y2, c, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>These are the remaining methods in the class and deal with creating and removing lines.  They are fairly self explanatory, and the only item of note is that the remove method ensures that the given line exists to avoid any errors. </p>
<p>Now that we have the Model we can create the Controller which sends all the information about the mouse location to the Model.  It implements Observer so that it can be an observer to the Model.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Controller <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Observer</span>, <span style="color: #003399;">ActionListener</span>, <span style="color: #003399;">MouseMotionListener</span></pre></div></div>

<p>This is the class declaration line, and as shown we implement Observer, ActionListener and MouseMotionListener.  You may also wish to implement some others, for example a KeyListener or a MouseListener.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> Model model;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> prevx, prevy;</pre></div></div>

<p>The variables we need for the Controller are an instance of the Model so that data about events can be sent, and also the ‘old’ positions of the mouse in order to create start and end points for the line.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> Controller<span style="color: #009900;">&#40;</span>Model m<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
model <span style="color: #339933;">=</span> m;
	model.<span style="color: #006633;">addObserver</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Register this class with the Model.</span>
	prevx <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>;
	prevy <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again this is a very simple constructor, where the Model is set to the one given, and registers this Controller as being an observer.  We also set the previous mouse values to -1 as this is an impossible number for the screen position to register and we can identify the first movement.  If we didn’t do this, we would start off with a big ugly line from the top right corner (0, 0) to where the mouse is.</p>
<p>There are several methods we need to allow us to have the implementations of the class (Observer etc.) but most of them aren’t used.  For this example we are only going to use ActionPerformed() and MouseMoved()</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> actionPerformed<span style="color: #009900;">&#40;</span><span style="color: #003399;">ActionEvent</span> e<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	model.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Basically this method will be fired every time the timer in the View ticks, so all we want to do is update the model.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> mouseMoved<span style="color: #009900;">&#40;</span><span style="color: #003399;">MouseEvent</span> arg0<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>prevx<span style="color: #339933;">&gt;-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> prevy<span style="color: #339933;">&gt;-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		model.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>prevx, prevy, arg0.<span style="color: #006633;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">7</span>, arg0.<span style="color: #006633;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">25</span>, <span style="color: #003399;">Color</span>.<span style="color: #006633;">GREEN</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	prevx <span style="color: #339933;">=</span> arg0.<span style="color: #006633;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">7</span>;
	prevy <span style="color: #339933;">=</span> arg0.<span style="color: #006633;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">25</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the section where we work out where the mouse is on the screen.  As you will see we have the condition for checking our first-move values (prevx and prevy are set to -1 before the first move and changed after it).  I’m also making a slight adjustment to the location of -7 on the x value and -25 on the y value to make it look like the line is being drawn from the tip of the mouse pointer.  If you are using a different pointer, you may need to alter these values.</p>
<p>That’s the Controller done, so let’s move on to the final class – the View.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">View</span> <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">JFrame</span> <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Observer</span></pre></div></div>

<p>In the class declaration we simply extend JFrame and implement Observer to allow this class to hold and show a JPanel as well as register as an observer to the Model.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> Model model;
<span style="color: #000000; font-weight: bold;">private</span> Controller controller;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Canvas</span> canvas;
<span style="color: #000000; font-weight: bold;">private</span> Vector<span style="color: #339933;">&lt;</span>Line<span style="color: #339933;">&gt;</span> lines;</pre></div></div>

<p>The variables we need are basically just instances of everything else.  We need a Model to register with, a Controller to deal with Timer ticks, a Canvas to show everything on and a Vector of lines to store the update from the Model for the Canvas to get a hold of and display.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">View</span><span style="color: #009900;">&#40;</span>Model m<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mouse Trail Tutorial&quot;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Properly instatiate the Superclass JFrame.</span>
	MakeThis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Set up the GUI stuff.</span>
	model <span style="color: #339933;">=</span> m;
	model.<span style="color: #006633;">addObserver</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Register this class with the Model</span>
	controller <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Controller<span style="color: #009900;">&#40;</span>model<span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Make the Controller using the same Model</span>
	<span style="color: #003399;">Timer</span> timer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span>, controller<span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Make the timer use the Controller</span>
	timer.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// ...and start it</span>
	lines <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&lt;</span>Line<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">addMouseMotionListener</span><span style="color: #009900;">&#40;</span>controller<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A fairly detailed constructor for the View, but it is fairly simple and is just about registering this with everything else and starting the Timer.  Note that I’m adding the MouseMotionListener available through the Controller to the View instead of the Canvas – strictly it should be the Canvas but as we are only showing the Canvas it’s a bit neater to do it this way.  There will be no difference to how it shows, it is just slightly less efficient if you were to have other objects in the Frame.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> MakeThis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	canvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Canvas</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setLocation</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">250</span>, <span style="color: #cc66cc;">250</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setSize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span>, <span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">addWindowListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">WindowAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowClosing<span style="color: #009900;">&#40;</span><span style="color: #003399;">WindowEvent</span> e<span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span>0<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a fairly straightforward method to create and show the View.  It initiates the Canvas variable to be linked with this View, then adds it to the contentPane and sets the size and location of the View.  It also sets the View to be visible and adds a window listener to close it properly when the ‘x’ at the corner of the screen is clicked.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> update<span style="color: #009900;">&#40;</span><span style="color: #003399;">Observable</span> arg0, <span style="color: #003399;">Object</span> arg1<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>arg1<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			Vector<span style="color: #339933;">&lt;</span>Line<span style="color: #339933;">&gt;</span> temp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Vector<span style="color: #339933;">&lt;</span>Line<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span>arg1;
			lines <span style="color: #339933;">=</span> temp;
			canvas.<span style="color: #006633;">repaint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error; Invalid update object&quot;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The update method is required to allow the View to be an observer, and this allows the Model to send data to it on update.<br />
In this example, the data being sent is a Vector of Line objects so we attempt to parse the Vector from the update Object and then tell the Canvas to repaint using the new lines.</p>
<p>And that’s it, done!</p>
<h2>Conclusion</h2>
<p>Today we’ve made a very simple example that is very probably useless to you at this point in time, but it does introduce a few key concepts of making 2D Graphics in Java – in particular the idea of using objects to represent items on the drawing which can then be individually modified, and also using the Graphics2D object to give a bit more freedom in the techniques you can achieve.<br />
If you are wondering what the point is in learning this kind of stuff, I’ve used these basic concepts to create a 2D graphics engine driving a full and extended version of Asteroids, an extension of the 2D graphics engine to produce a 3D mesh with Fractal composition, and a separate project to create a detailed and rather graphics-happy extension of Pinball.  </p>
<p>I hope you’ve enjoyed reading this and it’s inspired you to try and create something of your own.  I’d love to hear of any projects you use these concepts for!</p>
<p>Remember to head over to our <a href="http://progtuts.info/forum/">forum</a> if you have any problems or just want to talk about everything Programming!</p>
<p><a href="http://progtuts.info/wp-content/plugins/download-monitor/download.php?id=10" class="demobutton">Download the ZIP file</a></p>
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/46/model-view-controller-with-java/" title="Model View Controller with Java">Model View Controller with Java</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=eWjFvObUxPk:vRuWgiujJ4E:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=eWjFvObUxPk:vRuWgiujJ4E:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=eWjFvObUxPk:vRuWgiujJ4E:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=eWjFvObUxPk:vRuWgiujJ4E:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=eWjFvObUxPk:vRuWgiujJ4E:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=eWjFvObUxPk:vRuWgiujJ4E:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=eWjFvObUxPk:vRuWgiujJ4E:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=eWjFvObUxPk:vRuWgiujJ4E:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/192/simple-graphics-with-java/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Create a Parallax Website Header</title>
		<link>http://progtuts.info/186/create-a-parallax-website-header/</link>
		<comments>http://progtuts.info/186/create-a-parallax-website-header/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 12:54:44 +0000</pubDate>
		<dc:creator>gilbitron</dc:creator>
		
		<category><![CDATA[CSS Tutorials]]></category>

		<category><![CDATA[HTML Tutorials]]></category>

		<category><![CDATA[Javascript Tutorials]]></category>

		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=186</guid>
		<description><![CDATA[This tutorial will show you how to create a cool Parallax Website Header using jQuery and the jParallax jQuery plugin. This is quite a simple experiment and you won&#8217;t need any javascript knowledge, just plain old HTML and CSS. However this has the potential to create some really stunning effects.

The Files
So first of all we [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will show you how to create a cool Parallax Website Header using jQuery and the jParallax jQuery plugin. This is quite a simple experiment and you won&#8217;t need any javascript knowledge, just plain old HTML and CSS. However this has the potential to create some really stunning effects.<br />
<span id="more-186"></span></p>
<h2>The Files</h2>
<p>So first of all we will need some javascript files:</p>
<ul>
<li><a href="http://jquery.com/">Download jQuery</a></li>
<li><a href="http://webdev.stephband.info/parallax.html">Download jParallax</a></li>
</ul>
<p>There I told you this was simple. Next&#8230;</p>
<h2>The Images</h2>
<p>Before we do anymore its a good idea to create the images we will use in our header. You can have as many layers as you want but in this example we will use four images:</p>
<ul>
<li>Sky</li>
<li>Sun</li>
<li>Grass</li>
<li>Tree</li>
</ul>
<p>If you don&#8217;t know what I&#8217;m on about (or you don&#8217;t know what the Parallax effect is) have a look at this <a href="http://en.wikipedia.org/wiki/Parallax_scrolling">wikipedia article</a> and you&#8217;ll see what I mean. So once we have our images then lets do some HTMLing.</p>
<h2>The HTML</h2>
<p>The HTML is where most of the work is done. I tell a bit of a lie here because <em>most</em> of the work is done by <a href="http://webdev.stephband.info/parallax.html">jParallax</a>, but it is done automatically so we don&#8217;t really need to worry about it. So the HTML should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> &gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery-1.2.6.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.jparallax.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;style.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Parallax Website Header<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
jQuery(document).ready(function(){
	jQuery('#parallax').jparallax({});
});
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
&nbsp;
<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;parallax&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width: 700px; height: 300px;&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/0_day.png&quot;</span><span style="color: #66cc66;">/</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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width: 750px; height: 280px;&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:absolute; top:0px; left:350px;&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/1_sun.png&quot;</span><span style="color: #66cc66;">/</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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width: 700px; height: 250px;&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:absolute; top:200px; left:0px;&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/2_grass.png&quot;</span><span style="color: #66cc66;">/</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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width: 900px; height: 260px;&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:absolute; top:100px; left:150px;&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/3_tree.png&quot;</span><span style="color: #66cc66;">/</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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width: 900px; height: 260px;&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:absolute; top:100px; right:150px;&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images/3_tree.png&quot;</span><span style="color: #66cc66;">/</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: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width: 800px; height: 250px;&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;position:absolute; top:100px; right:355px;&quot;</span>&gt;</span>My Site<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</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>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Pretty simple yeh? So what&#8217;s happening here.</p>
<ul>
<li>First off we include our javascript files that we downloaded. Remember to include the jParallax file after the jQuery file.</li>
<li>Then we call our only bit of javascript. Just one function that initializes the jParallax on a specified div (in this case #parallax). Note you can pass in options here but we are just going to keep it simple and use the default options.</li>
<li>Inside our parallax div we specify our layers. In this case each layer is a div but it could be a li or just an img. The <strong>important</strong> thing to note is the styles of the images and divs. This is what controls the feel of the parallax when it is active. Unfortunately there is no set scheme to calculate the sizes. Just play around with them until you get something that looks good.</li>
</ul>
<p>And that&#8217;s that really. Obviously in this example we have put in some text as an example but anything could go inside the divs. Be creative.</p>
<h2>The CSS</h2>
<p>The CSS is almost too simple to mention but I will anyway. All we need to do is define the styles for #parallax.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"><span style="color: #cc00cc;">#parallax</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#ccc</span>; 
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span>; 
	<span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span>;
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">700px</span>; 
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">300px</span>; 
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Important things to remeber about the CSS are:</p>
<ul>
<li>Height and width for each child must be explicitly set via CSS. jParallax also automatically gives each child position:absolute in order to start moving them around, so there&#8217;s no need to set it manually.</li>
<li>The parallaxed element itself needs to have position:relative or position:absolute specified or the children will move relative to the document rather than the viewport!</li>
</ul>
<h2>Conclusion</h2>
<p>So there you have it. A simple parallax website header. This example is relatively simple but if you use your imagination I&#8217;m sure you could come up with some cool ideas. If you do why not tell us about them in the comments below.</p>
<p><a href="http://progtuts.info/downloads/ParallaxHeader/" class="demobutton" target="_blank">View the Demo</a> <a href="http://progtuts.info/wp-content/plugins/download-monitor/download.php?id=9" class="demobutton">Download the ZIP file</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/211/ajax-and-google-integrated-search/" title="AJAX and Google Integrated Search">AJAX and Google Integrated Search</a></li>
<li><a href="http://progtuts.info/232/javascript-jquery-tabs/" title="Javascript: JQuery Tabs">Javascript: JQuery Tabs</a></li>
<li><a href="http://progtuts.info/164/pure-css-accordion-menu/" title="Pure CSS Accordion Menu">Pure CSS Accordion Menu</a></li>
<li><a href="http://progtuts.info/128/creating-a-cms-website-layout-using-ext-js/" title="Creating a CMS Website Layout using Ext JS">Creating a CMS Website Layout using Ext JS</a></li>
<li><a href="http://progtuts.info/241/javascript-jquery-flip/" title="Javascript: JQuery Flip">Javascript: JQuery Flip</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=u22Sdl7d2Xo:pmSQZ1Y5Mnw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=u22Sdl7d2Xo:pmSQZ1Y5Mnw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=u22Sdl7d2Xo:pmSQZ1Y5Mnw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=u22Sdl7d2Xo:pmSQZ1Y5Mnw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=u22Sdl7d2Xo:pmSQZ1Y5Mnw:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=u22Sdl7d2Xo:pmSQZ1Y5Mnw:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=u22Sdl7d2Xo:pmSQZ1Y5Mnw:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=u22Sdl7d2Xo:pmSQZ1Y5Mnw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/186/create-a-parallax-website-header/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introducing ProgTuts New Look</title>
		<link>http://progtuts.info/182/introducing-progtuts-new-look/</link>
		<comments>http://progtuts.info/182/introducing-progtuts-new-look/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 12:44:21 +0000</pubDate>
		<dc:creator>gilbitron</dc:creator>
		
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=182</guid>
		<description><![CDATA[After the amazing growth of this site I decided that we should maybe update the look of the site and give it a unique feel. So here it is. I would really like to hear people&#8217;s thoughts on the new site design so please tell us what you think. After all it is you guys [...]]]></description>
			<content:encoded><![CDATA[<p>After the amazing growth of this site I decided that we should maybe update the look of the site and give it a unique feel. So here it is. I would really like to hear people&#8217;s thoughts on the new site design so please tell us what you think. After all it is you guys that will be using the site. So if you don&#8217;t like the colours, width or you think there are features missing you would really like to see then please comment below. On the other hand if you love the new look then please tell us as well.</p>
<p>I hope this new look will benefit everyone.<br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/226/write-for-progtuts/" title="Write For ProgTuts">Write For ProgTuts</a></li>
<li><a href="http://progtuts.info/99/forum-launch/" title="Forum Launch!">Forum Launch!</a></li>
<li><a href="http://progtuts.info/51/news-and-updates/" title="News and Updates">News and Updates</a></li>
<li><a href="http://progtuts.info/6/tutorial-suggestionsrequests/" title="Tutorial Suggestions/Requests">Tutorial Suggestions/Requests</a></li>
<li><a href="http://progtuts.info/5/launch/" title="Launch!">Launch!</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=88Yl4wKBKw4:3C-sUA2eAnA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=88Yl4wKBKw4:3C-sUA2eAnA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=88Yl4wKBKw4:3C-sUA2eAnA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=88Yl4wKBKw4:3C-sUA2eAnA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=88Yl4wKBKw4:3C-sUA2eAnA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=88Yl4wKBKw4:3C-sUA2eAnA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=88Yl4wKBKw4:3C-sUA2eAnA:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=88Yl4wKBKw4:3C-sUA2eAnA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/182/introducing-progtuts-new-look/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Automatically Create Thumbnail Images</title>
		<link>http://progtuts.info/178/php-automatically-create-thumbnail-images/</link>
		<comments>http://progtuts.info/178/php-automatically-create-thumbnail-images/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 01:11:29 +0000</pubDate>
		<dc:creator>jamie</dc:creator>
		
		<category><![CDATA[HTML Tutorials]]></category>

		<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://progtuts.info/?p=178</guid>
		<description><![CDATA[I recently wrote about making an AJAX image gallery.  In it I said you should have a separate thumbnail image so that you don’t have to load up the main image and resize it, wasting time and efficiency.  In this article, I’m going to show you how to automatically make a thumbnail when [...]]]></description>
			<content:encoded><![CDATA[<p>I recently wrote about making an AJAX image gallery.  In it I said you should have a separate thumbnail image so that you don’t have to load up the main image and resize it, wasting time and efficiency.  In this article, I’m going to show you how to automatically make a thumbnail when an image is uploaded using PHP.<br />
<span id="more-178"></span></p>
<h2>What do we need?</h2>
<p>Well, you need a page with a form containing a file field and a page to actually upload the file and make the thumbnail out of it.  We will be using the <a href="http://uk.php.net/gd" target="_blank">PHP GD library</a> which will need to be part of your PHP installation.  In more recent versions of PHP I think it is included as standard.  The library contains a load of image manipulation tools and I would highly recommend you have a look through it to see what other effects you can achieve.  </p>
<p>Before we start, I have to highlight the fact that this tool should not be openly available to the public unless it has extremely tight security measures on what can be uploaded.  We will be making a form which uploads files directly to your server which means rogue files could fill your server with junk or much worse compromise its security.  Making it public is done at your own risk!</p>
<h2>How do we do it?</h2>
<p>Well, the form is really simple – a standard form using POST method and having an input of type ‘file’.  For this project I’m also going to include a text input for the image name, but you don’t have to.  The only different bit is that the form has to have ‘enctype=&#8221;multipart/form-data&#8221;’ as one of its fields in order to handle the file correctly.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;POST&quot;</span>&gt;</span>
	What would you like to call the image?: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_name&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_name&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	What caption should the image have?: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_caption&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_caption&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MAX_FILE_SIZE&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3000000&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	Pick an Image from your computer: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user_image&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>I’ve included the hidden field MAX_FILE_SIZE, but this is a really primitive form of checking the file size and is easily circumvented – you need to carry out proper checks in the next file…</p>
<p>The next bit is to take the file that we will get from that form and upload it to the server.</p>
<p>When a file is sent to another page, it is stored in one of the PHP global arrays called $_FILES.  This array is kind of built in to PHP in the same way as $_GET or $_POST so you don’t need to initialize it.  It is an associative array, so you can access more information about the file such as the filename, size, type etc. as well as its temporary location.  When a file is uploaded, it is first put into a temporary location so that you can work out if it should be kept or thrown away.  This tutorial is fairly simple and so I only have a very minor check to see if I don’t want it – I check the size and type:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">// At this point you should check the size and type of the file supplied by the application.  Obviously because we are dealing with images here, the supplied file should
// be an image type.  I am restricting it to .jpeg but you will probably want to check more.
if($_FILES['user_image']['type'] == &quot;image/jpeg&quot; &amp;&amp; $_FILES['user_image']['size'] &lt; 3000000)
{
	// … accept the file
}
else
{
	// The file was the wrong format or size
?&gt;
	&lt;p&gt;Error processing file.  It is the wrong format (.jpeg only) or too big.&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, assuming that we want to keep the file, we now need to start doing some prep work for its new home on our server.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// The file supplied is valid...Set up some variables for the location and name of the file.</span>
<span style="color: #000088;">$target_folder</span> <span style="color: #339933;">=</span> <span style="">'images/'</span>; <span style="color: #666666; font-style: italic;">// This is the folder to which the images will be saved</span>
<span style="color: #000088;">$upload_image</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_folder</span><span style="color: #339933;">.</span><span style="color: #990000;">basename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="">'user_image'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// The new file location</span></pre></div></div>

<p>This just sets up the target directory for the images to be stored in, and also the exact filename of the new image.<br />
Now that we know where it’s going to go to, we use the move_uploaded_file PHP function to move the file from its temporary location into the newly specified home.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">if(move_uploaded_file($_FILES['user_image']['tmp_name'], $upload_image)) 
{
	// Successful move…now do the image manipulation
}
else
{
	// There was a problem with the file and it wasn't moved to the server.
?&gt;
	&lt;p&gt;Error processing file.  It may be too large.&lt;/p&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can see here we try to move the file from its temporary location (tmp_name) to its new location ($upload_image).</p>
<p>In this example, I am asking the user to specify the name of the file and also a caption for the file.  Because of this, I need to get these variables and sanitize them:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mysql.php&quot;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Include the mysql file so that we can strip SQL from the variables (And we need the SQL connection later...)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The following 2 variables depend on what was requested in the form - if it was only the image itself, they are unnecessary.</span>
<span style="color: #000088;">$newname</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'image_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Get the supplied image name and sanitize it</span>
<span style="color: #000088;">$caption</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'image_caption'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// Get the supplied caption and sanitize it</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The following 2 variables specify the planned names for the 2 images (actual and thumbnail).  In this example, I specify .jpg as the extension, but if you are</span>
<span style="color: #666666; font-style: italic;">// allowing multiple file extensions you may need to extend this to deal with the possibilities.</span>
<span style="color: #000088;">$thumbnail</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$newname</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_thumbnail.jpg&quot;</span>; <span style="color: #666666; font-style: italic;">// Set the thumbnail name</span>
<span style="color: #000088;">$actual</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target_folder</span><span style="color: #339933;">.</span><span style="color: #000088;">$newname</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.jpg&quot;</span>; <span style="color: #666666; font-style: italic;">// Set the actual image name</span></pre></div></div>

<p>…it’s all just about variable manipulation and this kind of thing will very likely vary from any product you build.</p>
<p>The next section is where we actually produce the new image in thumbnail size using some of the functions from the GD library.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Get new sizes</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$upload_image</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$newwidth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">150</span>; <span style="color: #666666; font-style: italic;">// This can be a set value or a percentage of original size ($width)</span>
<span style="color: #000088;">$newheight</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">150</span>; <span style="color: #666666; font-style: italic;">// This can be a set value or a percentage of original size ($height)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Load the images</span>
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> imagecreatetruecolor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$newwidth</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newheight</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$source</span> <span style="color: #339933;">=</span> imagecreatefromjpeg<span style="color: #009900;">&#40;</span><span style="color: #000088;">$upload_image</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Resize the $thumb image.</span>
imagecopyresized<span style="color: #009900;">&#40;</span><span style="color: #000088;">$thumb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newwidth</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newheight</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// Save the new file to the location specified by $thumbnail</span>
imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000088;">$thumb</span><span style="color: #339933;">,</span> <span style="color: #000088;">$thumbnail</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The functions are fairly self-explanatory, but basically what we do is get all the sizes we need (original dimensions and new dimensions), then use the imagecreatetruecolor function to create the thumbnail ‘canvas’ using the new sizes, and load the existing image (the big one we just uploaded) using imagecreatefromjpeg.  Note that this function relies on use of a jpeg…you need to use other functions such as imagecreatefromgif to handle other file types.  Our final 2 stages are to copy the $source image to the $thumb image using the imagecopyresized function, then ‘print’ the $thumb image to the server using the imagejpeg function (again, for other file types use the relevant function).</p>
<p>Because I am wanting to specify my own name for the image, I will now rename the original:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">rename</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$upload_image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$actual</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The last thing to do now is to put the details into the database.  I am using the same database format I described for the <a href="http://progtuts.info/142/ajax-image-gallery/" target="_blank">AJAX Image Gallery tutorial </a> which this tutorial ties in with.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">if(mysql_query(&quot;INSERT INTO images (caption, thumbnail, actual) VALUES('&quot;.$caption.&quot;', '&quot;.$thumbnail.&quot;', '&quot;.$actual.&quot;')&quot;))
{
	// ...it did work properly.  Tell the user and do anything else you particularly feel like doing.
	?&gt;
	&lt;p&gt;Image Successfully uploaded!&lt;br /&gt;Actual image: <span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$actual</span>;?<span style="color: #339933;">&gt;</span>&lt;br /&gt;Thumbnail image: <span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$thumbnail</span>;?<span style="color: #339933;">&gt;</span>&lt;br /&gt;Caption: <span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$caption</span>;?<span style="color: #339933;">&gt;</span>&lt;/p&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// ...it didn't work properly.  Unlink (delete) the actual and thumbnail files and then notify the user.</span>
	<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$actual</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$thumbnail</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;p&gt;There was an error storing your image in the database.  All traces of it on the server have been deleted.&lt;/p&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What we do here is evaluate the success of the SQL query.  If it is successful, you can do whatever you feel like doing!  Otherwise, it’s a good idea to unlink (delete) the 2 images we have just spent so long making so that they aren’t in the way for future requests.</p>
<p>That’s it!  All the code we need is right there.</p>
<p>In order to use it, you have to take care that both the form and the uploading script are password protected because of their ability to put stuff on your server.  Make sure that you make the target directory the right place for the gallery to find them otherwise you’ll spend ages trying to find the images knowing that you had uploaded them but not knowing where they’ve gone!</p>
<h2>Conclusion</h2>
<p>With a pretty short and fairly simple script we can completely automate the process of making a thumbnail when uploading an image for use in a gallery.  This is a really handy thing to do because it saves the gallery having to load up a big file, only to show it as a small file.  </p>
<p>Of course, this tutorial has also shown how to upload any file, you just have to modify the requirements for the file and change what you do with the file once you have it (after all, you don’t want to try and resize a document file do you?).</p>
<p>I hope this has been of use to you and you can tie it in to any projects you are working with that require thumbnails – hopefully it will make things much easier!</p>
<p>Remember to pop over to the forum to talk to us about web related stuff, get help with some code or just a general chat.</p>
<p><a href="http://progtuts.info/wp-content/plugins/download-monitor/download.php?id=8" class="demobutton">Download the ZIP file</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://progtuts.info/211/ajax-and-google-integrated-search/" title="AJAX and Google Integrated Search">AJAX and Google Integrated Search</a></li>
<li><a href="http://progtuts.info/142/ajax-image-gallery/" title="AJAX Image Gallery">AJAX Image Gallery</a></li>
<li><a href="http://progtuts.info/92/login-system/" title="PHP Login System">PHP Login System</a></li>
<li><a href="http://progtuts.info/7/create-an-email-form-using-ajax/" title="Create an Email Form using AJAX">Create an Email Form using AJAX</a></li>
<li><a href="http://progtuts.info/237/plain-text-feedburner-subscriber-count/" title="Plain Text Feedburner (Google Version) Subscriber Count [Updated]">Plain Text Feedburner (Google Version) Subscriber Count [Updated]</a></li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/progtuts?a=jQAHqWf5D6Q:oYmURqS-luY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/progtuts?i=jQAHqWf5D6Q:oYmURqS-luY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jQAHqWf5D6Q:oYmURqS-luY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/progtuts?i=jQAHqWf5D6Q:oYmURqS-luY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jQAHqWf5D6Q:oYmURqS-luY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/progtuts?i=jQAHqWf5D6Q:oYmURqS-luY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jQAHqWf5D6Q:oYmURqS-luY:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/progtuts?d=TzevzKxY174" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/progtuts?a=jQAHqWf5D6Q:oYmURqS-luY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/progtuts?d=yIl2AUoC8zA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://progtuts.info/178/php-automatically-create-thumbnail-images/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
