How to give alternating table rows different background colors using PHP
<p>I have a table of data that is generated dynamically based on the contents stored in a mysql database.</p>

<p>This is how my code looks:</p>

<pre><code>&lt;table border="1"&gt;
    &lt;tr&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Description&lt;/th&gt;
        &lt;th&gt;URL&lt;/th&gt;
    &lt;/tr&gt;
    &lt;?php
        $query = mysql_query("SELECT * FROM categories");
        while ($row = mysql_fetch_assoc($query)) {
            $catName = $row['name'];
            $catDes = $row['description'];
            $catUrl = $row['url'];

            echo "&lt;tr class=''&gt;";
            echo "&lt;td&gt;$catName&lt;/td&gt;";
            echo "&lt;td&gt;$catDes&lt;/td&gt;";
            echo "&lt;td&gt;$catUrl&lt;/td&gt;";
            echo "&lt;/tr&gt;";
        }
    ?&gt;
&lt;/table&gt;
</code></pre>

<p>Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:</p>

<pre><code>.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
</code></pre>

<p>and that would be the end of that. However since the table rows are dynamically generated, how can I achieve this?</p>
