Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Mark every other row differently

Explorer ,
Aug 17, 2010 Aug 17, 2010

Copy link to clipboard

Copied

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>

TOPICS
Server side applications

Views

928
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 19, 2010 Aug 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_ca
...

Votes

Translate
Community Expert ,
Aug 18, 2010 Aug 18, 2010

Copy link to clipboard

Copied

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


Wappler, the only real Dreamweaver alternative.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 19, 2010 Aug 19, 2010

Copy link to clipboard

Copied

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>

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 19, 2010 Aug 19, 2010

Copy link to clipboard

Copied

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, the only real Dreamweaver alternative.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 19, 2010 Aug 19, 2010

Copy link to clipboard

Copied

When taking your post over, it echoes the following:

Untitled-1.jpg

Should I copy the <td> cells somewhere in between then?

EDIT: changed the bgcolor to class for convenient sake. Don't think this should influence the output. I'm really sorry for the level of noob'ism 🙂

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 19, 2010 Aug 19, 2010

Copy link to clipboard

Copied

It looks like you have forgotten the <?php and ?> tags.

Having said that, you may be better off keeping your original unformatted table and I shall show you how to convert that table in a SpryHTMLDataSet so that you can use the inbuilt Spry features.

Have you got somewhere to upload your scripts so that you can supply an online URL?

Wappler, the only real Dreamweaver alternative.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 19, 2010 Aug 19, 2010

Copy link to clipboard

Copied

LATEST

So I replaced the opening tag of each row and it works fine now. Like this:

<?php
$i=0;
do {
    $i++;
    if($i % 2) { //this means if there is a remainder
        echo "<tr bgcolor=\"yellow\">";
    } else { //if there isn't a remainder we will do the else
        echo "<tr bgcolor=\"blue\">";
    }
?>

For example, you showed &lt; being the same as <.

Many thanks again vw2ureg!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines