JoJo JoJo - 2 months ago 17
CSS Question

Change td color depends on status

If i want to change bgcolor on whole tr, its work.

if ($sta =="1"){
print("<tr bgcolor=#2E0854>\n");
}
if ($sta =="2"){
print("<tr bgcolor=#000000>\n");
}

print("<td>$sta</td>\n");
print("<td>$name</td>\n");
print("<td>$info</td>\n");
print("<td>$day</td>\n");
print("</tr>\n");
}


But when i try to change td color on only one td, i cant make it work

if ($sta =="1"){
echo "<style type='text/css'>
td.farg {
background-color: #2E0854;
}
</style>";
}
if ($sta =="2"){
echo "<style type='text/css'>
td.farg {
background-color: #000000;
}
</style>";
}



print("<td>$sta</td>\n");
print("<td class='farg'>$name</td>\n");
print("<td>$info</td>\n");
print("<td>$day</td>\n");
print("</tr>\n");
}


It gives same td color for all even tho some has status 1 and some status 2

Answer

I think it is better to work with dynamic css class names instead of change the color directly.

CSS:

tr.sta-1 {
    background-color: #2E0854;
}
tr.sta-2 {
    background-color: #000000;
}

or for td:

tr.sta-1 td.farg {
    background-color: #2E0854;
}
tr.sta-2 td.farg {
    background-color: #000000;
}

PHP:

print("<tr class=\"sta-$sta\">\n");
print("<td>$sta</td>\n");
print("<td class=\"farg\">$name</td>\n");
print("<td>$info</td>\n");
print("<td>$day</td>\n");
print("</tr>\n");