0

I'm selecting data from mysql into an HTML table and looking for a way to change the css of the depending on that particular row data.

So far I have had the following code:

CSS

    .rowColor {
    background-color: <?php $row_color ?>
}

PHP

if ($row['status'] === "approved") {
    $row_color = "green;";
} else {
    $row_color = "orange;"; 
}

HTML

<tr class="rowColor">
<td>{$row['firstName']}</td>
<td>{$row['lastName']}</td>
<td>{$row['status']}</td>

Currently each row will become orange regardless of what the status datas is.

  • Welcome to stackoverflow. Did you inspect "view page source"? or inspect code? Also,, try to print $row['status'] values.... looks like straight forward problem... inspecting html generated, and variable values should resolve the issue... – tired and bored dev Mar 05 '20 at 11:26
  • Everything works as it should except the colors of each – Peter Gilbertson Mar 05 '20 at 11:40
  • @PeterGilbertson `Everything works except my problem` won't help anyone who tries to help you. Your code is not enough for others to understand your problem. Please provide more information. For example, `for-each` loop both in PHP and HTML. – glinda93 Mar 05 '20 at 13:05

2 Answers2

0

Instead of applying css to <tr>, you can change background-color inside style attribute as below

<tr style="background-color:<?php echo $row['status'] == 'approved' ? 'green' : 'orange'?>;">
<td>{$row['firstName']}</td>
<td>{$row['lastName']}</td>
<td>{$row['status']}</td>

Give it a try

0

A better practice is to apply a style class. Using inline CSS is discouraged.

<tr class="<?php echo $row['status'] == 'approved' ? 'status-approved' : 'status-pending'?>;">

css:

tr.status-approved
{
   background-color:green;
}

tr.status-pending
{
   background-color: orange;
}
Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26