Does anyone know how I'd go about making the row colors of my HTML table alternate between a red background and a white background?

Heres my code:

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<?php do { ?>
<tr><td><a href="javascript:;" onClick="MM_openBrWindow('details.php?id=<?php echo $row_get_associations_normal['customerid']; ?>','','menubar=yes,scrollbars=yes,resizable=yes,width=300,height=400')"><?php echo $row_get_associations_normal['customername']; ?></a></td></tr>
<?php } while ($row_get_associations_normal = mysql_fetch_assoc($get_associations_normal)); ?>
</table>

My mySQL code above this is:

mysql_select_db($database_cons, $cons);
$query_get_associations_normal = sprintf("SELECT * FROM tblassociations, tblcustomers WHERE customerid = assoccustid AND customercounty = '%s' AND assoccategory = '%s'", $thecounty_get_associations_normal,$thecat_get_associations_normal);
$get_associations_normal = mysql_query($query_get_associations_normal, $cons) or die(mysql_error());
$row_get_associations_normal = mysql_fetch_assoc($get_associations_normal);
$totalRows_get_associations_normal = mysql_num_rows($get_associations_normal);

    Give this a whirl. I didn't test it, because I didn't want to create a test environment database, but it works with a straight array and for loop, so it should work for you here too.

    <table width="100%" border="0" cellpadding="0" cellspacing="0"> 
    <?php
    $bgcolor = "FF0000";
    do { 
    if($bgcolor == "#FF0000"){$bgcolor = "#FFFFFF";} else {$bgcolor = "#FF0000";}
    ?> 
    <tr><td style="background-color: <?php echo $bgcolor; ?>;"><a href="java script:;" onClick="MM_openBrWindow('details.php?id=<?php echo $row_get_associations_normal['customerid']; ?> ','','menubar=yes,scrollbars=yes,resizable=yes,wid
    th=300,height=400')"><?php echo $row_get_associations_normal['customername']; ?></a></td></tr> 
    <?php } while ($row_get_associations_normal = mysql_fetch_assoc($get_associations_normal)); ?> 
    </table> 
    

      :bemused: Oops... Sorry LordShryku, I am still getting used to the whole "Feed a man a fish, he'll eat for a day, teach him how to fish, and he'll eat for a lifetime." thing.

      I will assume the position for the board beating now... :queasy:

        Nah, not in the mood right now. Plus, I don't see many of the alternating row questions anymore, and just happened to know that was one that was bookmarked.

          Fair Enough... Thank you for your mercy tonight!

            Try this:

            <?php

            // For alternate row colour
            $row_colour="#FFFFCC";
            $count_rows = 0;

            //loop and display the limited records being browsed
            while ($arr = mysql_fetch_array($rs))
            {
            $count_rows++;
            if ($count_rows%2==0)
            { $row_colour="#FFFFCC"; } // Even-numbered rows
            else
            { $row_colour="#CCCC99"; } // Odd-numbered rows
            ?>

            <tr bgcolor="<?php echo $row_colour; ?>"> <!-- Alternate Row Colours -->
            <td></td></tr>
            <?php }; ?>
            </table>

            Tell me if it doesnt work!

              Nah Weed... I was calibrated a couple of nights ago, and am turning it around... Just gotta give me time. 🙂

                Write a Reply...