Change background color table cell

1

I'm trying to change the background color of cells whose values are the same as the day the user visits the site. The variable $dataa saves the dates of each name ...

Formatting table (is within a php loop):

echo "
<tr>
  <td id=\"linha".$l."\">".$nomes[$l]."</td>
  <td class=\"dataa\">".$dataa[$l]."</td>
</tr>
";

Attempt to color:

<SCRIPT>

<?php

echo "var obj = new Array();";

for ($k=0; $k<16; $k++){

 if ( $data_pro_php[$k] == '23' ) {

    echo " 
    obj[$k] = getElementById(\"linha$k\");
    obj[$k].style.backgroundColor='#FF0000'; 
    ";

 }

}

?>

</SCRIPT>
    
asked by anonymous 23.11.2014 / 23:47

1 answer

2

You have an error in your JavaScript, is missing document in code.

Test like this:

echo " 
    obj[$k] = document.getElementById(\"linha$k\");
    obj[$k].style.backgroundColor='#FF0000'; 
";

I could however do this in PHP when it generates HTML, or pass an array to Javascript and iterate there.

But fixing that fault should work.

If you do this in PHP you could give a special class to those td . Something like:

$classe = $dataa[$l] == 23 ? 'dataa diaCerto' : 'dataa';
echo "
    <tr>
      <td id=\"linha".$l."\">".$nomes[$l]."</td>
      <td class=\"".$classe."\">".$dataa[$l]."</td>
    </tr>
";

and in CSS:

.diaCerto{ background-color: #FF0000';}
    
23.11.2014 / 23:55