我尝试了几种不同的选择,但是我无法理解这一点。 我希望表格单元格中的字体根据其字符串值进行更改: 例如,如果值为“ES”,则将字体变为橙色,将红色变为“X”,将灰色变为“不适用” 代码完美无缺,只想添加颜色
(基本上,查询检查员工的工作标签,看他是否需要证书,如果不是,则返回“不适用”,如果有,则检查获得证书的日期(有效期为1年)并查看天气已过期,即将过期或是否有效)
以下是我的PHP脚本片段:
<?php
$query = "SELECT`employee`.`empl_idno`, IF( `jobtitle`.`jt_medical`='n/a',`jobtitle`.`jt_medical`, IF( `certs`.`cert_medical` = 0, 'O',IF( (DATEDIFF((DATE_ADD(`certs`.`cert_medical`, INTERVAL 365 DAY)),CURDATE())) < 1, 'X', IF( (DATEDIFF((DATE_ADD(`certs`.`cert_medical`, INTERVAL 365 DAY)),CURDATE())) < 30, 'ES', 'ok')))) AS medical,IF( `jobtitle`.`jt_hse_ind`='n/a', `jobtitle`.`jt_hse_ind`, IF( `certs`.`cert_hse_ind` = 0, 'O IF( (DATEDIFF((DATE_ADD(`certs`.`cert_hse_ind`, INTERVAL 365 DAY)),CURDATE())) < 1, 'X', IF( (DATEDIFF((DATE_ADD(`certs`.`cert_hse_ind`, INTERVAL 365 DAY)),CURDATE())) < 30, 'ES', 'ok')))) AS hse_ind,IF( `jobtitle`.`jt_tf_ind`='n/a', `jobtitle`.`jt_tf_ind`, IF( `certs`.`cert_tf_ind` = 0, 'O',IF((DATEDIFF((DATE_ADD(`certs`.`cert_tf_ind`, INTERVAL 365 DAY)),CURDATE())) < 1, 'X', IF( (DATEDIFF((DATE_ADD(`certs`.`cert_tf_ind`, INTERVAL 365 DAY)),CURDATE())) < 30, 'ES', 'ok')))) AS tf_ind FROM `employee` JOIN `jobtitle` ON `employee`.`jobtitle_id` = `jobtitle`.`jobtitle_id` JOIN `certs` ON `certs`.`empl_idno` = `employee`.`empl_idno` WHERE `employee`.`empl_jc_code` = '$empl_jc_code'AND `employee`.`jobtitle_id` = '$jobtitle_id' ";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");
echo "<table><br>
<thead><tr>
<th>ID No</th>
<th class='vertical'>Medical Surveillance</th>
<th class='vertical'>HSE Induction</th>
<th class='vertical'>Topfix Induction</th>
</tr></thead>";
while($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr>\n
<td>$empl_idno</td>\n
<td>$medical </td>\n
<td>$hse_ind</td>\n
<td>$tf_ind</td>\n
</tr>\n";
}
echo "</table><br>";
?>
答案 0 :(得分:0)
更改
while($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr>\n
到
$colors = ['ES' => 'orange', 'X' => 'red', 'n/a' => 'gray'];
while($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr".(isset($colors[$tf_ind]) ? " style=\"color:".$colors[$tf_ind]."\"" : "") .">\n