我正在使用 PHP 代码生成一个 html 表,以从 .csv 表中获取内容。
现在我希望每个特定单元格的格式取决于它的内容。
我(可能很糟糕)的伪代码尝试:
if (cell content == "green")
use #green-css-style
if else (cell content == "blue")
use #blue-css-style
等等。
我只希望它“收听”有限数量的不同内容(大约 5 个)。
这是我的 PHP 表格生成器:
<?php
$hasTitle = true;
echo '<table id="dataTable" class="table">';
$handle = fopen("data.csv", "r");
$start = 0;
;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++)
{
if ($start == 0 && $hasTitle == true)
echo '<th title="ignore_case">'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
;
echo '</table>' . '<br style="clear: both;">';
?>
非常欢迎对此提供任何帮助,可根据要求提供更多详细信息! :)
最佳答案
只需在您的 php 代码中向 td 元素添加一个类。您无法访问 css 中元素的内容。
switch($data[x]) {
case 'green': $class = 'green'; break;
case 'blue': $class = 'blue'; break;
//...
default: $class = ''; break;
}
echo '<td class="'.$class.'">'.$data[$x].'</td>' . "\n";
然后您可以使用以下 css 代码:
td.green { color: green; }
td.blue { color: blue; }
关于php - 区分大小写的 CSS 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19705246/