Search code examples
phphtmlcsshtml-tablecase-sensitive

Case sensitive CSS formatting


I am generating an html table using a PHP code to grab the content from a .csv table.

Now I would like the format of every specific cell to be depending on it's content.

My (probably terrible) attempt in pseudocode:

if (cell content == "green")
    use #green-css-style
if else (cell content == "blue")
    use #blue-css-style

and so on.

I only want it to "listen" for a limited amount of different contents (approx. 5).

This is my PHP table-generator:

 <?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;">'; 

    ?>

Any help on this is very welcome, additional details available on request! :)


Solution

  • Just add a class to the td element in your php code. You cant access the content of an element in 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";
    

    You can then for example use the following css code:

    td.green { color: green; }
    td.blue { color: blue; }