我想创建一个日期过滤页面,在其中可以根据日期范围过滤存储在数据库中的数据。
我有一列“ DIFF”,其中存储了Intime和Outtime的时差。 我想根据存储在 $ diff 中的值更改单元格的背景颜色。如果小于8,则红色为绿色。
我无法执行此操作。 请帮助
$result = mysqli_query($connect, $query);
$cnt=1;
$output .= '
<table class="table table-bordered">
<tr>
<th width="8%">Sr No.</th>
<th width="18%">EMPLOYEE ID</th>
<th width="30%">EMPLOYEE NAME</th>
<th width="15%">IN_TIME</th>
<th width="15%">OUT_TIME</th>
<th width="20%">DATE</th>
<th width="20%">WORKING HOURS</th>
</tr>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$outtime = $row["outtime"];
$intime = $row["intime"];
$array1 = explode(':', $intime);
$array2 = explode(':', $outtime);
$minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
$minutes2 = ($array2[0] * 60.0 + $array2[1])/60;
$diff = round(($minutes2 - $minutes1),1).' hrs';
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " .
$row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td>
<td align="center" > '.$diff.' </td>
</tr>
'; $cnt++;
}
}
else
{
$output .= '
<tr>
<td colspan="5">No Record Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
}
答案 0 :(得分:7)
您可以通过使用php来做同样的事情:-
<td align="center"<?php if($diff<8){?> style="background-color: red; " <?php }else{?>style="background-color: green;"<?php } ?> > '.$diff.' </td>
或也像这样使用js:-
<td align="center" class="check" > '.$diff.' </td>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//alert($('.check').text());
$(" tr > td.check").each(function(){
if($(this).text()<8){
$( this ).css( "background-color", "red" );
}else{
$( this ).css( "background-color", "green" );
}
});
});
</script>
答案 1 :(得分:1)
尝试为您要填充颜色的td添加background-color属性。
代码应类似于:
$output .= '<tr>
<td align="center">' . $cnt . '</td>
<td align="center">' . $row["EmpId"] . '</td>
<td align="center">' . $row["FirstName"] . " " . $row["LastName"] . '</td>
<td align="center">' . $row["intime"] . '</td>
<td align="center"> ' . $row["outtime"] . '</td>
<td align="center">' . $row["date"] . '</td>';
if ($diff < 8) {
$output .= '<td align="center" style="background-color: #ff0000;"> ' . $diff . ' </td>';
} else {
$output .= '<td align="center" style="background-color: #00ff00;"> ' . $diff . ' </td>';
}
$output .= '</tr>';
答案 2 :(得分:0)
您是否尝试过对$ diff变量执行小于操作?
您可以尝试一下,它根据$ diff值设置td的背景颜色。
更改此部分:
<td align="center" > '.$diff.' </td>
对此
<td align="center" style="background-color: ($diff < 8) ? 'red' : 'green'" > '.$diff.' </td>
答案 3 :(得分:0)
检查$ diff是否小于8
使用以下代码
<?php
$result = mysqli_query($connect, $query);
$cnt=1;
$output .= '
<table class="table table-bordered">
<tr>
<th width="8%">Sr No.</th>
<th width="18%">EMPLOYEE ID</th>
<th width="30%">EMPLOYEE NAME</th>
<th width="15%">IN_TIME</th>
<th width="15%">OUT_TIME</th>
<th width="20%">DATE</th>
<th width="20%">WORKING HOURS</th>
</tr>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$outtime = $row["outtime"];
$intime = $row["intime"];
$array1 = explode(':', $intime);
$array2 = explode(':', $outtime);
$minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
$minutes2 = ($array2[0] * 60.0 + $array2[1])/60;
$diff = round(($minutes2 - $minutes1),1).' hrs';
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " .
$row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td>
<td align="center" ';
if($diff < 8)
{
$output .= 'bgcolor="red"';
}
else
{
$output .= 'bgcolor="green"';
}
$output .= ' > '.$diff.' </td>
</tr>
'; $cnt++;
}
}
else
{
$output .= '
<tr>
<td colspan="5">No Record Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
?>
答案 4 :(得分:0)
您可以将三元运算符用作
$diff = round(($minutes2 - $minutes1),1).' hrs';
$colorCode = ($diff < 8)?"#ff0000":"#00ff00";
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " .
$row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td>
<td align="center" style="background-color:$colorCode"> '.$diff.' </td>
</tr>
';