<?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>The Journal of Tim Roberts &#187; User Interface</title>
	<atom:link href="http://www.wiseguysonly.com/category/web-development/user-interface/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wiseguysonly.com</link>
	<description>PHP,MySQL and jQuery Developer</description>
	<lastBuildDate>Sun, 20 Jun 2010 17:12:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Select and Unselect All Checkboxes with jQuery</title>
		<link>http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/</link>
		<comments>http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 19:01:06 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.wiseguysonly.com/?p=308</guid>
		<description><![CDATA[I&#8217;ve been working with jQuery to spice up my interfaces for over 12 months now and figured it&#8217;s time to share some of the little techniques I&#8217;ve developed on the way.
The first is something I use quite alot when I am giving users the ability to administer lists of items in bulk. That including a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with jQuery to spice up my interfaces for over 12 months now and figured it&#8217;s time to share some of the little techniques I&#8217;ve developed on the way.</p>
<p>The first is something I use quite alot when I am giving users the ability to administer lists of items in bulk. That including a checkbox that when checked sets the state of all the checkboxes beside a list of items to &#8220;checked&#8221;. Naturally unchecking it will uncheck all checkboxes. I&#8217;m sure you get the picture here&#8217;s the code based on two scenarios depending on your preference. But firstly you need to include the jQuery library in the head of your document (I grab mine straight from Google&#8217;s repository to save my bandwidth):</p>
<p><span id="more-308"></span></p>
<p><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;</code></p>
<p>and then somewhere in your document a controller checkbox that toggles all the others (note the call to toggleChecked when the state of the checkbox changes):</p>
<p><code>&lt;input type="checkbox" onclick="toggleChecked(this.checked)"&gt; Select / Deselect All</code></p>
<p>Now here are  two versions of the toggleChecked function dependent on the semantics of your document. The only real difference is the jQuery selector for your list checkboxes:</p>
<p><strong>1: All checkboxes have a class of  &#8220;checkbox&#8221; (&lt;input type=&#8221;checkbox&#8221; class=&#8221;checkbox&#8221; /&gt;)</strong><br />
<code>function toggleChecked(status) {<br />
$(".checkbox").each( function() {<br />
$(this).attr("checked",status);<br />
})<br />
}</code></p>
<p><strong>2: All the checkboxes are contained within a div with an arbitary id:</strong></p>
<p><code>&lt;div id="checkboxes"&gt;<br />
&lt;input type="checkbox" /&gt;<br />
&lt;input type="checkbox" /&gt;<br />
&lt;input type="checkbox" /&gt;<br />
&lt;/div&gt;</code></p>
<p>In this case the function would look like this:</p>
<p><code>function toggleChecked(status) {<br />
$("#checkboxes input").each( function() {<br />
$(this).attr("checked",status);<br />
})<br />
</code></p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Drag and drop reordering of database fields (sortables) with jQuery</title>
		<link>http://www.wiseguysonly.com/2008/12/07/drag-and-drop-reordering-of-database-fields-sortables-with-jquery/</link>
		<comments>http://www.wiseguysonly.com/2008/12/07/drag-and-drop-reordering-of-database-fields-sortables-with-jquery/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 18:21:17 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.wiseguysonly.com/?p=143</guid>
		<description><![CDATA[This tutorial explains how to display a list of items from a database that can be reordered in real time by dragging and dropping, using the jQuery library. Moreover, no page reload is required on every reorder. If you want a bigger introduction to why you may want to do this I suggest reading my [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial explains how to display a list of items from a database that can be reordered in real time by dragging and dropping, using the jQuery library. Moreover, no page reload is required on every reorder. If you want a bigger introduction to why you may want to do this I suggest reading my tutorial on <a href="/2006/08/11/drag-and-drop-reordering-of-database-fields-with-ajax/">how to achieve the same functionality using scriptaculous</a>.</p>
<p>Previously I have explained how to achieve this in <a href="/2006/08/11/drag-and-drop-reordering-of-database-fields-with-ajax/">Scriptaculous</a> and <a href="/2007/10/10/drag-and-drop-reordering-of-database-fields-sortables-with-mootools/">Mootools</a>.</p>
<p><span id="more-143"></span></p>
<p><strong>Step one: Include the jQuery libraries.</strong></p>
<p>Grab <a href="http://docs.jquery.com/Downloading_jQuery">jQuery</a> and <a href="http://ui.jquery.com/download_builder/">jQuery UI</a> libraries.</p>
<p>Include them in the head of your document:<br />
<code>&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">"text/javascript" </span><span class="attribute-name">src</span>=<span class="attribute-value">"javascripts/jquery-1.2.6.min.js"</span>&gt;&lt;/<span class="end-tag">script</span>&gt;<br />
&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">"text/javascript" </span><span class="attribute-name">src</span>=<span class="attribute-value">"javascripts/jquery-ui-personalized-1.5.2.min.js"</span>&gt;&lt;/<span class="end-tag">script</span>&gt;</code></p>
<p><strong>Step two: Build a list of items from the database.</strong></p>
<p>Give your list and id &#8211; I am using item_list for this tutorial &#8211; and then make sure each list item has an id of &#8220;item_PRIMARYKEY&#8221; where PRIMARYKEY is the key of the item from the database. Here is something similar to the list you should have:</p>
<p><code><br />
&lt;ul id="item_list" &gt;<br />
&lt;li id="item_1"&gt;Item One&lt;/li&gt;<br />
&lt;li id="item_2"&gt;Item Two&lt;/li&gt;<br />
&lt;li id="item_3"&gt;Item Three&lt;/li&gt;<br />
&lt;li id="item_4"&gt;Item Four&lt;/li&gt;<br />
&lt;/ul&gt;</code></p>
<p><strong>Step 3: Make the list sortable and add a callback to our database script.</strong></p>
<p>Using jQuery&#8217;s onload function or by adding the following script below our list,  inject some javascript into our list to make the list items sortable</p>
<p><code> $("#item_list").sortable({stop:function(i) {<br />
$.ajax({<br />
type: "GET",<br />
url: "server_items_reorder.php",<br />
data: $("#item_list").sortable("serialize")<br />
});</code></p>
<p>Let&#8217;s look at this line by line so you can understand what is happening.</p>
<p>Line one uses the jQuery selector to target the element with an id of item_list (our unordered list in this case). We then apply the sortable plugin from jQuery UI.</p>
<p><a href="http://docs.jquery.com/UI/Sortable/sortable#options">Sortable has a number of options available</a>, but in this case we are going to use the stop option (when sorting has finished) to make an ajax call. The important options in our ajax call are as follows:</p>
<ul>
<li>type: the type of call (in this case GET);</li>
<li>url: (where the ajax post should be made) in this case &#8220;server_items_reorder.php&#8221;;</li>
<li>data : the string the makes up our GET call;</li>
</ul>
<p>The data can be easily built up by using more of the inbuilt functionality of jQuery&#8217;s sortable:</p>
<p><code>$("#item_list").sortable("serialize")</code></p>
<p>This takes the current order of our list and builds it into an array that can be passed on like a GET string:</p>
<p>item[]=1&amp;item[]=2&amp;item[]=3&amp;item[]=4</p>
<p>jQuery automatically serializes the list item id&#8217;s  into this array so you can see why I chose to give them the id item_PRIMARYKEY for the ease offered by convention.</p>
<p>So now we are making a ajax call and padding on the new list order we can write a script to update the database.</p>
<p><strong>Step four: The database script (updating the position).</strong></p>
<p>I am assuming your database table has an extra field called &#8220;position&#8221; that you can use to hold an integer of each items position as relating to its peers. What we need to do now is create our server_items_reorder.php script to collect the array of items and update the database. Here is that script:</p>
<p><code>// YOUR DB CONNECTION HERE<br />
foreach($_GET['item'] as $key=&gt;$value) {<br />
mysql_query("UPDATE my_items" SET position = '" . $key . "' WHERE id ='" . $value . "'");<br />
}</code></p>
<p>This script is simple enough to be written in the language of your choice. It collects the array of items from the GET variable and for each one updates the position based on the id of the item.</p>
<p>That&#8217;s all there is to it.</p>
<p>This tutorial is pretty low level as I have written most of these concepts including troubleshooting in my  <a href="/2006/08/11/drag-and-drop-reordering-of-database-fields-with-ajax/">Scriptaculous</a> and <a href="/2007/10/10/drag-and-drop-reordering-of-database-fields-sortables-with-mootools/">Mootools</a> versions of these, so if you experience any difficulties look at those tutorials first, read the user comments and/or post any questions here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2008/12/07/drag-and-drop-reordering-of-database-fields-sortables-with-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jQuery table effects plugin &#8211; v1.0</title>
		<link>http://www.wiseguysonly.com/2008/10/12/jquery-table-effects-plugin-v10/</link>
		<comments>http://www.wiseguysonly.com/2008/10/12/jquery-table-effects-plugin-v10/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 12:58:38 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://wiseguysonly.com/?p=78</guid>
		<description><![CDATA[See the demo here.
My first outing into producing a jQuery plugin is the limited, but useful “jQuery table effects”.
The current version defines the following features:

Alternating row colours for readability (striping).
onmouseover and onmouseout events for manipulating the background colour of the row.


Requirements:

jQuery 1.25+
jtable-effects: minified (1kb) &#124; source(1kb)

Optional complete package of demo files (19kb).

Implementation:
Include the jQuery library [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wiseguysonly.com/demos/jquery/table-effects-jquery.html"><strong>See the demo here</strong></a>.</p>
<p>My first outing into producing a jQuery plugin is the limited, but useful “jQuery table effects”.</p>
<p>The current version defines the following features:</p>
<ol>
<li>Alternating row colours for readability (striping).</li>
<li>onmouseover and onmouseout events for manipulating the background colour of the row.</li>
</ol>
<p><span id="more-78"></span></p>
<p><strong>Requirements:</strong></p>
<ul>
<li><a href="http://docs.jquery.com/Downloading_jQuery#Download_jQuery">jQuery 1.25+</a></li>
<li>jtable-effects: <a href="../demos/jquery/javascripts/jtable-1.0.min.js">minified (1kb)</a> | <a href="../demos/jquery/javascripts/jtable-1.0.js">source(1kb)<br />
</a></li>
<li><a href="../demos/jquery/table-effects-jquery.zip">Optional complete package of demo files (19kb)</a>.</li>
</ul>
<p><strong>Implementation</strong>:</p>
<p>Include the jQuery library and the jtable-effects plugin in the head of your document:</p>
<p><code>&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">“text/javascript” </span><span class="attribute-name">src</span>=<span class="attribute-value">“javascripts/jquery-1.2.6.min.js”</span>&gt;&lt;/<span class="end-tag">script</span>&gt;<br />
&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">“text/javascript” </span><span class="attribute-name">src</span>=<span class="attribute-value">“javascripts/jtable-1.0.min.js”</span>&gt;&lt;/<span class="end-tag">script</span>&gt;</code></p>
<p>Then call the jTable function for each instance of a table you wish to maniuplate &#8211; do this using the onload functionality of jQuery:</p>
<p><code>&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">“text/javascript”</span>&gt;<br />
$(document).ready(function(){<br />
jTable(”demo-table”,{stripe:’#EED’,over:’#FC0′,out:’#FFF’});<br />
});<br />
&lt;/<span class="end-tag">script</span>&gt;</code></p>
<p id="line1">The first argument is the id of the table body (tbody) to apply the effects to and the second is an array of options available:</p>
<ul>
<li><em>stripe: </em>defines the html colour reference for alternating rows. (hex or rgb).</li>
<li><em>over:</em> defines the html colour reference for mouseover. (hex or rgb).</li>
<li><em>out:</em> defines the html colour reference for mouseout &#8211; this has no effect on the striped rows which return to the value defined by <em>stripe</em>. (hex or rgb).</li>
</ul>
<p>Finally, <a href="http://wiseguysonly.com/demos/jquery/table-effects-jquery.html">a demonstration of jtable effects 1.0</a> can be seen here and<a href="../demos/jquery/table-effects-jquery.zip"> a set of demo files can be downloaded here (19kb)</a>.</p>
<p>Please post comments, support requests, bug reports and feature requests for v1.1 in the comments here</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2008/10/12/jquery-table-effects-plugin-v10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autocompletion with Scriptaculous and Ajax.</title>
		<link>http://www.wiseguysonly.com/2008/03/09/autocompletion-with-scriptaculous-and-ajax/</link>
		<comments>http://www.wiseguysonly.com/2008/03/09/autocompletion-with-scriptaculous-and-ajax/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 19:40:32 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scriptaculous]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://wiseguysonly.com/?p=45</guid>
		<description><![CDATA[Demo here &#124; Download sample files
This tutorial supercedes my previous turtorial on autocompletion by adding further tips and tricks to make your autocompletion a much better experience for your site users The tutorial is structured in the following way:

Introduction: What is autocompletion and why use it?
How it works.
Getting scriptaculous and building the form.
Building the autocomplete [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://wiseguysonly.com/demos/scriptaculous/ajax-autocompletion/autocomplete.php">Demo here</a> | <a href="http://wiseguysonly.com/downloads/autocompletion.zip">Download sample files</a></strong></p>
<p>This tutorial supercedes my previous turtorial on autocompletion by adding further tips and tricks to make your autocompletion a much better experience for your site users The tutorial is structured in the following way:</p>
<ol>
<li>Introduction: What is autocompletion and why use it?</li>
<li>How it works.</li>
<li>Getting scriptaculous and building the form.</li>
<li>Building the autocomplete server.</li>
<li>Passing back an id to our form.</li>
</ol>
<p><span id="more-45"></span></p>
<h4>1. What is autocompletion and why use it?</h4>
<p>I predict that most people will want to skip this section and get straight to the action, so I will keep it brief. Autocompletion is a means of helping your users when filling in forms. The most common application of autocomplete is found in your web browser’s address bar; as you type the bar makes suggestions of URLs based on previous pages visited. In our example we will query a database of musical artists and give our users suggestions as they type in an artist name field.</p>
<h4>2. How it works.</h4>
<p>As a user types in a specific input field on a form, on each key up event, we use JavaScript to call a script in the background using Ajax (XMLHttpRequest) features built into the Scriptaculous library. Our form will pass the currently typed text to this script and the script will return a list of suggestions which will be dynamically placed in our page. The important thing is that our user can select one of these items by mouse-click or key-stroke to populate the form field instantly.</p>
<h4>3. Getting scriptaculous and building the form.</h4>
<p>This implementation of autocomplete requires 3 components:</p>
<ol>
<li>The Scriptculous libraries;</li>
<li>An HTML form with a single field;</li>
<li>An autocomplete server script</li>
</ol>
<p>I use PHP as my language of choice for the autocomplete server script, but I have tried to keep this tutorial abstract enough to allow you to use your language of choice. The server script is the only thing that changes for different server-side scripting languages. Download the latest version of the <a href="http://script.aculo.us/downloads">Scriptaculous JavaScript Libraries</a> and include them in the head of a new HTML document as below. Don’t forget to change the paths to match your directory structure:</p>
<p><code>&lt;script type="text/javascript" src="lib/prototype.js"&gt;&lt;/script&gt;<br />
&lt;script type="text/javascript" src="src/scriptaculous.js"&gt;&lt;/script&gt;</code></p>
<p>Now add a form to your HTML page’s body. In this form we are going to add a field that will take our search term (with both id and name attributes as “searchterm”) and an empty div with an id of “hint”. This empty div acts as a holder for our hints when they are generated. You should have some HTML looking like this:</p>
<p><code>&lt;input type="text" name="searchterm" id="searchterm" /&gt;<br />
&lt;div id="hint"&gt;&lt;/div&gt;</code></p>
<p>All we need now is to initiate a new call to Scriptaculous’ built in Ajax.Autocomplete method below our HTML code for the autocompleter &#8211; like this:</p>
<p><code>&lt;script type="text/javascript"&gt;<br />
new Ajax.Autocompleter(”searchterm”,”hint”,”server.php”);<br />
&lt;/script&gt;</code></p>
<p>The Ajax.Autocompleter takes 3 arguments: The element supplying the search string, the element to show suggestions in and the server script that will generate our suggestions. With that in place we can write the server script.</p>
<h4>4. Building the autocomplete server.</h4>
<p>The following server is written in PHP connecting to a MySQL database but you can your language of choice and even a flat text file or XML file for the suggestion database. My database table, “Artists”, is simple &#8211; it comprises a unique id field and an artist “name”.</p>
<p>As the Autocompleter sends the value of a form field as a POST variable, we can use this in our server script’s query againstthe database. All we need to do is output an unordered list for the autocompleter. Scriptaculous does the leg work of inserting the latest list into our hint box everytime the user makes a keystroke. Here is my server code in PHP</p>
<p><code>&lt;?<br />
// don't forget your database connection here.<br />
echo "&lt;ul&gt;";<br />
$sql = "SELECT * FROM artists WHERE name LIKE '%" . $_POST['searchterm'] . "%'";<br />
$rs = mysql_query($sql);<br />
while($data = mysql_fetch_assoc($rs)) {<br />
echo "&lt;li id=\"" . $data['id'] . "\"&gt;" . $data['name'] . "&lt;/li&gt;";<br />
}<br />
echo "&lt;/ul&gt;";<br />
?&gt;</code></p>
<p>Note that I have given each list item an id that is the same as that of the record. We will use this shortly for a more advanced technique. But for now we have all we need for a basic autocompleter.</p>
<h4>5. Passing back an id to our form</h4>
<p>Once a user has selected an item from the autocompleter, the string is entered into the textfield. Often, for the sake of efficiency and accuracy it is better to deal with the id of the item than the string name when it is passed on to the next page. To do this we can use some extra functionality of Ajax.Autocompleter along with some JavaScript that will pass the id to a hidden field in our form.</p>
<p>The first thing we need to add to our form is a hidden field with an id that we can reference:</p>
<p><code>&lt;input type="hidden" id="artist_id" name="artist_id" /&gt;</code></p>
<p>Now we can use an extra piece of functionality built into Ajax.Autocompleter that allows us to pick up the id that we included in our list item from the server script. This is an extra option on our autocompleter call:</p>
<p><code>new Ajax.Autocompleter(”searchterm”,”hint”,”server.php”, {afterUpdateElement : getSelectedId});</code></p>
<p>We are telling our autocompleter to run a function called getSelectedId when an item is selected. All we need to do now is write this function to get the id and inject it into our hidden field. Insert the function below the</p>
<p><code>function getSelectedId(text, li) {<br />
$('searchterm').value=li.id;<br />
}</code></p>
<p>And that’s all there is to it. Of course you can be as clever as you want with your autocomplete server script. Try using the LIMIT clause in your SQL to make it run faster. Make your queries more advanced to bring back more specific results. And don’t forget the fun you can have with CSS on your hint div and the list of suggestions that it displays. If you have any implementations of autocompletion using this tutorial, please post them in the comments here.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p><a href="http://www.ipsukltd.co.uk/green-roofs.php">Single ply membrane</a> Products</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2008/03/09/autocompletion-with-scriptaculous-and-ajax/feed/</wfw:commentRss>
		<slash:comments>71</slash:comments>
		</item>
		<item>
		<title>Alternating table row colours in PHP.</title>
		<link>http://www.wiseguysonly.com/2006/03/09/alternating-table-row-colours-in-php/</link>
		<comments>http://www.wiseguysonly.com/2006/03/09/alternating-table-row-colours-in-php/#comments</comments>
		<pubDate>Thu, 09 Mar 2006 06:20:21 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.wiseguysonly.com/?p=111</guid>
		<description><![CDATA[Recently I have been tinkering with Ruby on Rails and I came across a rather nice method of alternating row colours using only two lines of code and a couple of CSS rules. I have been using this method in my PHP ever since and never looked back. This is a neat trick to alternate [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been tinkering with <a href="http://www.rubyonrails.com">Ruby on Rails</a> and I came across a rather nice method of alternating row colours using only two lines of code and a couple of CSS rules. I have been using this method in my PHP ever since and never looked back. This is a neat trick to alternate row colors on a table using PHP.</p>
<p><span id="more-111"></span></p>
<h3>Why would you want to do this?</h3>
<p>Alternating the row colors (sensibly) on tables makes them easier to read. Hard coding styles is easy enough, but when you are bringing rows back dynamically, from a database for example, you can never be sure which row will be where.</p>
<p>Lets say we have an array from the database called $data. Before we iterate over this information, we need to set a variable outside of our loop</p>
<p><code>$current_row = 0;</code></p>
<p>Now inside our loop, we need just one line that will alternate our <code>$current_row</code> count between 1 and 0</p>
<p><code>while( $data = mysql_fetch_array($recordset)) {<br />
$current_row = 1 - $current_row;<br />
... (rest of code goes here)<br />
}</code></p>
<p>What this does is sets the <code>$current_row</code> variable to 1 or 0 on each pass. The maths is that 1 &#8211; 0 = 1 and on the next pass 1 &#8211; 1 = 0 ¦ This logic can continue infinitely.</p>
<p>All we need to do now are two things:</p>
<ol>
<li>Set a dynamic class in the opening table row in our loop:</li>
<li>Create a class for our alternate row(s) in our CSS:<code>.row1 { background: #eed; }<br />
.row0 { background: #fff; }</code></li>
</ol>
<p>Two lines of extra php code, one extra class in your CSS, and you&#8217;re done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2006/03/09/alternating-table-row-colours-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
