简体   繁体   English

根据值更改文本颜色

[英]Change text color depending on value

Have the following to pull records from the database:有以下从数据库中提取记录:

echo "
<tr>
<td valign='top'>" . $row["id"]. "</td>
<td valign='top'>" . $row["fecha"]. "</td>
<td valign='top'>" . $row["numero"]. "</td>
<td valign='top' align='left'>" . $row["cliente"]. "<br>" . $row["addr1"]. "<br>" . $row["addr2"]. "</td>
<td valign='top'>" . $row["cif"]. "</td>
<td valign='top' align='left'>" . $row["trabajo"]. "</td>
<td valign='top' align='left'>&euro; " . $row["cantidad"]. "</td>
<td valign='top'>" . $row["status"]. "</td>
<td valign='top'><h2><i class='fas fa-edit'></i></h2></td>
</tr>";

Now when it shows the "status" record there are 2 values (dropdown box on form) "PRO FORMA" and "PAGADO" that I can echo on the results page using ". $row["status"]. "现在,当它显示“状态”记录时,有 2 个值(表单上的下拉框)“PRO FORMA”和“PAGADO”,我可以使用“.$row[“status”] 在结果页面上回显它们。”

I want to find out how to change the color of the text depending on the result, PRO FORM in red and PAGADO can stay as is.我想了解如何根据结果更改文本的颜色,红色的 PRO FORM 和 PAGADO 可以保持原样。

Anyone can give me a little push into the right direction?任何人都可以给我一点推动正确的方向?

From what I understand you want to change the text color of the table depending on the value contained in the variable $row['status'] , correct?据我了解,您想根据变量$row['status']中包含的值更改表格的文本颜色,对吗?

For this, an idea is to use the ternary operator in this way:为此,一个想法是以这种方式使用三元运算符:

echo "
<tr ".((strcmp($row['status'], 'PAGADO') != 0) ? ('style="color: red;"') : ('')).">
...
</tr>";

Here in this example I would only change the first line because I used the algorithm in the first tr, but if you want to apply it to all lines, just use that algorithm in the line where the table tag is.在此示例中,我只更改第一行,因为我在第一个 tr 中使用了算法,但如果要将其应用于所有行,只需在 table 标记所在的行中使用该算法。

You can use this line:-您可以使用此行:-

<td valign='top' style='" . (($row['status'] == 'PRO FORM') ? 'color:#f00' : '') . "'>" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM