<?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>Functino &#187; PHP</title>
	<atom:link href="http://functino.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://functino.com</link>
	<description>My daily struggle with webdevelopment</description>
	<lastBuildDate>Sun, 07 Mar 2010 19:38:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>cakePHP cycle-helper: alternating row colors with cake</title>
		<link>http://functino.com/2009/07/cakephp-cycle-helper-alternating-row-colors-with-cake/</link>
		<comments>http://functino.com/2009/07/cakephp-cycle-helper-alternating-row-colors-with-cake/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 15:09:15 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://functino.com/?p=365</guid>
		<description><![CDATA[It&#8217;s been a long time since my last posting. Today I&#8217;m going to show you my cycle-helper for cakePHP. If you haven&#8217;t read my other posts on alternating row colors, do so now: Alternate Table Rows in CakePHP (and ZF, Rails, Smarty) 
The cycle-helper is easy to use and install and if your familiar with [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long time since my last posting. Today I&#8217;m going to show you my cycle-helper for cakePHP. If you haven&#8217;t read my other posts on alternating row colors, do so now: <a href="http://functino.com/2009/04/alternate-table-rows-in-cakephp-and-zf-rails-smarty/">Alternate Table Rows in CakePHP (and ZF, Rails, Smarty)</a> </p>
<p>The cycle-helper is easy to use and install and if your familiar with the rails cycle-helper you already know how it works&#8230;. So: how does it look when you use my helper for alternating row colors:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;table&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;tr<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cycle</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' class=&quot;altrow&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&gt;
		&lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'rowdata'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
	&lt;/tr&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p>I think it&#8217;s pretty nice and a more beautiful approach then what cake bake produces. </p>
<p>Of course you can also use it with css-classes &#8220;even&#8221; and &#8220;odd&#8221;:<br />
<span id="more-365"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;table&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</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: #000088;">$cycle</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'odd'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'even'</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;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'rowdata'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
	&lt;/tr&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p>If you have nested loops you have the possibility to give your cycle a name via the &#8220;name&#8221;-option:</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: #000088;">$animals</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dogs&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Santa's Little Helper&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Beethoven&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lassie&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Pluto&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Brian&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				 <span style="color: #0000ff;">&quot;Cats&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Snowball&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Snowball II&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Snowball III&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Garfield&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				 <span style="color: #0000ff;">&quot;Monkeys&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mojo&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Lazlo&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;King Kong&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				 <span style="color: #0000ff;">&quot;Frogs&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Kermit&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Ed Bighead&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
				 <span style="color: #0000ff;">&quot;Kangaroos&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Skippy&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Jack&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span> (<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cycle</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>)&lt;br /&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cycle</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cycle</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;br /&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;hr /&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The helper code contains comments that should explain how it all works. The code for the helper itself looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #666666; font-style: italic;">/*
 * Cycle Helper for cakePHP
 * Copyright (c) 2009 Andreas
 * http://functino.com
 *
 * @author      andreas
 * @version     1.0
 * @license     MIT
 *
 */</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
 * The CycleHelper is used to cycle through the provided strings. 
 * You can use it for example to set alternate classes for table rows.
 * Usage is simple: 
 * &lt;code&gt;
 *   &lt;table&gt; 
 *   &lt;?php foreach($items as $item):?&gt;
 *     &lt;tr class=&quot;&lt;?php echo $cycle-&gt;cycle(&quot;odd&quot;, &quot;even&quot;);?&gt;&quot;&quot;&gt;
 *       &lt;td&gt;&lt;?php echo $item;?&gt;&lt;/td&gt;
 *     &lt;/tr&gt;
 *   &lt;?php endforeach;?&gt;  
 * &lt;/code&gt; 
 * 
 * This will create rows where the class alternates between &quot;odd&quot; and &quot;even&quot;.
 * 
 * If you need more than one cycle you can use named cycles:
 * &lt;code&gt;
 *   &lt;?php $items = array(&quot;Car&quot; =&gt; array(&quot;VW&quot;, &quot;Fiat&quot;, &quot;Ford&quot;), &quot;Food&quot; =&gt; array(&quot;Pizza&quot;, &quot;Hotdog&quot;), &quot;...&quot; =&gt; array(&quot;...&quot;));?&gt; 
 *   &lt;table&gt; 
 *   &lt;?php foreach($items as $item =&gt; $aliases):?&gt;
 *     &lt;tr class=&quot;&lt;?php echo $cycle-&gt;cycle(&quot;odd&quot;, &quot;even&quot;);?&gt;&quot;&quot;&gt;
 *       &lt;td&gt;
 *         &lt;?php echo $item;?&gt;: 
 * 	       &lt;?php foreach($aliases as $alias):?&gt;
 *           &lt;div class=&quot;&lt;?php echo $cycle-&gt;cycle(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, array(&quot;name&quot; =&gt; &quot;second_cycle&quot;));?&gt;&quot;&gt;
 * 				&lt;?php echo $alias;?&gt;
 *           &lt;/div&gt;
 *         &lt;?php endforeach?&gt;  
 *      &lt;/td&gt;
 *     &lt;/tr&gt;
 *   &lt;?php endforeach;?&gt;  
 *   &lt;/table&gt;
 * &lt;/code&gt;
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> CycleHelper <span style="color: #000000; font-weight: bold;">extends</span> AppHelper <span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/**
  * @var    array
  * @access private
  */</span>	
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$cycles</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>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Creates a Cycle object. Cycles through elements of an array everytime it is called. This is useful for example if you want to
 * use alternate classes for table rows etc. 
 * This method takes an arbitrary number of strings to cycle through. The last parameter can be an array with options. 
 * If you pass in an options array with the key &quot;name&quot; you create a named cycle. 
 * If you don't pass in this name the name &quot;default&quot; is used. You can use named cycles to use more than one cycle in a loop.
 * @param string $name aribtrary number of strings to cycle through
 * @param array $options Options is an array. Currently only array(&quot;name&quot; =&gt; &quot;xyz&quot;) is implemented
 * @return string $options Link attributes e.g. array('id'=&gt;'selected')
 * @access public 
 */</span>    
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#93;</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: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;default&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$cycle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCycle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cycle</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$cycle</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCycle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Resets a cycle. If you reset a cycle it starts form the first element again. 
 * If you don't pass in a name the default cycle is reset. To reset a named cycle pass in it's name. 
 * @param  string $name Name of the cycle to reset
 * @return Cycle
 * @access public 
 */</span>	
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;default&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycles</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Returns the current cycle string. Normally you simply call the cycle() method to get the current cycle string. This current()
 * method is useful if you need to get the current cycle string more than one times. 
 * Pass in a name to get the value of a named cycle.  
 * @param  string $name Name of the cycle
 * @return string The current cycle string
 * @access public 
 */</span>	
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;default&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycles</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Creates a ViewCycle and stores it in the cycle array by it's name 
 * @param  string $name Name of the cycle
 * @param  array $params array of cycle strings 
 * @return ViewCycle
 * @access public
 */</span> 	
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCycle<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycles</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ViewCycle<span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycles</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Returns a cycle by it's name 
 * @param  string $name Name of the cycle
 * @return mixed ViewCycle or false
 * @access private
 */</span> 	
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getCycle<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycles</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>	
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycles</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> 
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * ViewCycle is used by the Cycle helper to cycle through an array of elements (in most cases: strings)
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> ViewCycle
<span style="color: #009900;">&#123;</span>
<span style="color: #009933; font-style: italic;">/**
 * Holds the name of this Cycle
 * @var    string
 * @access private
 */</span>		
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Holds an array of strings
 * @var    array
 * @access private
 */</span>	
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$cycle</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * number of cycle strings
 * @var    integer
 * @access private
 */</span>		
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * iteration counter
 * @var    string
 * @access private
 */</span>		
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Takes an array of strings to cycle through 
 * @param  array $cycle
 * @return void
 * @access public
 */</span> 		
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cycle</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cycle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Returns the current cycle string and then sets the next cycle string
 * @return string current cycle string
 * @access public
 */</span> 		
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycle</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">i</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">i</span><span style="color: #339933;">++;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Returns the current cycle string 
 * @return string
 * @access public
 */</span> 		
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Resets this cycle
 * @return void
 * @access public
 */</span> 		
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Set an array of strings to cycle through 
 * @param  array $cycle array of strings
 * @return void
 * @access public
 */</span> 		
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> assign<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cycle</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycle</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cycle</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cycle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you want to use this helper just copy the code to a file &#8220;cycle.php&#8221; in app/view/helpers and add it in your app_controller.php to the $helpers array:</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: #000000; font-weight: bold;">class</span> AppController <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$helpers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Form'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Cycle'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s it!</p>
 <img src="http://functino.com/wp-content/plugins/feed-statistics.php?view=1&post_id=365" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://functino.com/2009/07/cakephp-cycle-helper-alternating-row-colors-with-cake/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zebra Tables: How to alternate table row colors</title>
		<link>http://functino.com/2009/04/zebra-tables-how-to-alternate-table-row-colors/</link>
		<comments>http://functino.com/2009/04/zebra-tables-how-to-alternate-table-row-colors/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 14:17:23 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[alternating rows]]></category>
		<category><![CDATA[modulo]]></category>
		<category><![CDATA[zebra table]]></category>

		<guid isPermaLink="false">http://functino.com/?p=103</guid>
		<description><![CDATA[What is a &#8220;Zebra Table&#8221;? It&#8217;s a striped table with alternating row colors like this:
You can achieve this with very different approaches:
CSS only
With CSS 3 you can achieve such a table with the pseudo class nth-child. The rules are extremly simple and no additonal markup is required. Just add these lines to your CSS:

tr:nth-child&#40;even&#41; &#123;background-color: [...]]]></description>
			<content:encoded><![CDATA[<p>What is a &#8220;Zebra Table&#8221;? It&#8217;s a striped table with alternating row colors like this:</p>
<div id="attachment_104" class="wp-caption alignright" style="width: 310px"><a href="http://functino.com/wp-content/uploads/striped_table.png"><img class="size-medium wp-image-104" title="striped_table" src="http://functino.com/wp-content/uploads/striped_table-300x88.png" alt="A simple Zebra Table" width="300" height="88" /></a>
<ul style="display:none">
<li></li>
</ul>
<p> <p class="wp-caption-text">A simple Zebra Table</p></div>
<p>You can achieve this with very different approaches:</p>
<h2>CSS only</h2>
<p>With CSS 3 you can achieve such a table with the pseudo class nth-child. The rules are extremly simple and no additonal markup is required. Just add these lines to your CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>even<span style="color: #00AA00;">&#41;</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: #cc00cc;">#FFF</span><span style="color: #00AA00;">&#125;</span>
tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>odd<span style="color: #00AA00;">&#41;</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: #cc00cc;">#FFCC00</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>But since not all browsers understand this CSS 3 selector this is perhaps not the best solution.<br />
Instead of relying on Browsers CSS 3 support you can also use JavaScript to achieve the same result.<br />
<span id="more-103"></span>There is a great article on the jQuery-Blog with examples for all major JS-Framework. Take a look: <a href="http://blog.jquery.com/2006/10/18/zebra-table-showdown/"> http://blog.jquery.com/2006/10/18/zebra-table-showdown/</a></p>
<h2>Using PHP to add classes</h2>
<p>If you don&#8217;t want to use the CSS-only or the JavaScript solution you can use one of the following solutions. The basic idea is to add classes to your rows that indicate if it is an even or an odd row.  You can do this by adding an &#8220;odd&#8221; class to odd rows and an &#8220;even&#8221; class to even rows.  Or if you want a more minimalistic approach: Just add an &#8220;altrow&#8221; class to odd rows. It could look like this:</p>
<p>Or:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;table&gt;
  &lt;tr&gt;
    &lt;!-- your tds go here //--&gt;
  &lt;/tr&gt;
  &lt;tr class=&quot;altrow&quot;&gt;
    &lt;!-- your tds go here //--&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;!-- your tds go here //--&gt;
  &lt;/tr&gt;
  &lt;tr class=&quot;altrow&quot;&gt;
    &lt;!-- your tds go here //--&gt;
  &lt;/tr&gt;
&lt;/table&gt;</pre></div></div>

<p>And style it with CSS like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.even</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: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.odd</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: #cc00cc;">#FFCC00</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">tr <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
tr<span style="color: #6666ff;">.altrow</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: #cc00cc;">#FFCC00</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>So how do we add those class names with PHP.  I assume we have the following array with animals and want to print them out to a table:</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: #000088;">$animals</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dogs&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Cats&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Monkeys&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Frogs&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Kangaroos&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You can use the modulo operator % to determine wether a row is even or odd:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;table&gt;
<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;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> 
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;even&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: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;odd&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</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: #000088;">$class</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
    &lt;td&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
  &lt;/tr&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p>Or if we use the handy ternary operator:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;table&gt;
  <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;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</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;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">&quot;even&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;odd&quot;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
      &lt;td&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;/table&gt;
Or if you use the &quot;altrow&quot; instead of &quot;even&quot; and &quot;odd&quot;:
&lt;pre lang=&quot;php&quot;&gt;&lt;table&gt;
  <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;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;tr <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;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' class=&quot;altrow&quot;'</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&gt;
      &lt;td&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p>If you use classnames like &#8220;row_0&#8243; and &#8220;row_1&#8243; instead of even and odd it comes down to this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;table&gt;
  <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;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;tr class=&quot;row_<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;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
      &lt;td&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p>Instead of the modulo operator you can also use a simple bit operation like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;table&gt;
  <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;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</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;">$i</span> <span style="color: #339933;">&amp;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'odd'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'even'</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
      &lt;td&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;
&nbsp;
If you don't need the row number ($i) you can also do something like this:
&lt;pre lang=&quot;php&quot;&gt;
&lt;table&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$even</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #000088;">$even</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$even</span><span style="color: #339933;">;</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: #000088;">$even</span> ? <span style="color: #0000ff;">'even'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'odd'</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
      &lt;td&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$animal</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p>There is another solution with PHPs next() and reset() functions:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;table&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$classes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;even&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;odd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$classes</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$classes</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #000088;">$class</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
      &lt;td&gt; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$animal</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endfor</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/table&gt;</pre></div></div>

<p>With this solution you can also use more than two different styles like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000088;">$colors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;#f00&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;#0f0&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;#00f&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>loopingThroughWhatever<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$colors</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$colors</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//use it somewhere</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$color</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Or another simple example:</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: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;odd&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>loopingThroughWhatever<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'even'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'odd'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'even'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>However I find it kind of annoying when I have to initalize $class evrytime. Well, if you don&#8217;t need both classes (even and odd) you can simply do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>loopingThroughWhatever<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'odd'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//use it somewhere</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$class</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>With this last approach you can simplify things a lot. Take a look at this code generated by CakePHP&#8217;s bake generator:</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: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">' class=&quot;altrow&quot;'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;tr<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$class</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&gt;</pre></div></div>

<p>This would become something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$animals</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;tr <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">' class=&quot;altrow&quot;'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&gt;</pre></div></div>

<p>(Note that there is a little difference in the behaviour of this two: The latter one adds the altrow class to the first, third, fifth, &#8230; row while the first one adds the altrow class to the second, fourth, &#8230;)</p>
<p>I know there are other solutions out there and you can easily combine different approaches (for example use if(){} instead of the ternary operator ?:, or create a function, use a switch, etc&#8230; )). So what do you use?</p>
<p>In my next posting I&#8217;ll take a look at how this is handled in Smarty, Zend Framework, Rails and in CakePHP &#8211; and I&#8217;ll come up with my own CakePHP-Helper to achieve Zebra Tables.</p>
 <img src="http://functino.com/wp-content/plugins/feed-statistics.php?view=1&post_id=103" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://functino.com/2009/04/zebra-tables-how-to-alternate-table-row-colors/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
