How do I make a conditional formatting on my php page?

Asked

Viewed 504 times

-1

I have a table and would like to create a conditional formatting for when it appears certain text in the table, which are 5 these texts, (Beginning, half, end, canceled, awaited). change the font color, for each text a different color. Hold on, you know how to do that? Thank you

<table id="destino" class="table table-bordered table-striped">
            <thead>
             <tr>
              <th>ID</th>
              <th>Nome</th>
              <th>Cliente</th>
              <th>Data</th>
              <th>Destino</th>
             </tr>
            </thead>
            <tbody>
            <?php while($dado = $con->fetch_array()){ ?>
            <tr>
            <td><?php echo $dado["id"];?></td>
            <td><?php echo $dado["nome"];?></td>
            <td><?php echo $dado["cliente"];?></td>
            <td><?php echo $dado["data"];?></td>
            <td><?php echo $dado["destino"];?></td>
            </tr>
            <?php } ?>
            </tbody>                
            </tfoot>        
           </table>

In the Destination column where these specific text will appear.

  • first you have to show the code so we can know what you’re talking about, right because there’s no way to answer without knowing what it is

1 answer

0


Do so. At the beginning of your page, where you render PHP create an associative array of values and colors:

<?php
$color = array(
           'Inicio' => '#cor1', 
           'metade' => '#cor2', 
           'fim' => '#cor3', 
           'cancelado' => '#cor4', 
           'aguardado' => '#cor5'
         );
?>

Remembering that #cor is the hexa code, of the type #000000.

Then on your page, more precisely on the line <td><?php echo $dado["destino"];?></td> do:

<td style='color: <?= $color[$dado["destino"]] ?>'><?php echo $dado["destino"];?></td>

This way the correct position in the array will return the hexa of the color and you will have an inline style with conditional color.

  • Thanks, I’m taking the test

  • Everything worked ok, really worth

Browser other questions tagged

You are not signed in. Login or sign up in order to post.