Skip to main content
Inspiring
August 18, 2010
Answered

Mark every other row differently

  • August 18, 2010
  • 1 reply
  • 1154 views

I got stuck when wanting to display a soccer calendar on a website. There are 30 games per season and I would like every other row to have a light grey background color. Now my question is, is that possible with PHP? I guess it has got something to do with a counter or something.

Below a quick example of how that table would look like with of course the <tr> element getting a repeat region for as many records as there are in the recordset.

Thanks!

<table>
<tr>
<td>Date</td><td>Team 1 vs. Team 2</td><td>Result</td>
</tr>
</table>

This topic has been closed for replies.
Correct answer BenPleysier

Try

<table cellpadding="1" cellspacing="3" border="1">
<tr>
<td>Match #</td><td>Date</td><td>Game</td>
</tr>
<?php
$i=0;
do {
    $i++;
    if($i % 2) { //this means if there is a remainder
        echo "&lt;TR bgcolor=\"yellow\"&gt;\n";
    } else { //if there isn't a remainder we will do the else
        echo "&lt;TR bgcolor=\"white\"&gt;\n";
    }
?>

<td><?php echo $row_calendar['day']; ?></td>
<td><?php echo $row_calendar['date']; ?></td>
<td><?php echo $row_calendar['hometeamid']; ?> - <?php echo $row_calendar['awayteamid']; ?></td>
</tr>
<?php } while ($row_kalender = mysql_fetch_assoc($calendar)); ?>
</table>

Also not the difference in spelling calendar vs kalender

1 reply

BenPleysier
Community Expert
Community Expert
August 18, 2010

Have a look here http://labs.adobe.com/technologies/spry/samples/data_region/EvenOddRowSample.html


Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
derjaanAuthor
Inspiring
August 19, 2010

So this one you're showing me works with an XML file. Is there a PHP version available as I'm totally not into XML because I don't know anything about that. I use the following code:

<table cellpadding="1" cellspacing="3" border="1">
<tr>
<td>Match #</td><td>Date</td><td>Game</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_calendar['day']; ?></td>
<td><?php echo $row_calendar['date']; ?></td>
<td><?php echo $row_calendar['hometeamid']; ?> - <?php echo $row_calendar['awayteamid']; ?></td>
</tr>
<?php } while ($row_kalender = mysql_fetch_assoc($calendar)); ?>
</table>

Which would output something like the following:

<table cellpadding="1" cellspacing="3" border="1">
<tr>
<td>Match #</td><td>Date</td><td>Game</td>
</tr>
<tr>
<td>1</td>

<td>2010-09-04 11:00:00</td>
<td>5-u12 - 3031-u12</td>
</tr>
<tr>
<td>1</td>
<td>2010-09-04 11:15:00</td>
<td>556-u12 - 79-u12</td>
</tr>
<tr>
<td>1</td>
<td>2010-09-04 12:00:00</td>

<td>1349-u12 - 1434-u12</td>
</tr>
<tr>
<td>1</td>
<td>2010-09-04 13:00:00</td>
<td>5009-u12 - 311-u12</td>
</tr>
<tr>
<td>1</td>
<td>2010-09-04 13:00:00</td>
<td>5661-u12 - 1614-u12</td>

</tr>
</table>

BenPleysier
Community Expert
BenPleysierCommunity ExpertCorrect answer
Community Expert
August 19, 2010

Try

<table cellpadding="1" cellspacing="3" border="1">
<tr>
<td>Match #</td><td>Date</td><td>Game</td>
</tr>
<?php
$i=0;
do {
    $i++;
    if($i % 2) { //this means if there is a remainder
        echo "&lt;TR bgcolor=\"yellow\"&gt;\n";
    } else { //if there isn't a remainder we will do the else
        echo "&lt;TR bgcolor=\"white\"&gt;\n";
    }
?>

<td><?php echo $row_calendar['day']; ?></td>
<td><?php echo $row_calendar['date']; ?></td>
<td><?php echo $row_calendar['hometeamid']; ?> - <?php echo $row_calendar['awayteamid']; ?></td>
</tr>
<?php } while ($row_kalender = mysql_fetch_assoc($calendar)); ?>
</table>

Also not the difference in spelling calendar vs kalender

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!