<?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; HTML Tutorials</title>
	<atom:link href="http://www.earn-web-cash.com/category/html-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sat, 02 Jan 2010 04:26:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Marking Up and Styling an Image Gallery</title>
		<link>http://www.earn-web-cash.com/2008/03/25/semantic-markup-image-album/</link>
		<comments>http://www.earn-web-cash.com/2008/03/25/semantic-markup-image-album/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 00:32:36 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Images]]></category>
		<category><![CDATA[Markup]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/25/marking-up-and-styling-an-image-gallery/</guid>
		<description><![CDATA[While working on a new site (Rolling Horde), I decided to include a small image gallery.  I randomly display four images from a flickr album in the sidebar.
The pictures are presented in the mark-up that flickr creates, and I used the styling that came with the template I was working from.  It creates [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a new site (<a href="http://rolling-horde.net">Rolling Horde</a>), I decided to include a small image gallery.  I randomly display four images from a flickr album in the sidebar.</p>
<p>The pictures are presented in the mark-up that flickr creates, and I used the styling that came with the template I was working from.  It creates a simple two by two square of photos, with a slight gap in between them.</p>
<p>Although I like the way they are presented, I&#8217;m not sure I like the mark-up that flickr creates.  What would be the simplest, semantic way to mark-up and style images in a small album like this?<br />
<span id="more-181"></span></p>
<h4>Semantic Mark-up: An Unordered List</h4>
<p>Flickr places all of the photos in divs.  These are then styled to float next to each other or appear however you like.</p>
<p>In my opinion, the most semantic way to mark-up photos like this would be to create an unordered list.  A photo album is essentially a list of photos.</p>
<p>This could be an ordered list if the photos were intended to be sequential &#8211; like a photo essay or a comic book &#8211; but the average photo album is just a collection of pictures.</p>
<p>The mark-up would look something 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;">ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;images&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></div></div>

<p>The images would then be displayed in a long, vertical list.</p>
<h4>Styling &#8211; Making the List Inline</h4>
<p>In the end, we&#8217;d like to have the images sit side by side.  Ideally, they&#8217;d form a rectangle of either 2 x 3 pictures, or 3 x 2.</p>
<p>The first thing we want to do, then, is remove the list style type, clear the padding/margins of the list, and make the images appear next to each other.  The last bit we can achieve by giving the list items the &#8220;display: inline&#8221; css declaration.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#images</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">list-style-type</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#images</span> li <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This would create a simple horizontal list, like this&#8230;</p>
<ul id="images_one">
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
</ul>
<h4>Width: Restricting the Number of Columns</h4>
<p>Notice how the list terminates at the end of its container div and continues on a second line?  </p>
<p>Without a width declaration, the unordered list expands as large as wide as it can.  When that width limit is hit, it starts a second line, and continues until all of the list items have been displayed.</p>
<p>We can use this to our advantage by setting a width declaration.  This allows us to limit the number of images to be displayed in each row.</p>
<p>If our images are 75px square, and we want to display three images across, we can set our width to be about 250px.  The extra space can be used later for paddings and margins &#8211; but the images will appear left justified within the list container.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#images</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">list-style-type</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">250px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#images</span> li <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This would create a list like this&#8230;</p>
<ul id="images_two">
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
</ul>
<h4>Finishing Touches: Padding and Borders</h4>
<p>With the basic structure complete, we can add a few finishing touches.</p>
<p>First, we might we to add a little padding to the images.  This would space them out a few pixels from each other so that they don&#8217;t appear so cramped.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#image</span> img <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Adding this line to the css file would change the image album to this.</p>
<ul id="images_three">
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
</ul>
<p>Finally, a cool effect for images is to add a border around the image.  If you include a padding value for the img tag, the border is placed 2-3 pixels away from the edge of the picture.  It gives a nice framing effect.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#image</span> img <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</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>This final change would look something like this.</p>
<ul id="images_four">
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
<li><img src="http://farm4.static.flickr.com/3047/2362739858_c82b4de7c2_s.jpg" /></li>
</ul>
<p>By using an unordered list to mark-up your image collection, you end up with semantically correct and simple to style content.  Much better, in my opinion, than a string of divs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/25/semantic-markup-image-album/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash Movies: Standards Compliant Embedding</title>
		<link>http://www.earn-web-cash.com/2008/03/18/flash-movies-standards-compliant-embedding/</link>
		<comments>http://www.earn-web-cash.com/2008/03/18/flash-movies-standards-compliant-embedding/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 22:27:13 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[slideshow]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/18/flash-movies-standards-compliant-embedding/</guid>
		<description><![CDATA[The other day, I was looking into using Picasa to create an image album and embed it as a slideshow on a website.
Uploading the images and creating the album was easy.  However, I noticed that the code Google creates to embed the slideshow (a flash file) is not standards compliant &#8211; it uses the [...]]]></description>
			<content:encoded><![CDATA[<p>The other day, I was looking into using Picasa to create an image album and embed it as a slideshow on a website.</p>
<p>Uploading the images and creating the album was easy.  However, I noticed that the code Google creates to embed the slideshow (a flash file) is not standards compliant &#8211; it uses the <code>&lt;embed&gt;</code> tag.</p>
<p>How can we convert this code to a standards compliant version?<br />
<span id="more-178"></span></p>
<h4>The Original Picasa Embed Code</h4>
<p>The code that Google generates to embed the flash file uses the embed element.  The embed tag includes all of the pertinent information &#8211; the swf file, the height, the width, and the parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;embed <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;application/x-shockwave-flash&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://picasaweb.google.com/s/c/bin/slideshow.swf&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;288&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;192&quot;</span></span>
<span style="color: #009900;">  flashvars<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;host=picasaweb.google.com&amp;RGB=0x000000&amp;feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Fslowwalkere%2Falbumid%2F5178325186511467969%3Fkind%3Dphoto%26alt%3Drss&quot;</span></span>
<span style="color: #009900;">  pluginspage<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.macromedia.com/go/getflashplayer&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>embed&gt;</span></pre></div></div>

<p>The problem here is that the code uses the embed tag.  This is no longer recognized as a valid html element &#8211; although it still functions in most browsers.</p>
<h4>Converting Embed to Object</h4>
<p>We can, however, convert this embed element into an object element.  The object element <strong>is</strong> standards compliant, and <a href="http://www.alistapart.com/articles/flashsatay">with a few parameters</a> it is compatible across most browsers.</p>
<p>To do this, let&#8217;s identify the information that we&#8217;ll need from the original Picasa code.</p>
<p>Obviously, we&#8217;ll need the flash file.  This is the value of the &#8220;src=&#8221; attribute.  We&#8217;ll also need the height and width, which are pretty obvious.  Finally, we&#8217;ll need the parameters to be passed to the flash movie &#8211; located in the flashvars parameter.</p>
<p>Here&#8217;s what an object element might look like to display a flash movie.</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;">object</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;application/x-shockwave-flash&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">data</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flash-file.swf&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;400&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;movie&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flash-file.swf&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">object</span>&gt;</span></pre></div></div>

<p>We create the object and give it the attribute <code>type="application/x-shockwave-flash"</code>.  This tells the browser it is a flash movie.</p>
<p>We pass the name of the flash file with the data attribute and the &#8220;movie&#8221; param element &#8211; notice that the filename is passed twice here.</p>
<p>Finally, we pass the height and width parameters as attributes to the object element.</p>
<h4>Filling Out the Object</h4>
<p>Now we can take the values provided for us by Google and create a new, valid object to display the flash slideshow.</p>
<p>The only extra thing we need to know is that we can send another param element &#8211; name=&#8221;flashVars&#8221; &#8211; to contain the flash parameters.</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;">object</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;application/x-shockwave-flash&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">data</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://picasaweb.google.com/s/c/bin/slideshow.swf&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;288&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;192&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;movie&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://picasaweb.google.com/s/c/bin/slideshow.swf&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flashVars&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;host=picasaweb.google.com&amp;amp;RGB=0x000000&amp;amp;feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Fslowwalkere%2Falbumid%2F5178325186511467969%3Fkind%3Dphoto%26alt%3Drss&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">object</span>&gt;</span></pre></div></div>

<p>This should do the trick and display our Flash slideshow in a standards compliant way.  You should also be able to apply this to any other widget that displays a flash movie.</p>
<p>Also, here are two minor things to keep in mind.</p>
<p>The param elements are self closing &#8211; i.e. <code>&lt;param /&gt;</code>.  The param element doesn&#8217;t contain any text, so it must be self closing to be standards compliant.</p>
<p>The flashVars value is constructed as a query string.  It utilizes ampersands (&amp;) to separate key/value pairs.  Ampersands are a standards no-no &#8211; they should be escaped as an html entity (<code>&amp;amp;</code>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/18/flash-movies-standards-compliant-embedding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Spoof a Form, or Why Your Form Isn&#8217;t Safe</title>
		<link>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/</link>
		<comments>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 00:54:03 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spoof]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/</guid>
		<description><![CDATA[Forms can create all kinds of security holes in PHP applications.  The biggest reason for this is that you can never be sure just what kind of input you&#8217;ll be getting.
There are some steps you can take later on to protect your applications from this unknown input.  But first, let&#8217;s take a look [...]]]></description>
			<content:encoded><![CDATA[<p>Forms can create all kinds of security holes in PHP applications.  The biggest reason for this is that you can never be sure just what kind of input you&#8217;ll be getting.</p>
<p>There are some steps you can take later on to protect your applications from this unknown input.  But first, let&#8217;s take a look at <strong>why</strong> you can never trust a user&#8217;s input and just how easy it is for someone to spoof a form.<br />
<span id="more-160"></span></p>
<h4>What Does It Mean to Spoof a Form?</h4>
<p>It could mean a number of related things, but generally speaking it means submitting form information in a manner other than the original form was intended.  The form processing script, completely unaware of the situation, processes the input as if it came from the original form.</p>
<p>For example, if a form generates a URL query string, one way to spoof that form would be to modify the query string and re-visit the URL.  The new page would think that my query came from the original form &#8211; even though I really crafted the query myself.</p>
<p>Why would someone want to do this?  It may be curiosity.  &#8220;I wonder what happens if I change this variable&#8230;&#8221;  Or it may be malicious.  A hacker can spoof a form processing script to send dangerous input to your PHP script.</p>
<h4>The Problem With the &#8220;Get&#8221; Method</h4>
<p>It is easiest to spoof a form if the information is transmitted through the &#8220;Get&#8221; method.  This means that the variables are placed into a query string and appended to the form&#8217;s action URL.</p>
<p>Let&#8217;s say you have a form with three elements &#8211; a textarea element, a hidden input, and a select element.  Logically, you might think that the hidden input is restricted to whatever value you set it to and the select element is restricted to one of the options you provided.</p>
<p>To make this a little more concrete, let&#8217;s assume the textarea contains a post to a multi-user blog, the hidden input contains the author&#8217;s name, and the select element includes the category number.</p>
<p>When the user enters the values and clicks &#8220;submit,&#8221; he&#8217;s sent to a URL that looks something like this.</p>
<pre>processing.php?text=Bla+bla+bla&#038;author=Walkere&#038;catid=2</pre>
<p>What if I wanted to pretend to be someone else &#8211; like Bob?  I could simply change the &#8216;author&#8217; variable.</p>
<pre>processing.php?text=Bla+bla+bla&#038;author=Bob&#038;catid=2</pre>
<p>Maybe I want to post my message in a special category &#8211; one that isn&#8217;t normally open to me.  Again, it&#8217;s as simple as changing the catid variable to another number.</p>
<p>Now, a well designed script will notice these data flukes and either ignore them or spit out an error.  However, if you have too much faith in form input &#8211; and you blindly used the values from your form &#8211; you&#8217;d soon be in a mess of trouble.</p>
<h4>But Post Is More Secure&#8230; Right?</h4>
<p>The &#8220;Post&#8221; method is somewhat more secure.  Why?  Because all of the information is transmitted behind the scenes.</p>
<p>When I was younger, I tried to meddle with the $_GET variables of an online game that I played.  I never would have known enough about the workings of the script to spoof a &#8220;post&#8221; form &#8211; but it was so blatantly obvious with &#8220;get.&#8221;</p>
<p>However, someone &#8220;in the know&#8221; can easily spoof a form using the post method as well.  One way to do this is&#8230; write your own form.</p>
<p>If I look at the HTML source of your form, I can easily see what values you&#8217;re submitting to the processing script.  I can recreate those input elements on a form on <strong>my</strong> site &#8211; and then set the action to your processing script.</p>
<p>In this way, I could make the hidden and select statements regular text inputs &#8211; and input whatever values I want.  If you&#8217;re form processing script isn&#8217;t savvy, my custom values will get passed on to the database.</p>
<p>One thing you can do to prevent this is to check the referring URL coming into your script.  This will prevent someone from simply creating a form off-site to spoof your script.</p>
<h4>Sending an Actual HTTP Request</h4>
<p>The final, and most devious way, to spoof a form is to craft your own HTTP request in PHP.  This is essentially what a form does &#8211; but you can manually craft the request to your liking instead of letting a form do it.</p>
<p>How do we do this?</p>
<p>First, we create a stream context.  To do that we create a simple array and then call the function stream_context_create.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'method'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'headers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Referer: http://www.google.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// A regular query string with parameters</span>
&nbsp;
<span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$processingUrl</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fpassthru</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The actual form values are placed in the $data variable.  This is in a typical query string format &#8211; key=var&#038;key2=var2, etc.  The values should also be url encoded.</p>
<p>The nifty thing here is that you can manually specify HTTP headers to be sent with your request.  In this example, we set the referring URL to Google.  If someone knew your processing script checked the referrer&#8217;s address &#8211; they could simply spoof it with an HTTP header.</p>
<h4>Never Trust Input</h4>
<p>The bottom line is &#8211; never trust input that you haven&#8217;t thoroughly tested, validated, and cleaned.</p>
<p>Even if an input item is supposed to be hidden or supposed to come from a specific set of values &#8211; you can&#8217;t be sure that the form processing script is going to receive the correct value.  The script needs to check the info and make sure that it got what it was looking for.</p>
<p>It&#8217;s pretty simple for someone to spoof your form and send any input they like to your processing script.  If you don&#8217;t close the security holes, then someone will eventually blow them open for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RSS Feed: Building an RSS Data Feed in PHP with SimpleXML</title>
		<link>http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/</link>
		<comments>http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 01:48:07 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/</guid>
		<description><![CDATA[RSS feeds are a must-have for modern websites.  It&#8217;s easy enough to make an RSS feed of recent articles in PHP.  But did you know an RSS feed can simply be information &#8211; not links to articles?
This article will look at how to build a basic feed of data &#8211; for our purposes, [...]]]></description>
			<content:encoded><![CDATA[<p>RSS feeds are a must-have for modern websites.  It&#8217;s easy enough to <a href="http://www.earn-web-cash.com/2008/02/14/create-rss-feed-php/">make an RSS feed of recent articles in PHP</a>.  But did you know an RSS feed can simply be information &#8211; not links to articles?</p>
<p>This article will look at how to build a basic feed of data &#8211; for our purposes, we&#8217;ll consider random quotes.  It will also illustrate how to use SimpleXML to build the feed for us.<br />
<span id="more-145"></span></p>
<h5>The RSS 2.0 Standard</h5>
<p>RSS feeds, like XML in general, have specific standards.  In order to be use-able across platforms, it is imperative that standard syntax and naming patterns are used.  If you&#8217;re unfamiliar with the RSS 2.0 standard, you should look through a <a href="http://cyber.law.harvard.edu/rss/rss.html">copy here</a>.</p>
<p>Before we get to building the feed, let&#8217;s identify what tags we&#8217;re going to use.</p>
<p>The entire feed is wrapped in an <strong>&lt;rss&gt;</strong> tag.  Within that element is a <strong>&lt;channel&gt;</strong> element.</p>
<p>The <strong>&lt;channel&gt;</strong> element has a number of optional child elements.  The basic ones we&#8217;ll want to include are &#8211; <strong>&lt;title&gt;</strong>, <strong>&lt;description&gt;</strong>, <strong>&lt;link&gt;</strong>, and <strong>&lt;pubDate&gt;</strong>.</p>
<p>The actual data (our random quotes) is wrapped in <strong>&lt;item&gt;</strong> elements.  These also have a bunch of optional parameters.  We&#8217;ll be using the basics &#8211; <strong>&lttitle;&gt;</strong>, <strong>&lt;description&gt;</strong>, and <strong>&lt;pubDate&gt;</strong>.  Notice that there&#8217;s no link &#8211; because we&#8217;re feeding simple data, not articles!</p>
<h5>Creating the SimpleXML Object</h5>
<p>In order to create the SimpleXML object, you need to feed it some kind of XML string.  Normally this would be a completed file or string &#8211; like an existing feed.</p>
<p>Since we&#8217;re <strong>building</strong> the feed with SimpleXML, we don&#8217;t have a lot to start with.  We can give it a very basic string to create the object &#8211; an opening and closing <strong>rss</strong> element.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;rss version=&quot;2.0&quot;&gt;&lt;/rss&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Adding Child Elements</h5>
<p>The <code>addChild()</code> method of SimpleXML allows us to add child elements to existing elements.  In addition to adding a child element, the method returns a reference to the new child &#8211; allowing us to manipulate it in turn.</p>
<p>The first child we&#8217;ll need to create is the <strong>channel</strong> element.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$channel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'channel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That creates the <strong>channel</strong> element and stores a reference to it in $channel.  We can then use the $channel variable to add more children &#8211; the information defining the <strong>channel</strong> element itself.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Test Feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.earn-web-cash.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'This is a feed testing how to build an RSS feed in SimpleXML'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s T&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You may want to include other optional parameters that are included in the specification.  Just add another $channel->addChild() call with the parameter&#8217;s name and its value.</p>
<h5>Creating the Random Quote Items</h5>
<p>At this point, we just need to add our <strong>item</strong> elements and we&#8217;re done.  To do that, we&#8217;ll loop through an array of data, create an <strong>item</strong> element, and add the appropriate children to it.</p>
<p>Let&#8217;s assume that I retrieved information from a database table with three columns &#8211; title, description, and pubDate.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$newItem</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s T&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, you can add extra data to your feed by adding extra child elements.</p>
<h5>Outputting the Feed</h5>
<p>Now that the entire feed is stored in the $xml variable, we just need to output it.</p>
<p>In doing this, we&#8217;ll do two things.  We need to define the content-type as xhtml/xml.  Then we use the asXML() method to print the information in an xml string.</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;">'Content-Type: application/xhtml+xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You could alternatively save this to an xml file.  There are two ways to do this &#8211; either file_put_contents the output from asXML() or pass a filename as the parameter for asXML().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</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: #666666; font-style: italic;">//  or ...</span>
<span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Start Feeding</h5>
<p>With this basic script, you can serve up any kind of information as an RSS feed.  This makes it easy for people to read it in their RSS readers or even access the information on the cell phones.</p>
<p>There are plenty of types of data you could use this for &#8211; quotes, jokes, question of the day, trivia, tip of the day, etc.  By publishing tiny bits of data on a daily basis, you keep your users coming back for more &#8211; but you don&#8217;t need to publish full size articles every day.</p>
<p>In case you ran into trouble putting the script together, here&#8217;s the entire source code with an array of data built into it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mark Twain on Profanity'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Under certain circumstances, profanity provides a relief denied even to prayer.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Woody Allen on People'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'It seemed the world was divided into good and bad people. The good ones slept better... while the bad ones seemed to enjoy the waking hours much more.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Will Rogers on Vets'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'The best doctor in the world is the veterinarian. He can\'t ask his patients what is the matter - he\'s got to just know.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Create the root element - RSS</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;rss version=&quot;2.0&quot;&gt;&lt;/rss&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Create the channel element, a child of RSS</span>
<span style="color: #000088;">$channel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'channel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Add random channel information</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Random Quotes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.earn-web-cash.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'This is a feed of random, interesting quotes.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s T&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Loop through and create item elements</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$newItem</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Output</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/xhtml+xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><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/02/21/create-rss-feed-simplexml/feed/</wfw:commentRss>
		<slash:comments>2</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, [...]]]></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>4</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 [...]]]></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>13</slash:comments>
		</item>
		<item>
		<title>How to Access XML Attributes in PHP Using SimpleXML</title>
		<link>http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/</link>
		<comments>http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 19:29:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/</guid>
		<description><![CDATA[A user on Programming Talk had a problem the other day.  He was trying to build an XML parser to work with an xml data file (a character sheet from the WoW Armory).

His parser wasn't working - because all of the information was stored in attributes instead of child elements.  What's the difference between the two?  And how can we fix it?

Use SimpleXML.]]></description>
			<content:encoded><![CDATA[<p>A user on Programming Talk had a problem the other day.  He was trying to build an XML parser to work with an xml data file (a character sheet from the WoW Armory).</p>
<p>His parser wasn&#8217;t working &#8211; because all of the information was stored in attributes instead of child elements.  What&#8217;s the difference between the two?  And how can we fix it?</p>
<p>Use SimpleXML.<br />
<span id="more-114"></span></p>
<h5>Difference Between a Child and an Attribute</h5>
<p>For starters, let&#8217;s take a look at a sample XML file.  This will help illustrate the difference between a child element and an attribute, and it will give us something to work with later on.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;library<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>John Steinback<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Of Mice and Men<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>F. Scott Fitzgerald<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Great Gatsby<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/library<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This simple XML file is a &#8220;library.&#8221;  It contains a list of books &#8211; each of which has an author, a title, and a &#8220;lang&#8221; attribute.</p>
<p>In this example, &#8220;book&#8221; is an element.  It is a <strong>child</strong> element of library &#8211; which means it is an element created <strong>inside</strong> the open library element.  You could also refer to the library element as the <strong>parent</strong> of all the book elements.</p>
<p>To create a child element, you simply create a set of tags within a larger set of tags.  The book is a child of library, while the author is a child of book.  We could create more children for &#8220;Author&#8221; &#8211; like age, sex, and hair color.</p>
<p>By contrast, &#8220;lang&#8221; is an attribute.  It is defined inside the opening tag of an element.  By declaring <code>lang="en"</code> we are saying that the book has the attribute of being written in English.</p>
<p>It is simply another (perhaps more annoying) way to store information in an XML file.  Most basic XML parsers look for tags and content &#8211; but the attribute is neither.  It&#8217;s contained inside a tag, so it needs to be handled specially.  </p>
<p>Thankfully, SimpleXML is designed to help us do just that.</p>
<h5>Loading the File in SimpleXML</h5>
<p>I saved the xml file above in &#8220;xmltest.xml.&#8221;  In my php script, I can load it into a SimpleXML object with this function call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'xmltest.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This creates a SimpleXML object which is a tree of connected objects.  The root object represents the &#8220;library&#8221; element.  It then has an array of children called &#8220;book,&#8221; and each &#8220;book&#8221; element has two children element of its own.</p>
<p>Using the SimpleXML object structure, we can access some pieces of our XML document like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//  All of the info in Book 1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">author</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// First book, John Steinbeck</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Second book, The Great Gatsby</span></pre></div></div>

<h5>Accessing the Lang Attribute in the Object Tree</h5>
<p>The initial question now resurfaces &#8211; how do we access an attribute, like the &#8216;lang&#8217; attribute?</p>
<p>All of the elements are nicely arranged in a tree.  The parent element has a child element, which has a child element, so on and so forth.  We can easily access these as related objects like we did above.</p>
<p>The attributes are exploded into an array and attached to the element that they define.  So, for example, we defined a &#8216;lang&#8217; attribute for our book element.  Therefore, the value of lang will reside in an array attached to that specific book element.</p>
<p>We can access it like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lang'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// en</span></pre></div></div>

<p>To find an attribute of an object with SimpleXML, you navigate to the element in question and then access an array indexed by the names of the attributes.  In this case, our attribute was named &#8216;lang&#8217; so we use &#8216;lang&#8217; as a key in the array to get the value.</p>
<p>For simplicity&#8217;s sake, it is usually better to define all of your XML data as child elements.  This allows you to easily expand data types in the future, and you won&#8217;t have to mix methods for accessing the data.</p>
<p>However, if you&#8217;re working with someone else&#8217;s XML file, you may need to look up attributes.  Now you know how.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Alternate CSS Styles Using PHP</title>
		<link>http://www.earn-web-cash.com/2008/02/13/alternate-css-styles-php/</link>
		<comments>http://www.earn-web-cash.com/2008/02/13/alternate-css-styles-php/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 04:08:08 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/13/how-to-alternate-css-styles-using-php/</guid>
		<description><![CDATA[There are a lot of situations in which you'd like to alternate CSS styles from one element to the next.  Long tables, for example, look a lot nicer when the background color of the rows is alternated (think: Gmail).

How do we do that?  Some pretty simple PHP can help you accomplish this task and spice up your HTML/CSS.]]></description>
			<content:encoded><![CDATA[<p>There are a lot of situations in which you&#8217;d like to alternate CSS styles from one element to the next.  Long tables, for example, look a lot nicer when the background color of the rows is alternated.</p>
<p>How do we do that?  Some pretty simple PHP can help you accomplish this task and spice up your HTML/CSS.<br />
<span id="more-111"></span></p>
<h5>Look At the HTML/CSS</h5>
<p>Before we start making things dynamic, let&#8217;s look at the basic HTML.</p>
<p>Let&#8217;s say we want to make a list of items &#8211; in a table &#8211; maybe a list of emails.  The end result should be a table with alternating background colors for rows &#8211; white and light blue.</p>
<p>If we were to create this table by hand, it might look something 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;">table</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blue&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>E-mail subject<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Sender<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;white&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>E-mail subject<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Sender<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></div></div>

<p>This assumes that we have a CSS class defined as &#8220;blue&#8221; with a blue background and &#8220;white&#8221; with a white background.</p>
<h5>Adding Some PHP to Assign Classes</h5>
<p>We&#8217;re setting the background color by adding a class declaration to the table row.  The first step in making this dynamic is to use PHP to echo the class.  Like this&#8230;</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;">tr</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php echo 'blue'; ?&gt;</span></span>&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span> ... <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span></pre></div></div>

<p>We&#8217;ve taken the same table row element from before and replaced &#8220;blue&#8221; with a php snippet that echos the same phrase, &#8220;blue.&#8221;  The end result is the same HTML that we created in the first example.</p>
<h5>Dynamically Choosing the Class</h5>
<p>The final step in making this dynamic is to add a conditional statement to the PHP block.  For example, let&#8217;s say we&#8217;re executing a loop 5 times.  On the odd iterations (1, 3, 5) we&#8217;ll use &#8220;blue&#8221; as the class.  On the even iterations (2, 4), we&#8217;ll use &#8220;white.&#8221;</p>
<p>To test whether or not a number is odd, you can use the modulus (%) operator.  This returns the remainder of a division &#8211; so <code>2 % 3 = 1</code>.  With a little basic math theory, we know that if a number is divided by 2 and has no remainder it&#8217;s even &#8211; but if there&#8217;s a remainder of 1 it&#8217;s odd.</p>
<p>Here&#8217;s what the loop might look like.</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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
   &lt;tr class=&quot;<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;">$x</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;white&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;&lt;td&gt; ... &lt;/td&gt;&lt;/tr&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h5>That If Statement Is Kind of Long&#8230;</h5>
<p>Well, we can shorten that up.  This is a great place to use the obscure and un-readable ternary operator in PHP.  It&#8217;s a shortcut that allows you to write a simple if-statement in one line.</p>
<p>For example&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span> ? <span style="color: #0000ff;">&quot;Yes&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;No&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The ternary operator takes three parts &#8211; the conditional statement (in this case I just wrote true), a value to return if the statement is true (&#8220;Yes&#8221;), and a value to return if the statement is false (&#8220;No&#8221;).  I echo&#8217;ed the result just so you could see what the statement outputs (in this case, &#8220;Yes&#8221;).</p>
<p>We turn our previous if statement into a ternary operator statement and shorten up that code block quite a bit.  Like so&#8230;</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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
   &lt;tr class=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span> ? <span style="color: #0000ff;">&quot;blue&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;white&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;&lt;td&gt; ... &lt;/td&gt;&lt;/tr&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Creating these table rows is just one example of how you can use PHP to help spice up your HTML.  With this concept, you can dynamically alternate styles based on a bunch of factors.</p>
<p>How else could you use this?</p>
<p>You could alternate the background colors of comments in your blog template.  You could include a bottom-border after every five rows in your table.  You could make a calendar module and display Saturday and Sunday in boldface.</p>
<p>The options are really endless &#8211; so long as you understand the basic principal.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/13/alternate-css-styles-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Use Attribute Selectors to Fine Tune Your CSS</title>
		<link>http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/</link>
		<comments>http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 00:28:01 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/</guid>
		<description><![CDATA[Here's a quick tip for fine tuning your CSS declarations.  

You can use attribute selectors to style an html element that contains an attribute with the value that you declare.  This allows you to do a bunch of cool stuff - like style different "input" types differently, style anchor tags with titles differently, and et]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tip for fine tuning your CSS declarations.  </p>
<p>You can use attribute selectors to style an html element that contains an attribute with the value that you declare.  This allows you to do a bunch of cool stuff &#8211; like style different &#8220;input&#8221; types differently, style anchor tags with titles differently, and etc.<br />
<span id="more-89"></span><br />
How does it work?</p>
<p>An attribute selector follows this basic formula&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">&#91;</span>attribute<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;value&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> style information... <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Like an id or class selector, you can use it alone (as in the example above), or attach it to a specific html element.</p>
<p>How can we use it?  Here are a few examples.</p>
<h5>Style Links with Title Elements Differently</h5>
<p>You could use this to give users a hint that an anchor tag or link has a title element.  This way they know to hover over the link and read the tooltip.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #00AA00;">&#91;</span>title<span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">thin</span> <span style="color: #993333;">dotted</span> <span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>That declaration gives any anchor tag containing a &#8220;title&#8221; attribute a thin dotted blue underlining.  You may have to tweak it a bit to make sure the normal anchor-underlining doesn&#8217;t interfere with it.</p>
<h5>Style Particular Input Types on Forms</h5>
<p>You can use this to style Submit buttons, Text Inputs, etc. &#8211; independent of other input types.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span>  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">blue</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This simple declaration gives all Submit buttons a blue background color.  Fairly silly, but the point is the declaration &#8211; not the actual styling.  You could do something similar with these declarations&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">input <span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* style information */</span> <span style="color: #00AA00;">&#125;</span>
input <span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* style information */</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<h5>Style Internal Links Different from External Links</h5>
<p>This is a really nifty trick&#8230; but unfortunately it requires a bit of CSS 3.  This will probably invalidate your CSS, and your mileage may vary.</p>
<p>It&#8217;s still interesting to try out.</p>
<p>You can use &#8220;^=&#8221; instead of &#8220;=&#8221; to signal that the browser should match any element with the given attribute that contains the quoted string.  It doesn&#8217;t have to be an exact match.  So you can use &#8220;^=&#8217;mydomain.com&#8217;&#8221; to match any links that point to the domain you&#8217;re working on.</p>
<p>Like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #00AA00;">&#91;</span>href<span style="color: #00AA00;">^=</span><span style="color: #ff0000;">&quot;earn-web-cash.com&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* style information */</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>You could use this to given internal links a slightly different color &#8211; some kind of queue to help the user differentiate between internal and external links.</p>
<p>For a longer list of ways to use this neat little trick, check out this post <a href="http://www.search-this.com/2007/11/14/hip-to-be-square/" title="Uses for CSS Attribute Selectors">Hip to Be Square</a>.  There&#8217;s also a list of the different ways to search for values (like ^=) &#8211; which will be valid in CSS 3.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/01/31/css-attribute-selectors/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Create a 3D Button Using CSS and a Gradient Image</title>
		<link>http://www.earn-web-cash.com/2008/01/28/3d-button-gradient/</link>
		<comments>http://www.earn-web-cash.com/2008/01/28/3d-button-gradient/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 00:30:43 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[gradient]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/01/28/3d-button-gradient/</guid>
		<description><![CDATA[While I was browsing through <a href="www.dzone.com" title="Developer's Zone">dzone</a> today, I stumbled on this <a href="http://www.dave-woods.co.uk/?p=158">3d button css tutorial by Dave Woods</a>.

Using some simple css and a little color know-how, he shows you how to turn a square button into a pseudo 3-dimensional button.  I'll take it a step further and add a gradient background image to enhance the lighting effect.  Like this...]]></description>
			<content:encoded><![CDATA[<p>While I was browsing through <a href="http://www.dzone.com" title="Developer's Zone">dzone</a> today, I stumbled on this <a href="http://www.dave-woods.co.uk/?p=158">3d button css tutorial by Dave Woods</a>.</p>
<p>Using some simple css and a little color know-how, he shows you how to turn a square button into a pseudo 3-dimensional button.  I&#8217;ll take it a step further and add a gradient background image to enhance the lighting effect.  Like this&#8230;<br />
<span id="more-79"></span><br />
<a class="button" href="http://www.web-cash-com">Web Cash</a></p>
<p>We can build this button up in a couple of steps.  First, we need to make our anchor tag (link) into a block &#8211; with some padding and a background.</p>
<p><a class='button2' href="http://www.earn-web-cash.com">Web Cash</a></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #6666ff;">.button</span> <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ab7f20</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">.5em</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span></pre></div></div>

<p>That gives us a nice, squarish look&#8230; kind of like a button.  But it definitely looks flat and two dimensional.  The next step we&#8217;re going to take is to add a gradient background.  This will add a nice lighting effect &#8211; giving the button a bit of depth.</p>
<p>To do this, we need a background image.  I made a gradient image in GIMP (read this <a href="http://www.earn-web-cash.com/2008/01/24/how-to-create-a-gradient-background-image-in-gimp/">tutorial to learn how to create a gradient image</a> like this).  Then we need to add the image to the css declaration by changing one line.</p>
<p><a class='button3' href="http://www.earn-web-cash.com">Web Cash</a></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #6666ff;">.button</span> <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ab7f20</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">uploads/2008/01/gradient-button.thumbnail.png</span><span style="color: #00AA00;">&#41;</span>
     <span style="color: #993333;">repeat-x</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">;</span></pre></div></div>

<p>Notice that the color (#ab7f20) is the same as the second color in the gradient image.  This gradient is long enough to color the whole area &#8211; but if our button gets too tall, we want it to blend in nicely.</p>
<p>The last thing we need to do is give it some borders &#8211; light on the top and dark on the bottom.  This will give it the illusion of being hit by the light.  It&#8217;ll also accentuate the effect of the gradient background image.</p>
<p><a class="button" href="http://www.web-cash-com">Web Cash</a></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #6666ff;">.button</span> <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f6daa3</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#724f05</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#724f05</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f6daa3</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.button</span><span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#724f05</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f6daa3</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f6daa3</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#724f05</span> <span style="color: #933;">3px</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Notice that the background colors are flipped when you hover over the button.  This gives the illusion that you are actually &#8220;pressing&#8221; the button &#8211; just by flipping the way the lighting hits.</p>
<p>You could also use the special &#8220;inset&#8221; and &#8220;outset&#8221; border types to cut down on that last css declaration.  You lose a little control over the color and light placement, but it gives a pretty decent effect.  Like this&#8230;</p>
<p><a class="button" style="border: #ab7f20 3px outset;" href="http://www.web-cash-com">Web Cash</a></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #6666ff;">.button</span> <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ab7f20</span> <span style="color: #933;">3px</span> <span style="color: #993333;">outset</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.button</span><span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ab7f20</span> <span style="color: #933;">3px</span> <span style="color: #993333;">inset</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>And that&#8217;s all there is to it.  Change the colors to your liking, add a custom gradient image, and you&#8217;ve got a nifty little 3 dimensional button.</p>
<p>All it takes is some css.  No need to create a bunch of fancy images, and it&#8217;s easily expandable to accommodate the text inside of your button.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/01/28/3d-button-gradient/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
