<?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>KirkHings.com &#187; row</title>
	<atom:link href="http://kirkhings.com/tag/row/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirkhings.com</link>
	<description>The '-berger' part trips up so many folks that I sometimes simplify my name for you. Yes, just for you.</description>
	<lastBuildDate>Sat, 13 Jun 2009 18:35:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to change alternating table row colors</title>
		<link>http://kirkhings.com/code/php/how-to-change-alternating-table-row-colors/44/</link>
		<comments>http://kirkhings.com/code/php/how-to-change-alternating-table-row-colors/44/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 06:21:48 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[connection object]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[row]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=44</guid>
		<description><![CDATA[ photo credit: Lee Carson
Everyone loves the alternating table row colors on web pages. The same idea is applied to alternating blog comment styles. You can do it quite easily, and here are the three simple steps to do it.
This technique involves PHP, CSS, and a touch of SQL. However, the logic and therefore the [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/59271446@N00/3101281900/" title="Chocolate Santa Clones" target="_blank"><img src="http://farm4.static.flickr.com/3059/3101281900_a8dc7144c0_m.jpg" alt="Chocolate Santa Clones" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/59271446@N00/3101281900/" title="Lee Carson" target="_blank">Lee Carson</a></small></div>
<p>Everyone loves the alternating table row colors on web pages. The same idea is applied to alternating blog comment styles. You can do it quite easily, and here are the three simple steps to do it.</p>
<p>This technique involves PHP, CSS, and a touch of SQL. However, the logic and therefore the heavy lifting is found in the PHP. You can use this method with table rows (as is commonly used for WordPress comments), or for stacked div elements. I will present a table row version first, then a div version.</p>
<p><strong>Step one</strong> is to style your two table rows so they appear distinct from each other. This of course goes in your stylesheet:</p>
<pre class="brush: css;">
tr.rowA {
    background-color: #666;
    text-align: left;
}
tr.rowB {
    background-color: #333;
    text-align: right;
}
</pre>
<p><strong>Step two</strong> is to write the function to determine if the row is an even or odd row (and therefore rowA or rowB). This is accomplished using the modulus operator (% in PHP). This operation gives you the remainder of a division. No matter what number we feed it, even numbers divided by 2 result in 0 remainder (therefore boolean &#8216;false&#8217;). Odd numbers divided by 2 result in .5 remainder (therefore boolean &#8216;true&#8217;).</p>
<pre class="brush: php;">
function alternatingRows(&amp;$counter) {
	if ($counter % 2) {
		$row = 'rowA';
	}
	else {
		$row = 'rowB';
	}
	$counter++; // increment counter for next iteration
	return $row; // return rowB or rowA
}
</pre>
<p>Note that this function can be placed on the page where it is used, but a better place would a globals file that you include. Code reuse is next to godliness. </p>
<p><strong>Step three</strong> is to write the code to select your data, and present it in a loop. This is assuming you know how to create your connection object and have done so already. </p>
<pre class="brush: php;">
$myQuery= &quot;SELECT * FROM table&quot;;
$myResult = @$connectionObject-&gt;query($myQuery);
</pre>
<p>Note that if this is in a table you will need to code the table start and end tags outside the loop (or you will get multiple tables). </p>
<pre class="brush: php;">
// echo alternating table rows
echo &quot;&lt;table&gt;&quot;;
$ndx = 0;
while ($row = $myResult-&gt;fetch_assoc()) {
    $rowClass = alternatingRows($ndx);
    echo &quot;&lt;tr class=\&quot;$rowClass\&quot;&gt;&quot;;
    echo &quot;&lt;td&gt;$row[data1]&lt;/td&gt;&quot;;
    echo &quot;&lt;td&gt;$row[data2]&lt;/td&gt;&quot;;
    echo &quot;&lt;/tr&gt;&quot;;
}
echo &quot;&lt;/table&gt;&quot;;
</pre>
<p>The function logic is the simplest part. The echoing out your data is the trickiest part, but start small until it works and then you can expand the complexity. The styling is the funnest part, limited only by your imagination and CSS abilities. </p>
<p>Here is another similar example using div elements instead of table rows:</p>
<p>Stylesheet:</p>
<pre class="brush: css;">
.divA {
    float: left;
    background-color: #666;
    text-align: left;
}
.divB {
    float: right;
    background-color: #333;
    text-align: right;
}
</pre>
<p>Function:</p>
<pre class="brush: php;">
function alternatingRows(&amp;$counter) {
	if ($counter % 2) {
		$div = 'divA';
	}
	else {
		$div = 'divB';
	}
	$counter++; // increment counter for next iteration
	return $div; // return divA or divB
}
</pre>
<p>Display:</p>
<pre class="brush: php;">
// echo alternating divs
$ndx = 0;
while ($each = $myResult-&gt;fetch_assoc()) {
    $divClass = alternatingRows($ndx);
    echo &quot;&lt;div class=\&quot;$divClass\&quot;&gt;
    echo &quot;&lt;h1&gt;$each[data1]&lt;/h1&gt;&quot;;
    echo &quot;&lt;p&gt;$each[data2]&lt;/p&gt;&quot;;
    echo &quot;&lt;/div&gt;&quot;;
}
</pre>
<p>Questions or comments?</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/php/how-to-change-alternating-table-row-colors/44/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
