2016-10-21 119 views
1

我有以下问题: 我想用PHP打印一个<td>,它有一个类“myclass”,它给出<td>的蓝色背景,它支持小于5的变量,包含大于5的变量的<td>的橙色背景。 问题是,当我尝试这样做时,<td>背景保持默认。 任何人都知道为什么?PHP print-TD class is not working

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


} 

} 

?> 

回答

2

因为你关闭你的<table>标签早:

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

这应该是这种格式:

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

实例与您的代码:

<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> 
+0

你是对的!感谢它解决了这个问题:) –

+1

@DimitrisHD:很高兴帮助你的朋友,如果它的工作不忘记接受这将有助于他人。 – devpro

+1

是的,我会的。它还需要5分钟!再次感谢 –