i cant seem to solve this.

how do i change the cell background color using php?

this is what i got;

<tr>
<td ><span class="style2">1</span></td>
<td >PERAK IP-F002</td>
<td bgcolor=<? if($a1==1) echo "#00ff00"; else echo "#ff0000"; ?>>&nbsp;</td>
<td bgcolor=<? if($a2==1) echo "#FF0000"; else echo "#00FF00"; ?>>&nbsp;</td>
<td bgcolor=<? if($a3==1) echo "#00ff00"; else echo "#ff0000"; ?>>&nbsp;</td>
</tr>
<tr>

is this correct?

Recommended Answers

All 2 Replies

Try this

<tr>
<td ><span class="style2">1</span></td>
<td >PERAK IP-F002</td>
<td style="background-color:<?php if($a1==1) echo "#00FF00"; else echo "#FF0000"; ?>">&nbsp;</td>
<td style="background-color:<?php if($a2==1) echo "#FF0000"; else echo "#00FF00"; ?>">&nbsp;</td>
<td style="background-color:<?php if($a3==1) echo "#00FF00"; else echo "#FF0000"; ?>">&nbsp;</td>
</tr>

You can also use ternary operator for minimizing code.

<tr>
<td ><span class="style2">1</span></td>
<td >PERAK IP-F002</td>
<td style="background-color:<?=($a1==1)?('#00FF00'):('#FF0000');?>">&nbsp;</td>
<td style="background-color:<?=($a2==1)?('#FF0000'):('#00FF00');?>">&nbsp;</td>
<td style="background-color:<?=($a3==1)?('#00FF00'):('#FF0000');?>">&nbsp;</td>
</tr>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.