View Full Version : PHP CSS Alternating Row Colors

06-28-2007, 03:33 PM
I have a query and a do while loop. I am trying to alternate row colors and think I can do it if I can pull in a number with each result returned, but am not sure what direction to head with it. Any assistance would be greatly appreciated.

Here is my query:

$documents = mysql_query("SELECT docid, docname, docdescription, docbegindate, docenddate, docstatus, doctypeicon, doctypename, docfile, emp_name, emp_email, docstatusname, docorder FROM documentsmain WHERE (docstatus = '2' or docstatus = '1') AND docfolder = '$dcparent'") or die(mysql_error());
$row_documents = mysql_fetch_array($documents);
$row_documents_total = mysql_num_rows($documents);

Here is my loop:

<?php do {?><tr>
<td width="5" class="list_item_boxleft"><table width="10" border="0" cellspacing="0" cellpadding="0">
<td class="icon_box"><a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank"><img src="images/file_icons/<?php echo $row_documents['doctypeicon']; ?>" alt="<?php echo $row_documents['docdescription']; ?>" width="16" height="16" border="0"></a></td>
</table> <a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank"></a><a href="uploads/<?php echo $row_documents['docfile']; ?>"></a></td>
<td class="list_item_box"><a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank" title="<?php echo $row_documents['docdescription']; ?>"><?php echo $row_documents['docname']; ?></a></td>
<td class="list_item_box"><?php echo $row_documents['docbegindate']; ?></td>
<td class="list_item_box"><?php echo $row_documents['docenddate']; ?></td>
<td class="list_item_box"><?php echo $row_documents['emp_name']; ?></td>
<td class="list_item_box"><?php echo $row_documents['docstatusname']; ?></td>
<td width="50" height="3" nowrap class="list_item_box"><form name="form3" method="post" action="">

<input name="docorder" type="text" id="docorder" value="<?php echo $row_documents['docorder']; ?>" size="3" maxlength="3">
<input name="submit" type="image" src="/images/refresh.png" alt="Approve Time Off Request" align="middle" width="16" height="16">

</form> </td>
<td class="list_item_boxright"><div align="center">&nbsp;</div></td>
</tr><?php } while ($row_documents = mysql_fetch_array($documents)); ?>

06-28-2007, 03:42 PM
You could set a counter $counter and increment it for every iteration. Test if $counter%2 == 0 or 1 then call in a style for odd and even.


$counter = 0
do {
if ($counter%2==0){
echo '<tr class="even">';
} else {
echo '<tr class="odd">';


tr.odd td {
background-color: #c00;
tr.even td {
background-color: #efefef;

Or whatever colours you needed.
You could of course use tertiary statements but I find this is simpler to understand.
Hope this helps.