php - 区分大小写的 CSS 格式

标签 php html css html-table case-sensitive

我正在使用 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/

相关文章:

php - 如何使用 PHP 发送带有大图像的信号推送通知

html - DIV 未垂直覆盖整个内容

html - 向 div 添加额外的填充导致其他 div 向下移动 - CSS?

javascript - 使用数据列表自动下一个焦点到日期选择器

jquery - html5 视频宽度未扩展 100%

html - Css 悬停下拉列表

php - Laravel 性能

javascript - 如何进行实时搜索(包括加载搜索)

php - 如何在 Joomla k2 item.php 中包含新模块

javascript - 如果文件夹名称包含空格,HTA 脚本将不起作用