JavaScript
x
<td style='color:blue;'><?php echo $row['birthday'];?></td>
I want to change the color of the text in a td element in html.
It works while loading the page, but after the page is loaded the style does not work.
How can I get it working?
Advertisement
Answer
First, you would encapsulate the logic that decides which css class to apply to the element based on the birthday inside a function makeTdClass:
JavaScript
function makeTdClass($date) {
/* compare $date with current date and return "red", "blue" or "" */
}
With such a function, you would pass in the date parameter and it would return the appropiated css class, wich you might define on a tag on in a separated .css file:
JavaScript
red {
color: red;
}
blue {
color: blue;
}
Finally, you would be able to call this function from within your PHP file and put the returned value as the class attribute of your elements:
JavaScript
<?php $tdClass = makeTdClass($row['birthday']); ?>
<td class="<?php echo $tdClass;?>"><?php echo $row['birthday'];?></td>
You can use the short way for inserting php content:
JavaScript
<td class="<?= makeTdClass($row['birthday']) ?>"><?= $row['birthday'] ?></td>