How change cell table color in php based on cell value?

rpirez

I just started programming in php.

I made a table that returns values ​​from the database. I'm trying to do the following: Change the color of the third column by the value it has.

I was thinking this way, but the values ​​are repeated and the color is not the right way.

Can anyone help me please?

Did you understand my question?

Thank you all. I apologize for the inconvenience.

Code:

<table style="width:324px;" border="3" cellspacing="1" cellpadding="1">
    <tr>
        <td style="background-color:#A4A4A4;">
            <b><font face="Arial, Helvetica, sans-serif">Task</font></b>
        </td>
        <td style="background-color:#A4A4A4;">
            <b><font face="Arial, Helvetica, sans-serif">deadline</font></b>
        </td>
        <td style="background-color:#A4A4A4;">
            <b><font face="Arial, Helvetica, sans-serif">Status</font></b>
        </td>
    </tr>
<?php
$i=0;
while ($i < $num)
{
    $f1=mysql_result($result,$i,"Task");
    $f2=mysql_result($result,$i,"deadline");
    $f3=mysql_result($result,$i,"status");

    ?>
    <tr style="background-color:#A4A4A4;">
        <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
        <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
        <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
        <? 
        if($f3==2)
        { 
            // Display RED 
            ?> 
            <td style="background-color:#FF0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> 
            <? 
        }
        elseif($f3==1)
        { 
            // Display YELLOW 
            ?> 
            <td style="background-color:#0F0;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> 
            <?     
        }
        else
        { 
            ?> 
            <td  style="background-color:#F00;"><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> 
            <? 
        } 
        ?> 
    </tr> 
    <?php
    $i++;
}
?>
</table>

Result from code:

enter image description here

SOLVED

Thanks everyone for your help. Status is a predefined value recorded in the database. Only ranges between 1 and 4 so that the resolution has served @Daniel Andre correctly. All responses were valid for me to try to understand a little more php, but it was so simple. Again thank you all for your time and for your help.

The final result was:

<table style="width:324px;" border="3" cellspacing="1" cellpadding="1">
<tr>

<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Tarefa</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Prazo</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Status</font></b>
</td>
</tr>

<?php
$i=0;while ($i < $num) {
$f1=mysql_result($result,$i,"idTarefa");
$f2=mysql_result($result,$i,"PrazoExecucao");
$f3=mysql_result($result,$i,"status");

?>
<tr style="background-color:#A4A4A4;">
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<!--<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>-->

<?php
  $status_colors = array(1 => '#0000FF', 2 => '#00FF00', 3 => '#FFFF00', 4 => '#FF0000');
?>
<td style="background-color: <?php echo $status_colors[$f3]; ?>;">
</tr> 


<?php
$i++;
}
?>
</table>

enter image description here

Daniel Andre

If the statuses are static (meaning, they don't change, but are a fixed set of statuses), you can make a php array mapping the statuses to colors:

<?php
  $status_colors = array(1 => '#FF0', 2 => '#F0F', 3 => '#0FF', 4 => '#0F0');

Then, in your td's, use the correct status color:

<td style="background-color: <?php echo $status_colors[$f3]; ?>;">

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How change cell table color in php based on cell value?

From Dev

DATA Table ... How to change the Color of cell as per the Cell Value Of Table

From Dev

How to change DataGrid cell background color based on cell value

From Dev

How to change background color of cell based on other cell value by VBA

From Dev

Using javascript to change table cell background color based on json value

From Dev

How to change the row color based value of Cell in list view

From Dev

How to Change the row Background color based on cell value

From Dev

How to change font color in Excel based on cell value?

From Dev

How to Change the row Background color based on cell value

From Dev

Change a cell color based on value - VueJs & Vuetify

From Dev

Change color of the td cell based on the value in database

From Dev

Change Row background color based on cell value

From Dev

Change cell color based on value faster

From Dev

Change cell value based on cell color in google spreadsheet

From Dev

How do I change the value of a cell based on the position of a team in a table?

From Dev

Color Cell based on value

From Dev

AngularJS: Change cell class in a dynamically generated table based on cell value

From Dev

how to change the table cell color using jquery

From Dev

How to Change Conditional Formatting of a Cell Based on a Change in the Cell Value

From Dev

How to change the css style and link of a previous table cell based on current cell value in jQuery

From Dev

Change interior color based on cell value of color constant

From Dev

Change value in a cell based on value in another cell

From Dev

Color Cell Based On Text Value

From Dev

Change Row background color based on cell value DataTable

From Dev

Excel "Insert shapes" Color change based on cell value

From Dev

change color of Jtable cell based on resultset value from msyql

From Dev

VBA-Change color of cells based on value in particular cell

From Dev

jquery datatable.net change row color based on value of cell

From Dev

Conditional change of font color based on value in a different cell

Related Related

  1. 1

    How change cell table color in php based on cell value?

  2. 2

    DATA Table ... How to change the Color of cell as per the Cell Value Of Table

  3. 3

    How to change DataGrid cell background color based on cell value

  4. 4

    How to change background color of cell based on other cell value by VBA

  5. 5

    Using javascript to change table cell background color based on json value

  6. 6

    How to change the row color based value of Cell in list view

  7. 7

    How to Change the row Background color based on cell value

  8. 8

    How to change font color in Excel based on cell value?

  9. 9

    How to Change the row Background color based on cell value

  10. 10

    Change a cell color based on value - VueJs & Vuetify

  11. 11

    Change color of the td cell based on the value in database

  12. 12

    Change Row background color based on cell value

  13. 13

    Change cell color based on value faster

  14. 14

    Change cell value based on cell color in google spreadsheet

  15. 15

    How do I change the value of a cell based on the position of a team in a table?

  16. 16

    Color Cell based on value

  17. 17

    AngularJS: Change cell class in a dynamically generated table based on cell value

  18. 18

    how to change the table cell color using jquery

  19. 19

    How to Change Conditional Formatting of a Cell Based on a Change in the Cell Value

  20. 20

    How to change the css style and link of a previous table cell based on current cell value in jQuery

  21. 21

    Change interior color based on cell value of color constant

  22. 22

    Change value in a cell based on value in another cell

  23. 23

    Color Cell Based On Text Value

  24. 24

    Change Row background color based on cell value DataTable

  25. 25

    Excel "Insert shapes" Color change based on cell value

  26. 26

    change color of Jtable cell based on resultset value from msyql

  27. 27

    VBA-Change color of cells based on value in particular cell

  28. 28

    jquery datatable.net change row color based on value of cell

  29. 29

    Conditional change of font color based on value in a different cell

HotTag

Archive