drcj64241 2015-11-30 14:53
浏览 40

从数据库更改表格单元格颜色

Here's what I was doing (as part of a PHP echo):

<td bgcolor={$row['AwayB']}><font color={$row['AwayF']}>{$row['AwayTeam']}</font></td>

Then I found some CSS to alternate table row background colours:

table.sortable tbody tr:nth-child(2n) td {
  background: #666;
}
table.sortable tbody tr:nth-child(2n+1) td {
  background:#333;
}

But then the background colour was the same the whole way across the row, instead of changing for the two table cells it should change for (the second is almost identical to the first, just with 'home' instead of 'away'). And apparently I should be using CSS instead of the font tag anyway.

So I knocked together this in the stylesheet to test:

table.sortable tbody tr td.awayTeam {
    background-color:#900;
}

And removed the bgcolor code from the td tag and replaced it with class='awayTeam'. And it worked. And I found something on CSS-Tricks about using variables with CSS, and tried that, with this at the top of the CSS:

<?php
    header("Content-type: text/css; charset: UTF-8");
    $bgcolor = "#900";
?>

And then this as the style:

table.sortable tbody tr td.awayTeam {
    background-color:<?php echo $bgcolor; ?>;
}

Still worked. So I replaced $bgcolor with $row['AwayB'], and "{$row['AwayB']}", and ($row['AwayB'])...and all three just returned

table.sortable tbody tr td.awayTeam {
    background-color:;
}

Is there a way I can make this work?

EDITED TO ADD: Moving the CSS code into the while loop used to generate the table overrides the zebra striping and uses the team colour of the last team in the table for all teams. Overriding the zebra striping isn't a huge deal--the team colours provide enough differentiation--but obviously the colours need to work.

I need to keep the zebra striping in the table class, because I need that class for sorting and there are other, non-sports tables that need the background differentiation.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度