Change table cell background color

Asked

Viewed 1,460 times

1

I’m trying to change the background color of cells whose values are equal to the day the user accesses the site. The variable $dataa keeps the dates of each name...

Table formatting (inside a php loop):

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

Attempted staining:

<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>
  • I think you’re doing it the hard way. I could do it in PHP when you generate HTML, or in Javascript. So it’s a middle ground that gets confused. Can you add more PHP code? However there is an error in JS: obj[$k] = getElementById(\"linha$k\"); missing "Document" must be obj[$k] = document.getElementById(\"linha$k\");

  • Did it work? I can put answer, it may be that others have the same problem...

  • 1

    yes. It was the lack of Document.

  • If you think it’s worth it, put.

1 answer

2


There is an error in your Javascript, missing document in the code.

Test like this:

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

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

But correcting that fault should work.

If you do this in PHP you could give these a special class 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 the CSS:

.diaCerto{ background-color: #FF0000';}
  • The reason for this is that the real goal is not this. I tried to make a simple javascript code to see if I was doing it right. Now I’ve adapted to my use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.