简体   繁体   中英

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"]. "

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.

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?

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.

You can use this line:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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