根据条件改变颜色

时间:2015-02-19 10:53:03

标签: php html css

嗨我有这个PHP,它将sql查询分成两列并打印表。现在我需要改变TD的颜色,其中值>我按班级添加了颜色变化。它工作但不正确。它改变整个字符串的颜色但不改变TD细胞的颜色。

$stmt=ociparse($olink, $sql);
if (!$stmt) {
    $e = oci_error($olink); 
    trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$r =ociexecute($stmt,OCI_NO_AUTO_COMMIT);
if (!$r) {
    $e = oci_error($stmt);  // For oci_execute errors pass the statement handle
    print htmlentities($e['message']);
    print "\n<pre>\n";
    print htmlentities($e['sqltext']);
    printf("\n%".($e['offset']+1)."s", "^");
    print  "\n</pre>\n";
}
$ncols = oci_num_fields($stmt);
$cur_dt = 1;
echo "<TABLE border=\"1\" width=\"100%\" align='center' cellspacing='0' cellpadding='0'  class=\"sortable\">";
/* echo "\n<tr>"; */
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
$str=1;
while (oci_fetch($stmt)) {
if ($str % 2 == 1)
{
    echo "\n";
echo "<tr";
$hrr="";
echo ">";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt)  {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
echo "</TABLE>";
oci_close($olink);
?>
</div>
</body>
</html>

0 in second column should be blue, but it ged "hour" class and change color

4 个答案:

答案 0 :(得分:0)

使用css设置单个单元格style="background-color: #2B75C1;"

的样式

如果要更改字符串style="color: #2B75C1;"

的颜色
echo '<th style="background-color: #2B75C1;" class=\"header\">'.oci_field_name($stmt, $i).'</th>';

答案 1 :(得分:0)

替换

echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt)  {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";

有关

echo '<td' . (oci_result($stmt, 2) >= $cur_dt ? ' class="hour"' : '') . '>';
//                              ^ shouldn't be there '$i'?
echo oci_result($stmt, $i);
echo '</td>'; // closing tag for <td> in optional, you can remove that to make your HTML more readable.

// remove $hrr variable, you don't need it.

答案 2 :(得分:0)

我想,你必须用$ i替换“2”。替换以下代码

 if (oci_result($stmt, 2) >= $cur_dt) 

有了解决方案。

解决方案1:

if (oci_result($stmt, $i ) >= $cur_dt) 

解决方案2:

  if ( (oci_result($stmt, $i ) >= $cur_dt) || oci_result($stmt, $i ) > 0) 

答案 3 :(得分:0)

在您的class文件中创建一个css,例如.change{background-color:green} <td>

for ($i = 1; $i <= $ncols; $i++) {
$hrr ="";
if (oci_result($stmt, 2) >= $cur_dt)  {$hrr = 'class="change"';}
echo "<td ".$hrr.">";
 echo oci_result($stmt, $i);
}
 echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
 $str++;
}