Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

function - Trying to Alternate Row Color in PHP

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...