Dimitris HD Dimitris HD - 1 month ago 5
CSS Question

PHP print- TD class is not working

I have the following problem:
I want to print a

<td>
in PHP, which will have the class "myclass" which gives blue background to the
<td>
that cointains variables less than 5 ,and an orange background to the
<td>
that contains variables greater than 5 .
Problem is that when ever i try to do that, the
<td>
background stays default.
Anyone knows why?

<body>

<style type="text/css">
.myclass{

background-color:orange;

}
</style>
<style type="text/css">
.myclass2{
background-color:red;
}

</style>

<table id="myTable"></table>
<?php
for($x=0;$x<10;$x++){
$myVar=rand(0,10);
if($myVar<5){
print "<tr>";
print "<td class='myclass'>$myVar</td>";
print "<br>";
print "</tr>";
}
else{
print "<tr>";
print "<td class='myclass2'>$myVar</td>";
print "<br>";
print "</tr>";


}

}

?>



Answer

Because you closed your <table> tag to early:

<table id="myTable"></table>

This should be in this format:

<table>
   <tr>
      <td>
      </td>
   </tr>
</table>

Example with your code:

<table id="myTable">
<?php
for($x=0;$x<10;$x++){
$myVar=rand(0,10);
  if($myVar<5){
    print "<tr>";
    print "<td class='myclass'>$myVar</td>";
    print "<br>";
    print "</tr>";
  }
   else{
    print "<tr>";
    print "<td class='myclass2'>$myVar</td>";
    print "<br>";
    print "</tr>";
}
}
?>
</table>