根据内部文本更改背景颜色


Change Background Color based on innerText?

我对PHP很陌生,我不知道该怎么做。 我有一个超大的CSV文件,它使用fget作为html表加载。 如何浏览特定列并将该列中包含"是"一词的特定单元格的 td 背景颜色更改为绿色?

作为参考,这是我的代码:

<!DOCTYPE html>
<html>
<head>
<link href="stylesheets/styles.css" rel="stylesheet" />
</head>
<body>
<?php
echo "<table>'n'n";
$f = fopen("market_research.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
            if (substr($cell, 0,4) == "http") {
                echo "<td>" ."<a href='" . $cell . "'>Go!</a>";
            } else
                echo "<td>" . htmlspecialchars($cell);
        }
        echo "<tr>'n";
}
fclose($f);
echo "'n</table></body></html>";

?>

这是我目前的 CSS:

tr {background-color: #f7f7f7;}
body {background-color:#8b9dc3;}
td:first-child { 
        text-align: center;
        font-weight: bold;
        padding-left: 5px;
        padding-right: 5px;
}
td:nth-child(odd)
{
background-color: #FFFFFF;
}
tr:first-child {
        background-color: #FFFFFF !important;
        text-align: center;
        font-weight: bold;
        font-size: 110%;
        height: 50px;
}
td {
        padding-left: 5px;
        padding-right: 5px;
}
你可以

这样设置它:

希望这对你有帮助

您需要将data-attr添加到元素<p>,同时使用fget()生成<td>

<p data-val="Yes">Yes</p>

你的CSS是这样的:

p[data-val="Yes"] {background-color:green;}

工作示例

您需要为这些单元格设置一个类。这将标识一个组,然后您可以在样式表或标签中设置这些样式。

添加一个额外的 IF 语句,用于检查条件"$cell包含'是'"

你可以检查这样的字符串中的字符串,这个例子$a是你的变量,你正在检查字符串'mattiscool' - 如果它找到它,那么它将输出'true'

if (strpos($a,'mattiscool') !== false) { 回声"真";}

相反,我们希望输出您的TD单元格以及类标签。因此,在标签中添加部分 if 语句打印 - 就像这样

<td Class="myfirstclass">

然后在样式表中添加此类的样式 - 例如。

myfirstclass{
font-family:Verdana;
font-size:16pt;
}

这是你要找的?