Tarik Kdiry
Tarik Kdiry

Reputation: 57

Changing color of whole row in a table when a condition is met?

How do I change the color of a whole row in a table when a condition is met?

For example, FAILURE is returned? Make the whole row RED.

<tr> <!-- Disk Space Available -->
                <td class="tg-yw4l">ld</td>
                <td class="tg-yw4l">
                <?php 
                    if ($ld_status == 0) {
                        echo 'SUCCESS';
                    } else if ($ld_status == 1) {
                        echo 'WARNING';
                    } else {
                        echo 'FAILURE';
                    }   
                ?>
                </td>

Upvotes: 0

Views: 1023

Answers (2)

Kld
Kld

Reputation: 7068

You can generate the class of the tr with PHP, then in the css you can change the style of each class.

CSS

.success{
  background-color: green;
  }

.warning{
  background-color: yellow;
  }

.failure{
  background-color: red;
  }

HTML

<tr class=" <?php 
                    if ($ld_status == 0) {
                        echo 'success';
                    } else if ($ld_status == 1) {
                        echo 'warning';
                    } else {
                        echo 'failure';
                    }   
                ?>"> 

                <td class="tg-yw4l">ld</td>
                <td class="tg-yw4l">
                <?php 
                    if ($ld_status == 0) {
                        echo 'SUCCESS';
                    } else if ($ld_status == 1) {
                        echo 'WARNING';
                    } else {
                        echo 'FAILURE';
                    }   
                ?>
                </td>

Upvotes: 3

Vinod Kumawat
Vinod Kumawat

Reputation: 741

Try This it will be work proper for your condition

 <html>
    <body>
    <?php $ld_status=2; ?>
    <table border="1">
    <tr style="background:<?php if($ld_status == 0) { echo 'green'; } else if ($ld_status == 1){ echo 'yellow';  } else { echo 'red';}?>;" > <!-- Disk Space Available -->
                    <td class="tg-yw4l">ld</td>
                    <td class="tg-yw4l">
                    <?php 
                        if ($ld_status == 0) {
                            echo 'SUCCESS';
                        } else if ($ld_status == 1) {
                            echo 'WARNING';
                        } else {
                            echo 'FAILURE';
                        }   
                    ?>
                    </td>
            </table>        
    </html>

Upvotes: 0

Related Questions