I have fetched mysql results as below..I need to do is, check each $row["$date07"]
value with a threshold and highlight the cell.
while ($row = $result -> fetch_assoc())
{
$table_rows[$rowId] .= '<tr>
<td style="text-align:center"><b>'.$row['table_name'].'</td>
<td style="text-align:center;">'.$row["$date07"].'</td>
<td style="text-align:center;">'.$row["$date06"].'</td>
<td style="text-align:center;">'.$row["$date05"].'</td>
<td style="text-align:center;">'.$row["$date04"].'</td>
<td style="text-align:center;">'.$row["$date03"].'</td>
<td style="text-align:center;">'.$row["$date02"].'</td>
<td style="text-align:center;">'.$row["$date01"].'</td>
</tr>';
$table_rows[$rowId] ;
}
Here is my try...
if($row["$date07"]<$row["threshold"]){
$table_rows[$rowId] .= '<tr>
<td style="text-align:center"><b>'.$row['table_name'].'</td>
<td style="text-align:center;background-color: red;">'.$row["$date07"].'</td>
<td style="text-align:center;">'.$row["$date06"].'</td>
<td style="text-align:center;">'.$row["$date05"].'</td>
<td style="text-align:center;">'.$row["$date04"].'</td>
<td style="text-align:center;">'.$row["$date03"].'</td>
<td style="text-align:center;">'.$row["$date02"].'</td>
<td style="text-align:center">'.$row["$date01"].'</td>
</tr>';
$table_rows[$rowId] ;
}
As you can see I need thousands of if statements if I follow this way to achieve what I need(I need to check all 7 days and need to consider more than one day as well). So I need to find a better way to achieve what I need..
Is this possible to do? I have found some usefull threads if else statement inside echoed TD but need to use echo for that. Is there anyway to achieve what I need in some optimized method?
Update:
I need to consider all below cases as well when highlighting the cell.
1)Threshold breached for one day.(Ex: 2022-08-25 has breached need to highlight it)
2)Threshold breached for more than one day(Ex: 2022-08-25 and 2022-08-24 has breached. need to highlight both)
3)Threshold breached for two days but not adjacent(Ex: 2022-08-25 and 2022-08-21 breched. Still need to highlight them)
4)More than two days can be breached.. Need to highlight them all.
CodePudding user response:
You can use ternary operators within the string to check each day against the threshold, and output the extra style instructions where needed, something like this:
$table_rows[$rowId] .= '<tr>
<td style="text-align:center"><b>'.$row['table_name'].'</td>
<td style="text-align:center;'.($row["$date07"] < $row["threshold"] ? "background-color:red;" : "").'">'.$row["$date07"].'</td>
<td style="text-align:center;'.($row["$date06"] < $row["threshold"] ? "background-color:red;" : "").'">'.$row["$date06"].'</td>
<td style="text-align:center;'.($row["$date05"] < $row["threshold"] ? "background-color:red;" : "").'">'.$row["$date05"].'</td>
<td style="text-align:center;'.($row["$date04"] < $row["threshold"] ? "background-color:red;" : "").'">'.$row["$date04"].'</td>
<td style="text-align:center;'.($row["$date03"] < $row["threshold"] ? "background-color:red;" : "").'">'.$row["$date03"].'</td>
<td style="text-align:center;'.($row["$date02"] < $row["threshold"] ? "background-color:red;" : "").'">'.$row["$date02"].'</td>
<td style="text-align:center;'.($row["$date01"] < $row["threshold"] ? "background-color:red;" : "").'">'.$row["$date01"].'</td>
</tr>';
Demo: https://3v4l.org/NV8vF