Pergunta

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! :)

Foi útil?

Solução

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; }

Outras dicas

You can use switch. Try like this:

switch(cell_content){
    case "green":
         // something
         break;
    case "blue":
         // something
         break;
    case "red":
         //something
         break;
    default:
         //something
         break;
}

can you provide some real code?

you normaly need to define different css-classes and add them to the appropiate tag

use this code instead:

$hasTitle = true; 

$result_code = '<table id="dataTable" class="table">';


$handle = fopen("data.csv", "r"); 
$start = 0; 
;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)  
{ 
   $result_code .= '<tr>' . "\n"; 
   $css_class_name = '';
  for ( $x = 0; $x < count($data); $x++) 
    { 
    if ($start == 0 && $hasTitle == true)

        switch(strtolower($data[$x])) {
            case 'green': 
                $css_class_name = 'class-green';
                break;
           case 'red': 
                $css_class_name = 'class-red';
                break;
         default: 
               $css_class_name = 'DEFAULT VALUE';
               break;
       }

       $result_code .= '<th title="ignore_case" class="' . $css_class_name . '">'.$data[$x] . '</th>' . "\n";

    else 

        $result_code .= '<td>'.$data[$x].'</td>' . "\n";

    } 

    $start++; 

   $result_code .= '</tr>' . "\n"; 

} 
echo $result_code;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top