J'essaie de faire une mise en forme conditionnelle pour les résultats $row->cap24hrChange
. Si c'est < 0
, je veux que la valeur soit colorée en rouge, sinon elle devrait être verte. Je ne reçois aucune réponse du script.Javascript formatage conditionnel à l'intérieur de PHP
<table><head><style>
td {text-align: right;}
</style></head><table>
<thead>
<tr>
<th>#Rank</th>
<th>Name</th>
<th>Price</th>
<th>Mkt Cap</th>
<th>Volume</th>
<th>Supply</th>
<th>24h(%)</th>
</tr>
</thead>
<tbody>
<?php function compare($a, $b) {
return intval($a->position24) - intval($b->position24);
}
$json = file_get_contents('http://www.coincap.io/front');
$data = json_decode($json);
usort($data, 'compare');
?>
<?php foreach ($data as $row) { ?>
<tr>
<td><?= $row->position24; ?></td>
<td><?= $row->long; ?></td>
<td><?= number_format($row->price, 4); ?><\td>
<td><?= number_format($row->mktcap, 2); ?><\td>
<td><?= number_format($row->volume, 2); ?><\td>
<td><?= number_format($row->supply, 2); ?><\td>
<td><?= $row->cap24hrChange; echo "<script type=\"text/javascript\"> var trTags = document.getElementsByTagName("td"); for (var i = 0; i < trTags.length; i++) { var tdEightEl = trTags[i].children[7]; if (tdEightEl.innerText < 0) {tdEightEl.style.color = "red"; } else if (tdEightEl.innerText > 0) {tdEightEl.style.color = "green"; } }</script>";?></td>
</tr>
<?php } ?>
</tbody>
</table>
EDIT
Sur une note très connexe: comment pourrais-je travailler autour de ce code afin d'inclure des icônes à droite (↑ et ↓) pour les valeurs> 0 et < 0, respectivement?
EDIT (2)
j'ai tout compris avec l'aide de $foo = "bar".$foo
<td style="color:<?php if($row->cap24hrChange > 0){ echo "green"; $row->cap24hrChange = $row->cap24hrChange . " ";}else if($row->cap24hrChange < 0){ echo "red"; $row->cap24hrChange = $row->cap24hrChange . " ";}?>"><?= $row->cap24hrChange;?></td>
C'est vraiment super! Merci =) – tklein