Reputation: 55
I have a php query which displays values after fetching data from database. The displayed values are like 'T', 'F' or 'R'. The echo command is working fine. I want to change the color of specific values when they are displayed in web page. For example- 'T' should be in green, 'F' should be in red and 'R' in yellow. How can I acheive that?
Thanks!
Following is a part of my code. Where should I insert the styling elements in the code?
$sql = "select * from <database table> where attribute1 = '$val1' and attribute2 = '$val2'";
$fetch = $conn->query($sql);
if ($fetch->num_rows > 0)
{
// output data of each row
while($row = $fetch->fetch_assoc())
{
echo
"<tr>
<td>".$row["col1"]."</td>
<td>".$row["col2"]."</td>
<td>".$row["col3"]."</td>
<td>".$row["col4"]."</td>
<td>".$row["col5"]."</td>
</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
Upvotes: 1
Views: 533
Reputation: 706
This can be done with in-line styles, however this is bad practice. Optimally, you'll want to apply classes to the output.
e.g.
<td class="green"></td>
<td class="red"></td>
<td class="yellow"></td>
Then within your CSS file, you'll have corresponding CSS classes.
.green{
color: #0f0;
}
Read more on the CSS property color
on MDN.
Edit: adding part of my comment to the answer, based on OP's comment.
if($row['col1'] === 'T')
{
$class = "green";
}
then when outputting the <td></td>
s, loop:
echo "<td class='$class'></td>";
Alternatively:
echo '<td class="' . $class . '"></td>;
Upvotes: 2
Reputation: 39494
Create yourself a function that takes a value and outputs the <td>
with the appropriate styling. I'm using inline styles here, but you can instead use CSS classes:
function color($value) {
static $map = [ 'T' => 'green', 'F' => 'red', 'R' => 'yellow' ];
return sprintf(
'<td style="color:%s">%s</td>',
array_key_exists($value, $map) ? $map[$value] : 'black',
$value
);
}
Now, instead of echoing the cell data directly, wrap it in a call to this function, like:
while($row = $fetch->fetch_assoc())
{
echo
"<tr>".
color($row["col1"]).
color($row["col2"]).
color($row["col3"]).
color($row["col4"]).
color($row["col5"]).
</tr>";
}
Keeping the logic in a function lets you change the logic later without affecting the callers.
Upvotes: 1
Reputation: 1702
If the col1
in the database table has these single values T
, F
and R
on each rows respectively, consider the following code:
<?php
$color_codes = array(
'T' => 'green',
'F' => 'red',
'R' => 'yellow'
);
$sql = "select * from <database table> where attribute1 = '$val1' and attribute2 = '$val2'";
$fetch = $conn->query($sql);
if ($fetch->num_rows > 0) {
// output data of each row
while($row = $fetch->fetch_assoc()) {
echo "<tr>
<td style='color: " . $color_codes[$row['col1']] . "'>" . $row["col1"] . "</td>
<td>" . $row["col2"] . "</td>
<td>" . $row["col3"] . "</td>
<td>" . $row["col4"] . "</td>
<td>" . $row["col5"] . "</td>
</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
?>
OR
<style>
.red {
color: red;
}
.green {
color: green;
}
.yellow {
color: yellow;
}
</style>
<?php
$color_codes = array(
'T' => 'green',
'F' => 'red',
'R' => 'yellow'
);
$sql = "select * from <database table> where attribute1 = '$val1' and attribute2 = '$val2'";
$fetch = $conn->query($sql);
if ($fetch->num_rows > 0) {
// output data of each row
while($row = $fetch->fetch_assoc()) {
echo "<tr>
<td class='" . $color_codes[$row['col1']] . "'>" . $row["col1"] . "</td>
<td>" . $row["col2"] . "</td>
<td>" . $row["col3"] . "</td>
<td>" . $row["col4"] . "</td>
<td>" . $row["col5"] . "</td>
</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
?>
The only difference in the second code is the class
attribute is added and style
attribute is removed to define CSS rules for each color in the stylesheet. Both code works.
You can stick with the code that you're comfortable with. But the first one is flexible because you don't need to add CSS rules in the stylesheet for each color.
Hope it helps!
Upvotes: 0