gpt4 book ai didi

PHP 条件格式化与 MySQL 阈值表

转载 作者:行者123 更新时间:2023-11-29 08:23:11 26 4
gpt4 key购买 nike

我希望文本颜色根据目标而改变。基本上,0 到 100 之间的任何内容都是绿色,100 到 125 之间的任何内容都是黄色,超过 125 的任何内容都是红色。但这仅适用于 1 个特定部门。假设我在另一个部门有一个人没有相同的阈值,那么低于 150 的任何内容都是绿色的,等等...

这是我正在使用的代码。

while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row['Department']."</td>";
if (($row['Hold'] >= 0) && ($row['Handle_Time'] <= 100)) {
echo "<td style=\"color:#005e20; font-weight: bold;\">".$row['Handle_Time']."</td>"; //green
} elseif (($row['Handle_Time'] >= 100.01) && ($row['Handle_Time'] <= 124.99)) {
echo "<td style=\"color:#e77904; font-weight: bold;\">".$row['Handle_Time']."</td>"; // yellow
} elseif ($row['Handle_Time'] >= 125) {
echo "<td style=\"color:#FF0000; font-weight: bold;\">".$row['Handle_Time']."</td>"; // red
} else {
echo "<td>".$row['Hold']."</td>"; // no color
}
echo "</tr>";
}

我想到的是一个MySQL表,其中有每个部门的阈值。每个人都有一个部门代码,我希望部门能够匹配目标并相应地改变颜色。

这是名为 stats_threshold 的目标表

 Department    Metric       Target  Yellow  Red
------------------------------------------------------
WR Handle_Time 100.00 124.99 125.00
CA Handle_Time 100.00 124.99 125.00
RET Handle_Time 120.00 169.99 170.00
CET Handle_Time 200.00 249.99 250.00

如何查询部门代码以匹配表中的部门,然后回显目标和阈值。预期结果(颜色列不存在,它只是显示文本颜色应该是什么)

 Department   Handle_Time   Colors
------------------------------------------------------
CA 66 GREEN
CA 118 YELLOW
CA 137 RED
WR 96 GREEN
WR 102 YELLOW
WR 143 RED
RET 119 GREEN
RET 163 YELLOW
RET 240 RED
CET 199 GREEN
CET 201 YELLOW
CET 251 RED

最佳答案

I don't know how to set it based on the department code.

据我所知,您希望根据 Handle_Time 中的值设置每列中的值。您正在尝试定义给定部门应达到的标准,绿色 是最好的,红色 是最差的。虽然我不确定这是否是我实现它的方式,但我会这样看待它:

第 1 步

获取交易数据。因此,我们假设您已经做到了这一点,并且您已经:

Department    Handle_Time
CA 118

第 2 步

从包含标准的表中获取数据。

$department = $row['Department'];
// Make sure database request using the department. Something like:

$statement = $pdoOj.prepare("SELECT * FROM stats_threshold WHERE Department = :dep");
$statement.bindParam(':dep', $department);
// Remember to use prepared statements!

$statement->execute();
$targets_row = $statement->fetchAll();

第 3 步

您只需将处理时间中的值与每列中的值进行比较即可。所以:

$handle_time = $row['Handle_Time'];

if($handle_time <= $targets_row['Target'])
{
// Output Green.
}
else if($handle_time <= $targets_row['Yellow'])
{
// Output Yellow.
}
else
{
// If it's not less than the target, and not less than yellow, it's red.
}

只是一个旁注

您正在使用mysql_* 库。恐怕现在已弃用,因此您应该尝试切换到 PDOmysqli_*。如果您选择 PDOhereMySQL 人员提供的关于 MySQL 与 DBMS 一起使用的教程。

关于PHP 条件格式化与 MySQL 阈值表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18735389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com