dongzhuner6981 2011-11-03 07:31
浏览 31
已采纳

交替的行颜色

I am looking for a solution to use the alternating colors row design in my table which is from a csv.

my code:

<?php
echo "<table style='text-align: left; width: 99%;' border='1'>


<thead>
<tr>
<th>data</th>

</tr>
</thead>";
$f = fopen("local/datafiles.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr class='odds'>";
        foreach ($line as $cell) {
                      echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>
";
}
fclose($f);
echo "
</table>";
?>

so how can i have the tr class to alternate from odds and even? thanks

  • 写回答

4条回答 默认 最新

  • duanlie3187 2011-11-03 08:23
    关注
    <?php
    echo "<table style='text-align: left; width: 99%;' border='1'>
    
    
    <thead>
    <tr>
    <th>data</th>
    
    </tr>
    </thead>";
    $f = fopen("local/datafiles.csv", "r");
    $i = 0;
    while (($line = fgetcsv($f)) !== false) {
        echo "<tr style='background-color: ".(($i%2)?'green':'blue').";'>";
        foreach ($line as $cell) {
            echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>
    ";
        $i++;
    }
    fclose($f);
    echo "
    </table>";
    ?>
    

    Or you could assign a class just the same way and style it using css. This is much better practice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证