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

<channel>
	<title>Web Cash &#187; Code Snippets</title>
	<atom:link href="http://www.earn-web-cash.com/category/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to Time Delay Posts in WordPress</title>
		<link>http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/</link>
		<comments>http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 17:06:28 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/</guid>
		<description><![CDATA[One reason I&#8217;ve heard people say that they prefer a full-blown CMS to WordPress is that WordPress doesn&#8217;t allow time delayed posting. I was about to figure out a way to mod WordPress to make this possible &#8211; until I realized that there is some decent built-in functionality for time delaying. You can easily delay [...]]]></description>
			<content:encoded><![CDATA[<p>One reason I&#8217;ve heard people say that they prefer a full-blown CMS to WordPress is that WordPress doesn&#8217;t allow time delayed posting.  I was about to figure out a way to mod WordPress to make this possible &#8211; until I realized that there is some decent built-in functionality for time delaying.</p>
<p>You can easily delay the release of your posts into the main indexes (front page, archives, &#8220;Recent Posts&#8221; list, etc).  With a little bit of code, you can also restrict people from viewing the page before its appointed &#8220;Published&#8221; date.<br />
<span id="more-173"></span></p>
<h4>Delaying Publication: Setting a Date</h4>
<p>If all you want to do is delay the date and time that an article is &#8220;released&#8221; on your page&#8217;s index, you don&#8217;t have to do a whole lot of work.</p>
<p>Every article you post is &#8220;stamped&#8221; with a time by WordPress.  When you hit the &#8220;Publish&#8221; button, it checks the current time on the server and attaches that to your post.</p>
<p>You may not have noticed, but there&#8217;s an option for you to edit this time while you are working on the article.  For example, you could set the date back a week and backdate one of your posts.  For our purposes, we&#8217;ll set the date <strong>ahead</strong> &#8211; and WordPress won&#8217;t display your post in any indexes until that date arrives.</p>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/03/edit-timestamp-ss.png' title='Screenshot of the Edit Timestamp Widget'><img class='alignright' src='http://www.earn-web-cash.com/wp-content/uploads/2008/03/edit-timestamp-ss.thumbnail.png' alt='Screenshot of the Edit Timestamp Widget' /></a>In the &#8220;Write&#8221; section of your Dashboard, you should have a column of widgets on the righthand side (Categories, Discussion, Post Slug, etc).  If you scroll down a bit, there should be one titled &#8220;Post Timestamp.&#8221;  If you don&#8217;t see anything, click the &#8220;+&#8221; button in the title.</p>
<p>To set a custom release date, select &#8220;Edit Timestamp.&#8221;  Then use the inputs to enter the date and time you want the article released.  Voila &#8211; you&#8217;re done.</p>
<h4>Restricting Access to the Page</h4>
<p>For most people, this is good enough.  Your readers aren&#8217;t going to be able to find the article without a link to it appearing on your front page or in your RSS feed.</p>
<p>However, if your readers <strong>do</strong> know the link to the page, somehow, they can still access it directly.  If you want to prevent that, you&#8217;ll need to add a few lines of code to your template files.</p>
<p>We&#8217;re going to edit the &#8220;single.php&#8221; template file &#8211; the file responsible for formatting and displaying a single post.  After the page retrieves the post, we&#8217;re going to perform a check to see if the published date of the article is in the future.</p>
<p>If it is, we&#8217;ll display an alternative message to tell the user the article isn&#8217;t published yet.  Otherwise, the article will be displayed as normal.</p>
<h4>Editing Single.php</h4>
<p>Your template file probably doesn&#8217;t look exactly like mine, but there should be some very basic similarities.</p>
<p>If you open single.php, the page is displayed within &#8220;the loop.&#8221;  It&#8217;s how every WordPress template works.  The loop always begins with a line similar to this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This checks to see if a page was found in the database.  If it was, it loads the page into memory and prepares to output it.  The loop will eventually end with a line like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We&#8217;re going to create a new if statement and place it in between these two lines.  The new template will look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<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: #000088;">$pubDate</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;h3&gt;Article Not Published Yet&lt;/h3&gt;
  &lt;p&gt;Sorry!  The article you request is not published 
    yet.  It should be available soon, so check back in a few days.&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  ...  Regular post processing goes here ...
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4>Creating the Conditional Statement</h4>
<p>The only thing we have to do now is create a functioning conditional statement that checks to see if the published date of the article is in the past or the future.</p>
<p>To do that, we&#8217;ll use a few functions &#8211; get_the_time (a WordPress function to get the post&#8217;s timestamp), strtotime (a PHP function to convert a date string to a timestamp), and time (a php function to get the current tie).</p>
<p>get_the_time doesn&#8217;t return the actual time stamp &#8211; it returns a formatted date string, like &#8220;March 9, 2008, 11:13:35.&#8221;  You can specify how it&#8217;s formatted, just like you would with PHP&#8217;s date function.</p>
<p>To get the timestamp, we&#8217;re going to format the time string in a special way so that strtotime() can convert it back to a full timestamp (preserving the exact time the article should be published).  Then we compare that timestamp to the system time and see if the date has passed or not.</p>
<p>Use this if statement in the code above, and you should be good to go.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span>get_the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F j, Y, H:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If you time delay an article and someone tries to access the article directly, they will now see your alternate message.  You should of course personalize that to something appropriate for your site.</p>
<p>You can also apply the same concept to &#8220;Pages&#8221; in WordPress.  You&#8217;ll need to edit the page.php template file and insert the same snippet of code there.</p>
<p>What are you waiting for?  Go write some articles for the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/08/time-delay-posts-wordpress/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to Work With Directories in PHP</title>
		<link>http://www.earn-web-cash.com/2008/03/06/php-directory-functions/</link>
		<comments>http://www.earn-web-cash.com/2008/03/06/php-directory-functions/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 21:16:37 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[file system]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/06/php-directory-functions/</guid>
		<description><![CDATA[PHP offers a number of different functions to work with the local directory structure. There are at least three fairly simple ways to create and output a list of files in a directory. Like the file functions, each method is slightly different. Knowing how each operates will help you choose which function to use when [...]]]></description>
			<content:encoded><![CDATA[<p>PHP offers a number of different functions to work with the local directory structure.  There are at least three fairly simple ways to create and output a list of files in a directory.</p>
<p>Like the file functions, each method is slightly different.  Knowing how each operates will help you choose which function to use when you want to work with directories in your PHP script.<br />
<span id="more-172"></span></p>
<h4>PHP 4 &#8211; opendir, readdir, closedir</h4>
<p>The traditional method of working with directories is very similar to the traditional method of working with files.  You open a directory and save it as a resource in a variable.  Then, you read through it until the end.  Finally, you close it to tidy things up.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'images'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">opendir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filename</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That simple script will open a directory (&#8216;images&#8217;), read every filename in the directory, and then clean up the directory resource.</p>
<p>The readdir() function goes through the files as they are stored in the filesystem.  There&#8217;s no real logic to their order &#8211; they just come the way they are.  Each time you use readdir(), the pointer in the $dir resource is moved to the next file.</p>
<h4>PHP 5 &#8211; A Convenience Function, scandir</h4>
<p>In PHP 5, a new convenience function was introduced to handle all of the major directory functions with one call.  Similar to the way file_get_contents handles all of the file writing, scandir handles all of the directory listing.</p>
<p>With a single function, scandir opens a given directory, reads all of the files into an array, and closes the directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirname</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'images'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filenames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filenames</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This slightly shorter snippet of code accomplishes the same thing as the original example.  As an added bonus, the filenames are already stored in an array ($filenames).  The filenames are also sorted alphabetically.</p>
<p>You could of course achieve the same end with the original directory functions &#8211; but you&#8217;d have to add more lines of code to store the data in an array and then sort it.  One of the biggest reasons to use scandir() is that it&#8217;s a convenience.</p>
<p>There&#8217;s also one functional difference between the two.  scandir() never actually accesses a file &#8211; so it&#8217;s &#8220;Last Accessed Time&#8221; will remain the same.  When you use readdir() to cycle through a directory, each file is accessed &#8211; resetting the &#8220;Last Accessed Time.&#8221;</p>
<p>This often won&#8217;t matter, but it could play an important role in a script that works directly with a file&#8217;s &#8220;Last Accessed Time.&#8221;</p>
<h4>The Versatile Choice &#8211; glob</h4>
<p>The most versatile choice is glob().  Like scandir, it returns an array of filenames.  However, glob allows you to specify a pattern to match &#8211; so only the matching filenames are returned.</p>
<p>The pattern follows all of the rules for wildcards when using a command like <code>dir</code>.  For example, this will get all of the files in the &#8216;images&#8217; directory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'images'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filenames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/*.*'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filenames</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is a very important difference between this and the other functions, though.</p>
<p>glob() returns paths, not filenames.  In this case, the output will be something like &#8216;images/image.jpg,&#8217; not just &#8216;image.jpg.&#8217;  The directory listing is based on the current working directory &#8211; so specifying a path (like we did) will cause that directory to be included in the returned path.</p>
<p>If you don&#8217;t want the full path shown, you can use some other functions to change the working directory &#8211; like using &#8216;cd&#8217; in the command line.  By default, the working directory is where the script is executing.  In this case, we could change the working directory to &#8216;images&#8217; to get filenames without the directory as part of the path.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">chdir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can also use the GLOB_BRACE flag to pass multiple file extensions.  For example, this would allow you to find all images with extension .jpeg, .png, or .gif.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">chdir</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filenames</span> <span style="color: #339933;">=</span> <span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*.{jpeg,png,gif}'</span><span style="color: #339933;">,</span> GLOB_BRACE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//  Notice there are NO spaces after the commas</span></pre></div></div>

<p>glob() can be quite a powerful tool, indeed.  If all you want is a list of files in a directory, you&#8217;ll probably want to stick to scandir() &#8211; so you don&#8217;t have to go messing with the working directory. </p>
<p>For anything else &#8211; like a selective list of files &#8211; you should make use of glob&#8217;s matching functionality rather than sorting through the filenames yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/06/php-directory-functions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Use Javascript to Parse a Query String</title>
		<link>http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/</link>
		<comments>http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 04:19:33 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/</guid>
		<description><![CDATA[Most server-side scripting languages, like PHP, come with built-in functionality for reading query strings. Javascript doesn&#8217;t have any kind of standard counterpart, but that doesn&#8217;t mean you can&#8217;t use query strings in Javascript. With some String functions, you can create your own function to parse a query string attached to the request url and store [...]]]></description>
			<content:encoded><![CDATA[<p>Most server-side scripting languages, like PHP, come with built-in functionality for reading query strings.  Javascript doesn&#8217;t have any kind of standard counterpart, but that doesn&#8217;t mean you can&#8217;t use query strings in Javascript.</p>
<p>With some String functions, you can create your own function to parse a query string attached to the request url and store it in an associative array &#8211; just like PHP would for you.<br />
<span id="more-171"></span></p>
<h4>What Are Query Strings For</h4>
<p>If you&#8217;re developing anything more than a basic informative page, you&#8217;ll need to pass information from one page to the next.  You might want to pass a search query, some form values, the answer to a quiz question, or any number of other things.  Passing this information is the first step in converting your page from a static bit of information into a dynamic application.</p>
<p>One way to pass information between pages is to use a query string.  That&#8217;s the long line of text after the &#8216;?&#8217; in a URL.  Kind of like this.</p>
<pre>http://www.google.com/search?hl=en&#038;q=widgets&#038;btnG=Google+Search</pre>
<p>That&#8217;s a standard query string attached to a Google search.  It sends a few values to the new page &#8211; hl (en), q (widgets), and btn (Google Search).  These values are then used to determine what is displayed on the new page.</p>
<p>In PHP, these values are automatically stored in an associative array &#8211; the $_GET array.  To make these values easily available to our Javascript code, we can write a function to do the same thing.</p>
<h4>The Structure of a Query String</h4>
<p>In order to break a query string down, we need to understand the rules for building one.</p>
<p>A query string always starts with &#8216;?&#8217;.  This separates the query string from the actual URL.</p>
<p>This is followed by key-value pairs, like &#8220;key=value&#8221;.  If there is more than one key-value pair, they are separated by an ampersand &#8211; &#8220;key1=value1&amp;key2=value2&#8243;.</p>
<p>Finally, these values are url_encoded.  Special characters &#8211; like spaces, ampersands, and equal signs &#8211; are converted to code so that they don&#8217;t interfere with the URL or the query string.</p>
<h4>Step One: Get the Request String and Separate the Query</h4>
<p>The first thing we need to do is get the entire request string and separate out just the query string.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//  Fetch the entire request string</span>
<span style="color: #003366; font-weight: bold;">var</span> href <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> hrefParts <span style="color: #339933;">=</span> href.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  [0] is the URL, [1] is the query string</span>
<span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>hrefParts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The request string is accessible through the window.location object.  We use the split method of the string class to explode it at the &#8216;?&#8217;.  This leaves us with two parts, and we save the entire query string in the variable <code>query</code>.</p>
<h4>Step Two: Get the Key Value Pairs</h4>
<p>At this point, our query string in the variable <code>query</code> might look something like this:</p>
<pre>hl=en&#038;q=widgets&#038;btnG=Google+Search</pre>
<p>To get the key-value pairs, we can use another split() call.  This time, we split the string at each ampersand (&amp;).  The resulting array will contain the strings &#8220;hl=en&#8221;, &#8220;q=widgets&#8221;, and &#8220;btnG=Google+Search&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> valuePairs <span style="color: #339933;">=</span> query.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Step Three: Loop Through and Create an Array</h4>
<p>Now we can loop through the array valuePairs, split each pair, and create a new array where each value is indexed by its key.  This gives us a nicely indexed array just like PHP would.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> getArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> each <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> pair <span style="color: #000066; font-weight: bold;">in</span> valuePairs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// [0] = key, [1] = value</span>
  <span style="color: #003366; font-weight: bold;">var</span> tempPair <span style="color: #339933;">=</span> pair.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Unescape converts the text back from URL encoding</span>
  getArray<span style="color: #009900;">&#91;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> unescape<span style="color: #009900;">&#40;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>At this point, getArray[] should hold all of the values in our query string.</p>
<h4>Why Bother?</h4>
<p>Good question.  If PHP (and other server side scripts) automatically parse query strings, why should we bother using Javascript for it?</p>
<p>One reason may be to use query strings to pass information on pages that don&#8217;t have any server side scripting enabled.  I was looking into a web design competition for high school students, and it doesn&#8217;t allow any server-side scripts &#8211; only HTML, CSS, and Javascript.</p>
<p>With this method, we could use query strings to bring a little dynamism to the party.</p>
<p>This is also helpful for reading query strings in general &#8211; not just the one in the request string.  For example, if you use Ajax to get a value from a php script, how is the output formatted?</p>
<p>It may be plain text if it&#8217;s just one variable, or it may be in an XML format.  If the output is just a handful of key-value pairs, it might be easier and more efficient to create a query string and parse it with this function.</p>
<p>Or&#8230; maybe it&#8217;s just fun to say &#8220;I can do this.&#8221;  That&#8217;s a good enough reason for me.</p>
<p>And as usual, here&#8217;s the full function, in case you had some trouble.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> parseQueryString<span style="color: #009900;">&#40;</span>fullHref<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> hrefParts <span style="color: #339933;">=</span> fullHref.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//  [0] is the URL, [1] is the query string</span>
  <span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>hrefParts<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> valuePairs <span style="color: #339933;">=</span> query.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> getArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">for</span> each <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> pair <span style="color: #000066; font-weight: bold;">in</span> valuePairs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// [0] = key, [1] = value</span>
    <span style="color: #003366; font-weight: bold;">var</span> tempPair <span style="color: #339933;">=</span> pair.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Unescape converts the text back from URL encoding</span>
    getArray<span style="color: #009900;">&#91;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> unescape<span style="color: #009900;">&#40;</span>tempPair<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> getArray<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  To use it, pass the request string or another href</span>
<span style="color: #006600; font-style: italic;">//  to the function.  It returns the array.</span>
<span style="color: #003366; font-weight: bold;">var</span> href <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> getArray <span style="color: #339933;">=</span> parseQueryString<span style="color: #009900;">&#40;</span>href<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/04/use-javascript-to-parse-a-query-string/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Enable Client-Side Caching of Resized Images</title>
		<link>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</link>
		<comments>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 00:37:44 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/</guid>
		<description><![CDATA[About a month ago, I wrote a tutorial on dynamically resizing images in PHP. Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created. However, one reader asked if it was possible to enable a client-side cache of a dynamically resized [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago, I wrote a tutorial on <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">dynamically resizing images in PHP</a>.  Despite my best intentions, I never found the time to go back and rewrite the script to include server-side caching of the images created.</p>
<p>However, one reader asked if it was possible to enable a client-side cache of a dynamically resized image.  It surely is, and it&#8217;s very simple.<br />
<span id="more-166"></span></p>
<h4>Dynamic Image Resizing: Revisited</h4>
<p>If you&#8217;ve got no clue how to resize an image in PHP, then you ought to go back and re-read the <a href="http://www.earn-web-cash.com/2008/01/30/resize-images-php/">original article</a>.  For the rest of you, here&#8217;s a quick refresher on what happens.</p>
<p>Our script takes a filename as a $_GET parameter.  If the file exists, the script loads it in as an image resource.  Using the GD library, we create a new image resource that is the proper size.</p>
<p>Finally, we use a content-type header to tell the browser to interpret the data as an image and we output the image data.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Enabling a Client Side Cache</h4>
<p>In order to enable a client-side cache, all we need to do is add an extra header to the mix.</p>
<p>There is a set of headers in HTTP to handle this sort of need &#8211; Cache-Control.  You can use this to enable caching, disable caching, and set some restrictions on what can be cached for how long.</p>
<p>The option we&#8217;re most interested in is max-age=[seconds].  This response header is sent along with the data to tell the browser that it can re-use a cached copy as long as it is no older than the specified age.</p>
<p>To enable this with PHP, add another header() call before the imagepng() call, and set a Cache-Control header with an appropriate max-age.  Like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: max-age=3600&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;content-type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Keep Request Queries Consistent</h4>
<p>With this system, you could set a pretty long max-age for the cache.  Assuming that the original image never changes, the user could cache a resized copy for months and your script would be happy.</p>
<p>However, you need to keep your query strings consistent for this to work.  Since your request includes a query string, that full string is included in the location that is cached.</p>
<p>According to your browser, it&#8217;s not the image that is cached, it&#8217;s the location&#8230;</p>
<pre>http://www.earn-web-cash.com/scripts/resize-image.php?image=http://www.earn-web-cash.com/wp-content/uploads/2008/01/dzone-screenshot.png</pre>
<p>If you were to change the query string in any way &#8211; leaving out the www, for example &#8211; the browser will think it is fetching a new resource and it won&#8217;t consult the cache.</p>
<p>This is especially important if you include size parameters in the query string to dynamically choose the new size.  You would need to be careful to keep the parameters in the same order every time for the cache to work properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/01/php-cache-resized-images/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>How to Randomize AdSense Ads with Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</link>
		<comments>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 23:45:30 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Advertising]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[ad]]></category>
		<category><![CDATA[AdSense]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</guid>
		<description><![CDATA[I came across this problem on the forums today. How can you randomize which Ad Sense ad to show using Javascript? The solution, it turns out, is very simple. Why Randomize Ads? There are a few reasons you might want to do this. If you&#8217;re looking into changing the theme of your AdSense ad, you [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem on the forums today.  How can you randomize which Ad Sense ad to show using Javascript?</p>
<p>The solution, it turns out, is very simple.<br />
<span id="more-164"></span></p>
<h4>Why Randomize Ads?</h4>
<p>There are a few reasons you might want to do this.</p>
<p>If you&#8217;re looking into changing the theme of your AdSense ad, you may want to test the two themes against each other.  By giving each theme approximately 50% of the impressions for a given time period, you can make some direct comparisons between their performance.</p>
<p>Or maybe you and a friend both run a website, so you want to split the advertising time between two AdSense publisher ids.  That can be done too.</p>
<h4>How the AdSense Code Works</h4>
<p>Before we randomize it, let&#8217;s take a look at how the AdSense code works.</p>
<p>Here&#8217;s the snippet of code that creates the skyscraper on the left side of this page.</p>

<div class="wp_syntax"><div class="code"><pre class="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>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">google_ad_client = &quot;pub-2399151883698113&quot;;</span>
<span style="color: #808080; font-style: italic;">/* Web Cash Tall Skyscraper */</span>
<span style="color: #808080; font-style: italic;">google_ad_slot = &quot;8702750734&quot;;</span>
<span style="color: #808080; font-style: italic;">google_ad_width = 160;</span>
<span style="color: #808080; font-style: italic;">google_ad_height = 600;</span>
<span style="color: #808080; font-style: italic;">//--&gt;</span>
<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: #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>
<span style="color: #009900;"><span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>You&#8217;ll notice two major pieces to this code.  The first piece &#8211; in the first <code>&lt;script&gt;</code> element &#8211; defines the ad unit&#8217;s properties.  It sets google_ad_client (your publisher id), google_ad_slot (the saved ad unit&#8217;s id with color info), google_ad_width, and google_ad_height.</p>
<p>The second section &#8211; the script element with src &#8220;show_ads.js&#8221; &#8211; actually displays the ad based on the variables that were set previously.</p>
<p>In order to vary the type of ad shown, we need to come up with a conditional statement to vary the google_ad_client/slot/width/height settings.</p>
<h4>The Javascript Code&#8230;</h4>
<p>And here&#8217;s the moment you&#8217;ve all been waiting for &#8211; the Javascript code to randomize which ad is shown.</p>

<div class="wp_syntax"><div class="code"><pre class="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: #339933;">&lt;!--</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;5396010225&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3900536874&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">//--&gt;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
&lt;/script&gt;</pre></div></div>

<p>You&#8217;ll notice that there&#8217;s an if/else statement, and each branch of the conditional statement defines a different ad to display.  The results of that conditional statement rely on the outcome of Math.random().</p>
<p>Previously, we built a <a href="http://www.earn-web-cash.com/2008/02/24/random-number-function">function to get a useful random number out of Math.random()</a>.  This is actually a situation where we can use the value from Math.random() just the way it is.</p>
<p>Math.random() generates a random value between 0 and 1.  In other words, it gives you a probability.  By checking that Math.random() is greater than 0.5, the statement should evaluate to true approximately 50% of the time.</p>
<p>We could also make the rotation more lop-sided.  For example (Math.random() > 0.70) would be true 30% of the time.  Therefore the first ad would be displayed 30% of the time, while the second ad would be displayed 70% of the time.</p>
<p>I hope you weren&#8217;t looking for something more complicated because, well, it isn&#8217;t.  It&#8217;s just that simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>How to Generate a List of Possible Combinations</title>
		<link>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/</link>
		<comments>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 23:16:53 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/26/get-all-combinations/</guid>
		<description><![CDATA[In statistics and probability, the combination is something special. There are tons of problems that revolve around how many combinations you can get choosing a certain number of random items from a set. We&#8217;ve developed mathematical formulas to help us determine the number of possible combinations &#8211; and therefore the possibility of getting any single [...]]]></description>
			<content:encoded><![CDATA[<p>In statistics and probability, the combination is something special.  There are tons of problems that revolve around how many combinations you can get choosing a certain number of random items from a set.</p>
<p>We&#8217;ve developed mathematical formulas to help us determine the number of possible combinations &#8211; and therefore the possibility of getting any single combination &#8211; but how can we generate a list of the actual combinations?<br />
<span id="more-161"></span></p>
<h4>Find the Number of Combinations: the Binomial Coefficient</h4>
<p>Previously, we looked at <a href="http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/">how to find the total number of combinations</a>.</p>
<p>Mathematically, we can use the binomial coefficient to figure this out.  With a given set size (i.e. 52 cards) and a given sample size (i.e. 7 cards per hand), we can write the equation &#8211; (52 Choose 7).  We could then re-write the equation as (52! / (7! * 45!) ).</p>
<p>In order to calculate this, we found the most efficient way was to construct two loops.  The first loop calculated the numerator &#8211; after having canceled out all of the numbers contained in 45!.  We then calculated the numerator by looping through 7!.  With simple division, we come up with the answer.</p>
<h4>Listing the Actual Combinations</h4>
<p>A more interesting task, though, is to figure out how to generate a list of these combinations.  To do that, let&#8217;s take a look at a manually generated list &#8211; (6 Choose 3) &#8211; and look for a pattern.</p>
<p>Here&#8217;s the first few combinations&#8230;</p>
<pre>123
12 4
12  5
12   6
1 34
1 3 5
1 3  6
1   45
1   4 6
1    56
 234
 23 5</pre>
<p>You&#8217;ll notice that there&#8217;s a pattern and that the combinations seem to be developing sequentially.  If we can understand that pattern and write a script to repeat it, we&#8217;ll have done our job.</p>
<p>Here&#8217;s the gist of the pattern.  We look at the last number.  If we can increment that, we do.  If not, we look back one number.  If we can increment, we do.  If we increment that number, we &#8220;rewind&#8221; the next number to be equal to it plus one.</p>
<p>For example, look at this series.</p>
<pre>12   6
1 34</pre>
<p>We started by incrementing the last number until it maxed out at 6.  Then we moved back one space, incremented that number, and set the other number equal to one greater than that.  The same thing happens when the last two numbers max out.</p>
<pre>1   56
 234</pre>
<p>You move back to the number that can be incremented, increment it, and then set the other numbers to one greater than the number incremented.</p>
<h4>Using Hard-coded Loops</h4>
<p>The easy (and cheating) way to do this is to hard code a series of nested loops that follow this pattern.</p>
<p>The general formula is (k Choose n) &#8211; where k is the total number of items in the set and n is the sample size being taken.  We will need to create <strong>n</strong> nested loops &#8211; represent each item being chosen in our sample set.</p>
<p>The outer loop will go from <strong>1</strong> to <strong>k &#8211; (n &#8211; 1)</strong>.  Why?  Because at <strong>k &#8211; (n &#8211; 1)</strong>, we can&#8217;t increment the outer number anymore.  Imagine the real numbers are (6 Choose 3).  Once the outer loop gets to 4, we won&#8217;t be able to increment it any further &#8211; we&#8217;ll be at the final combination, 4, 5, 6.</p>
<p>The inner loop will have a beginning value that depends on the outer loop.  If the outer loop is in its first iteration, the first inner loop starts at 2.  When the outer loop is in its second iteration, the first inner loop starts at 3.  It&#8217;s always one greater.  It&#8217;s maximum value is always the same &#8211; <strong>k &#8211; (n &#8211; 2)</strong> &#8211; for the same reason we created the upper limit for the outer loop.</p>
<p>This continues until we get to the inner-most loop.</p>
<p>Here&#8217;s what these nested loops might look like in PHP, generating the combinations for (6 Choose 3).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You&#8217;ll see that that script outputs the correct 20 combinations for (6 Choose 3).</p>
<h4>Can We Improve On That?</h4>
<p>Surely we can.  The main problem here is that we&#8217;ve hard-coded the loops.  This is ok for a short-term solution and a small number of items being chosen.</p>
<p>What if you had to choose twenty items from a larger set?  You&#8217;d have to hard-code twenty loops.  It makes the code a bit more unseemly.  This is also inflexible &#8211; you&#8217;ll have to change the code every time you have a different problem.</p>
<p>Instead, we need to look for a generalizable solution.  This is always the best bet, but sometimes it can be tempting to look for a one-off short term solution because it&#8217;s more readily visible.</p>
<p>There is, however, a generalizable solution to this problem.  The key lies in creating a function that finds the next combination using the sequential method outlined above.  It takes a combination and returns a combination &#8211; and you can continuously call the function to iterate through all of the combinations.</p>
<p>Think on that for a while.  It&#8217;s an interesting challenge.  I&#8217;ll explain a <a href="http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/">solution to it later in the week</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Javascript Function: Random Number Generator</title>
		<link>http://www.earn-web-cash.com/2008/02/24/random-number-function/</link>
		<comments>http://www.earn-web-cash.com/2008/02/24/random-number-function/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:07:28 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/24/random-number-function/</guid>
		<description><![CDATA[Yesterday, I wrote a short article on generating random numbers in Javascript. The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful. At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote a short <a href="http://www.earn-web-cash.com/2008/02/23/random-number-javascript/">article on generating random numbers in Javascript</a>.  The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful.</p>
<p>At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the general formula for this into a short helper function to generate a random number.<br />
<span id="more-158"></span></p>
<h4>Random Numbers: General Formula</h4>
<p>First, let&#8217;s take a minute and recap the general formula for finding a random number.</p>
<p>Javascript&#8217;s Math.random() method returns a random number between 0 and 1.  By multiplying this decimal value by a whole number, we can generate a desired range of values.  For example, by multiplying Math.random()&#8217;s result by 10, we get a range of 0 to 10.</p>
<p>Next, we take the Math.floor value of that random number.  This gives us just the integer portion &#8211; and it effectively cuts our range to 0 through 9.  Finally, we can modify this by adding an offset number.  So if we wanted a random number between 51 and 60, we&#8217;d add 51 to that result.</p>
<h4>Creating a Random Number Function</h4>
<p>The basic formula we end up with is&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> setSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> setOffset<span style="color: #339933;">;</span></pre></div></div>

<p>Our code would be much more readable if we had a helper function that could be called like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">randomNumber <span style="color: #339933;">=</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our function is going to take two parameters &#8211; min and max.  From these two parameters, we can calculate the offset and the set size and generate the proper random number.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> min<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> range <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>max <span style="color: #339933;">-</span> min<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> randomNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> range<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> offset<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> randomNumber<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All done.</p>
<p>Add that function to a utility file, and you&#8217;ve got a handy random number generator at your disposal.  It even uses the same format as PHP&#8217;s rand() function, to help increase consistency and readability in your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/24/random-number-function/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JS Game: Making a Real Game Board from a Concept</title>
		<link>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/</link>
		<comments>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 04:08:57 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/</guid>
		<description><![CDATA[In a recent post, I sketched out how to use Javascript to build a gameboard for a browser-based game &#8211; specifically Tic Tac Toe. After a little tweaking, I&#8217;ve worked this into a functioning Tic Tac Toe board. In this post, we&#8217;ll explore the code used to set up this board. Once that&#8217;s done, the [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent post, I sketched out how to <a href="http://www.earn-web-cash.com/2008/02/19/js-online-game-board/">use Javascript to build a gameboard for a browser-based game</a> &#8211; specifically Tic Tac Toe.  After a little tweaking, I&#8217;ve worked this into a functioning Tic Tac Toe board.</p>
<p>In this post, we&#8217;ll explore the code used to set up this board.  Once that&#8217;s done, the next step will be to create a php script to evaluate some of the game&#8217;s logic.<br />
<span id="more-136"></span></p>
<h5>Recap: The Game Board Concept</h5>
<p>First, let&#8217;s take a step back and recap our Javascript game board concept.</p>
<p>The basic building block of our game board is a div element.  This div element is styled with a CSS stylesheet &#8211; 50px by 50px.  By using floats and clearss strategically, we can stack 9 of these div elements into a 3&#215;3 box.</p>
<p>Javascript is added to make these boxes functional.  An &#8220;onClick&#8221; handler is used to call a Javascript function that changes the background image of the div.  This effectively marks the square as either a &#8220;Circle&#8221; or &#8220;Cross&#8221; for Tic Tac Toe.</p>
<p>To alter the div &#8211; and also to add textual output &#8211; we&#8217;ll be using Javascript&#8217;s powerful DOM functions.  These allow us to grab a specific element by it&#8217;s ID and change it.</p>
<h5>Creating Graphics</h5>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' title='Tic Tac Toe Circle Image'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' alt='Tic Tac Toe Circle Image' /></a>I created two images in Inkscape to use for this board.  Each is a 50&#215;50 png file &#8211; with a gray background and a black border.  The actual token (the circle or cross) is red and laid on top of the gray background.</p>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' title='Tic Tac Toe Cross Image'><img class="alignright" src='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' alt='Tic Tac Toe Cross Image' /></a>I&#8217;m no great artist, but these should work for a functional demo.  We can worry about making nice circle and cross tokens later.  I think getting the game <strong>working</strong> is more important than making it pretty from the get-go.</p>
<h5>Turning Our .js Script into a Class</h5>
<p>In making this a functional board, I also converted the script into an actual class.  The script may end up being somewhat large and unwieldy &#8211; and an object oriented approach may help us keep it tidy and clean.  Or it may add a lot of overhead&#8230; but I like objects.</p>
<p>In our HTML file, we&#8217;ll create the object like this.</p>

<div class="wp_syntax"><div class="code"><pre class="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>&gt;</span>
var tictactoe = new game();
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>In the attached .js file, we actually define the <strong>game</strong> object.  Here&#8217;s part of the object definition.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> game<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;">//  Array to hold the bgImgs</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'tttcircle.png'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'tttcross.png'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//  Player information			</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Player One&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Player Two&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>This class constructor does some of the initialization for us.</p>
<p>First, it creates an array with background images.  At the moment we&#8217;re only using two images.  However, this technique would be useful if you had a more complex map &#8211; with 10-15 images you could lay over a div.</p>
<p>Second, we create some player information variables.  The &#8216;currentPlayer&#8217; property is going to track whether the &#8216;cross&#8217; or &#8216;circle&#8217; player is currently taking a turn.  The &#8216;players&#8217; array will just hold the names of those players for now.</p>
<h5>The Background Image Changing Function</h5>
<p>One of the major methods of this class will be changeBackground().  Just like in the previous example, this method will change the background style of a given div tag.  This way we can change it from an open square to a circle or cross token.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changeBackground</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'box-'</span> <span style="color: #339933;">+</span> boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'transparent url('</span> <span style="color: #339933;">+</span> 
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bgImgs</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">') top left no-repeat'</span><span style="color: #339933;">;</span>
&nbsp;
    box.<span style="color: #660066;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changePlayer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This should be pretty straightforward.  We&#8217;re storing the &#8216;div&#8217; element in the &#8216;box&#8217; variable.  We&#8217;re then setting the &#8216;background&#8217; style as we would in a css style.  Remember that we stored the background images in an array (this.bgImgs) and this.currentPlayer corresponds to a key in the this.bgImgs array (either 0 or 1).</p>
<p>The &#8216;box.removeAttribute()&#8217; method is removing &#8216;onClick&#8217; from that div.  We can&#8217;t use a square a second time, so we might as well eliminate the onClick handler altogether.</p>
<p>Finally, this.changePlayer() is calling a new method.  This is going to help us switch from Player One&#8217;s turn to Player Two&#8217;s turn.</p>
<h5>One Turn to Another &#8211; this.changePlayer()</h5>
<p>The last method we need to declare for this class at the moment is changePlayer.</p>
<p>This will toggle the active player &#8211; which in turn affects whether a circle or cross is placed on the board.  For some added effect, we&#8217;ll also create a new html element to display a message that says who&#8217;s turn it is.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">changePlayer</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: #006600; font-style: italic;">//  Switch the active player</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Get a reference to our 'message' element and create the message</span>
    <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> msg <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;It is &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">players</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">currentPlayer</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'s turn.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> txt <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Erase any existing text</span>
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>box.<span style="color: #660066;">hasChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      box.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>box.<span style="color: #660066;">lastChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//  Add the text node (our message) to our element</span>
    box.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, this is pretty straightforward.  The DOM functions are amazingly simple &#8211; once you see how they work.</p>
<p>&#8216;box&#8217; is a reference to our element (id = message).  The msg variable is a temp variable I created to hold the string.  The &#8216;createTextNode&#8217; method creates a new block of text (with our msg) that we can then insert into an HTML element.</p>
<p>The while() loop is simply there to erase any old text.  As long as our &#8216;box&#8217; element has any child nodes inside of it (text or other HTML tags), the loop will execute and delete one of those child nodes each time.  This way we have a clean slate on which to write down who&#8217;s turn it is.</p>
<p><script type="text/javascript">var tictactoe = new game();</script></p>
<p style="float: left; margin-right: 1em;" class="message break" id="message">
<div class="break">
<div class="box" id="box-1" onClick="tictactoe.changeBackground('1');">1</div>
<div class="box" id="box-2" onClick="tictactoe.changeBackground('2');">2</div>
<div class="box" id="box-3" onClick="tictactoe.changeBackground('3');" style="margin-right: 1em;">3</div>
</div>
<div class="break">
<div class="box" id="box-4" onClick="tictactoe.changeBackground('4');">4</div>
<div class="box" id="box-5" onClick="tictactoe.changeBackground('5');">5</div>
<div class="box" id="box-6" onClick="tictactoe.changeBackground('6');" style="margin-right: 1em;">6</div>
</div>
<div class="break">
<div class="box" id="box-7" onClick="tictactoe.changeBackground('7');">7</div>
<div class="box" id="box-8" onClick="tictactoe.changeBackground('8');">8</div>
<div class="box" id="box-9" onClick="tictactoe.changeBackground('9');" style="margin-right: 1em;">9</div>
</div>
<h5>Seeing It In Action</h5>
<p>Well, that&#8217;s really all there is to it.  Have a look at this functioning demo.  If you had trouble piecing together the example code, you can download the source files (.js, .css, and two .png) from the list below.</p>
<p>Good luck, and be sure to check back soon.  Our next task is going to be a <strong>real</strong> challenge.  We need to add some <strong>logic</strong> to this game &#8211; evaluating victoary conditions and creating AI!</p>
<p>Source code files:</p>
<ul>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tictactoe-basic.js' title='Basic Tic-Tac-Toe JS File'>Basic Tic-Tac-Toe JS File</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tictactoe1.css' title='Tic-Tac-Toe CSS File (#2)'>Tic-Tac-Toe CSS File (#2)</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcross.png' title='Tic Tac Toe Cross Image'>Tic Tac Toe Cross Image</a></li>
<li><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/tttcircle.png' title='Tic Tac Toe Circle Image'>Tic Tac Toe Circle Image</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JS Game: Creating a Simple Game Board in Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/</link>
		<comments>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/#comments</comments>
		<pubDate>Tue, 19 Feb 2008 23:46:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/19/js-online-game-board/</guid>
		<description><![CDATA[A thorough knowledge of Javascript can give you tremendous control over what happens on a website. Combined with PHP through an AJAX request, you can make some pretty nifty browser-based games. This tutorial will focus on the first part of making a basic board game &#8211; creating a board. The end result will be a [...]]]></description>
			<content:encoded><![CDATA[<p>A thorough knowledge of Javascript can give you tremendous control over what happens on a website.  Combined with PHP through an AJAX request, you can make some pretty nifty browser-based games.</p>
<p>This tutorial will focus on the first part of making a basic board game &#8211; creating a board.  The end result will be a three by three game board suitable for use in developing a tic-tac-toe game.<br />
<span id="more-132"></span></p>
<h5>Game Development with JS, AJAX, and PHP</h5>
<p>How can we use JS and PHP &#8211; connected through AJAX &#8211; to develop an online game?</p>
<p>Javascript&#8217;s main use for us is to create an interface for the game.  Through Javascript we can capture user input &#8211; i.e. mouse clicks and text.  We can also alter the game&#8217;s output &#8211; changing the HTML of the page, adding images, and moving things around.  Javascript will provide a lot of the front end work.</p>
<p>PHP, on the other hand, is a more robust language for dealing with the logic of the game.  PHP could be useful for developing an AI and evaluating winning conditions.  It also offers a great way to store information for later use through flat files or database integration.</p>
<p>If we&#8217;re going to use both Javascript and PHP, we&#8217;ll need to use AJAX.  This is the glue that holds the whole thing together.  It will send input from the main Javascript to the PHP processing scripts.  The PHP script will then send info back to the Javascript and it will alter the page&#8217;s layout accordingly.</p>
<h5>Step One: Building a Game Board</h5>
<p>The first step in creating a graphical online game is to build a game board.  The most basic board I could think of &#8211; and therefore a good starting point &#8211; is a tic-tac-toe board.</p>
<p>First, we should define and create our basic building block.  A square.</p>
<p>In terms of HTML/CSS, our square will be a div tag.  We&#8217;ll be able to position and style it to our liking, and we can add javascript triggers like &#8220;onClick&#8221; to help run our Javascript.</p>
<div class="basicbox" id="box" onClick="makeBlack('box');"></div>
<p>Our first box is going to be a simple 50px x 50px square.  When you click it, it will change background colors to black.</p>
<p>Here&#8217;s the CSS we&#8217;ll use for the square.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</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;">50px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">thin</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Inside the html body we can create our box like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack();&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Finally, we need to define our Javascript function makeBlack().  We&#8217;ll access the div element through it&#8217;s ID (&#8216;box&#8217;) and change it&#8217;s background style to be black.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> makeBlack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'black'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we&#8217;ve got a building block!  We just successfully used Javascript and DOM to create a click-able box.  With some tweaking, we can turn this into a nifty 3&#215;3 grid for Tic Tac Toe.</p>
<h5>Step Two: Build a Grid</h5>
<p>For Tic Tac Toe, we&#8217;re going to need a 3&#215;3 grid.  We can modify our CSS a bit and create 9 separate divs to accomplish this.  Add the following lines to the CSS for .box, and create a new class .break.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.break</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> bloth<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This way our boxes line up in a row.  If we include a &#8220;break&#8221; class in our div, we can start a new line.  Therefore we should update the html like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('one');&quot;</span>&gt;</span>1<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;two&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('two');&quot;</span>&gt;</span>2<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('three');&quot;</span>&gt;</span>3<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;four&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('four');&quot;</span>&gt;</span>4<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;five&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('five');&quot;</span>&gt;</span>5<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;six&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('six');&quot;</span>&gt;</span>6<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box break&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;seven&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('seven');&quot;</span>&gt;</span>7<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;eight&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('eight');&quot;</span>&gt;</span>8<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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nine&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;makeBlack('nine');&quot;</span>&gt;</span>9<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>Notice that each box has it&#8217;s own unique ID &#8211; one to nine.  I also changed the makeBlack() function to include a parameter &#8211; an ID.  Let&#8217;s update the makeBlack() function to utilize this to change the specific box that we click on.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> makeBlack <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'black'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="break">
<div class="box" id="one" onClick="makeBlack('one');">1</div>
<div class="box" id="two" onClick="makeBlack('two');">2</div>
<div class="box" id="three" style="margin-right: .5em;" onClick="makeBlack('three');">3</div>
</div>
<div class="break">
<div class="box" id="four" onClick="makeBlack('four');">4</div>
<div class="box" id="five" onClick="makeBlack('five');">5</div>
<div class="box" id="six" style="margin-right: .5em;" onClick="makeBlack('six');">6</div>
</div>
<div class="break">
<div class="box" id="seven" onClick="makeBlack('seven');">7</div>
<div class="box" id="eight" onClick="makeBlack('eight');">8</div>
<div class="box" id="nine" style="margin-right: .5em;" onClick="makeBlack('nine');">9</div>
</div>
<h5>Step Three: Use a Background Image Instead of a Background Color</h5>
<p>Turning a box to a black background color isn&#8217;t that impressive.  But it&#8217;s easy to use as a demonstration.</p>
<p>For a game, it would be much more useful to make an image appear in our div.  We can do that by editing the &#8220;background&#8221; style of the div in the makeBlack() function.  Note: You might also want to change the name of the function to something more descriptive, like changeBackground().</p>
<p>Here&#8217;s how to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> changeBackground <span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> box <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>boxId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  box.<span style="color: #660066;">style</span>.<span style="color: #660066;">background</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'transparent url(image.png) top left no-repeat'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The property &#8220;box.style.background&#8221; accesses the &#8220;background&#8221; css style for the &#8220;box&#8221; id.  Give it a value just like you would in a css stylesheet.  In this case, we&#8217;re giving it a background of &#8220;image.png.&#8221;  You might want to change this to a &#8220;circle.png&#8221; or a &#8220;cross.png&#8221; that you create for your game.</p>
<h5>Step Four: Changing the onClick Event</h5>
<p>The last thing we want to look at in this introduction is how to change the onClick event for our div.  You might want to get rid of it altogether &#8211; or maybe change it to a new function.  For example, after turning the box black, you might want to make the onClick call a new function &#8211; makeWhite().</p>
<p>We can do this through another DOM method, similar to the way we accessed the styles before.  Assuming we have the same variable box that refers to our div, here&#8217;s what we need to do in our function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//  Get rid of onClick altogether</span>
box.<span style="color: #660066;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//  Change onClick to a new function</span>
box.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onClick'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'makeWhite();'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Where To Go From Here?</h5>
<p>Now that you&#8217;ve got a basic 3&#215;3 grid and you know how to make it respond to user input, you can get started on making a game.</p>
<p>Here are a few of the things you&#8217;ll want to think about next.</p>
<ul>
<li>I need some images to place in my boxes.</li>
<li>How Can I Alternate Players (and images)?</li>
<li>How Can I Determine Who Wins?</li>
<li>How Can I Make the Computer Play (AI!)?</li>
</ul>
<p>You can go ahead and get started on your own &#8211; but I&#8217;ll certainly return to this topic within the next week or two with some more examples.  After the next tutorial, we&#8217;ll have a working <a href="http://www.earn-web-cash.com/2008/02/20/js-game-board-complete/">Tic-Tac-Toe board driven solely by Javascript</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/19/js-online-game-board/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>How to Only Show the Digg Badge for Popular Posts</title>
		<link>http://www.earn-web-cash.com/2008/02/18/digg-badge-popular-posts/</link>
		<comments>http://www.earn-web-cash.com/2008/02/18/digg-badge-popular-posts/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 19:45:09 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Online Tools]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[digg]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/18/digg-badge-popular-posts/</guid>
		<description><![CDATA[Digg can be a great source of traffic and recognition for tech related blogs. Some people take it to the extreme though, and show the &#8220;Digg This&#8221; badge on every article they write. As others have pointed out, this can be counter-productive. People don&#8217;t like to read articles that have just one Digg &#8211; they [...]]]></description>
			<content:encoded><![CDATA[<p>Digg can be a great source of traffic and recognition for tech related blogs.  Some people take it to the extreme though, and show the &#8220;Digg This&#8221; badge on every article they write.</p>
<p>As <a href="http://www.pronetadvertising.com/articles/why-the-digg-button-gets-you-buried-not-dugg11995.html">others have pointed out</a>, this can be counter-productive.  People don&#8217;t like to read articles that have just one Digg &#8211; they seem unpopular.</p>
<p>So here&#8217;s a solution: use the <a href="http://www.earn-web-cash.com/scripts-plugins-and-modules/wp-plugin-digg-badge/">Conditional &#8220;Digg This&#8221; WordPress Plugin</a> to only show the Digg Badge for articles that have a certain number of Diggs.<br />
<span id="more-129"></span></p>
<h5>Why Shouldn&#8217;t You Show the Digg Badge on Every Article?</h5>
<p>It&#8217;s easy enough to plaster the Digg Badge in your template or use a plug-in to add it to every post.  Most of your posts aren&#8217;t going to be popular though.  You&#8217;ll get at most one or two Diggs&#8230; nothing to write home about.</p>
<p>When a user opens your article, he or she might be impressed by &#8220;100 Diggs.&#8221;  He or she definitely won&#8217;t be impressed by &#8220;1 Digg.&#8221;  That&#8217;s a hint that your article wasn&#8217;t anything special &#8211; so why should the reader keep going?</p>
<h5>Using the Plugin to Only Show the Badge on Popular Articles</h5>
<p>The <a href="http://www.earn-web-cash.com/scripts-plugins-and-modules/wp-plugin-digg-badge/">Conditional Digg This</a> plug-in was designed to address this issue.</p>
<p>All you need to do is download the source code, upload it to your plugins directory, unpack the tar.gz, and click &#8220;Activate.&#8221;  You don&#8217;t need to modify any settings or insert anything into your template.  It&#8217;s all automated.</p>
<h5>How Does It Work?</h5>
<p>The basic concept behind the plugin is this:</p>
<ul>
<li>Load a story in your WordPress Blog</li>
<li>Check the Digg API to see how many diggs the article has</li>
<li>If the article has 10 or more diggs, show the badge&#8230;</li>
<li>Otherwise, leave it alone.</li>
</ul>
<p>Originally, I coded the plug-in to use a php function to access the API and check the number of Diggs each article had as it loaded.  This worked perfectly &#8211; but it took a while.  Sometimes, the API is slow to respond.  If the API took 8 seconds to spit back a response, that meant your page would take 8 extra seconds to load up.</p>
<p>Even if the API was responding quickly, this was a problem for the front page and category pages.  At best, the API takes about .25 to .5 seconds to respond &#8211; and with 10-15 checks on a page&#8230; you&#8217;re still waiting a good long time for the page to load.</p>
<h5>Load First, Check the API Later</h5>
<p>The solution I came up with was to use some nifty AJAX to perform the API checking behind the scenes.</p>
<p>The plug-in creates an AJAX request for each article.  It sends information to a php script which accesses the Digg API behind the scenes.  The page loads up instantly and AJAX will quietly do its thing.  When the response is ready, it&#8217;ll change the page to include the Digg This badge if necessary.</p>
<p>So, the next time you want to display a &#8220;Digg This&#8221; badge on your article, think about how many diggs your article has already.  Or, use the <a href="http://www.earn-web-cash.com/scripts-plugins-and-modules/wp-plugin-digg-badge/">Conditional &#8220;Digg This&#8221; WordPress Plugin</a> to automatically add a badge to your popular articles.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/18/digg-badge-popular-posts/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

