Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm looking to alternate row colors using PHP function. Here's what I have (although it does not work):

function row($year) {
    if($year%2) 
        $color == "#FFF";
    else
        $color == "#000";
}

for ($year=2013; $year<=2023; $year++) 
    {
    row($year);
    echo "<tr bgcolor='$color'><td>$year</td><td>$tdate</td></tr>";
    }

Basically, if a year is odd I would like the color of the row to be white. If even, black.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
265 views
Welcome To Ask or Share your Answers For Others

1 Answer

Why don't you just use CSS with the nth-child selector?

tr:nth-child( 2n ) {
  background-color: #000;
}
tr:nth-child( 2n + 1 ) {
  background-color: #FFF;
}

Then no further attributes are needed on the <tr> element.

Besides the IE, most browsers support this. See the Browser compatibility of MDN.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...