I heard somewhere that it's better to have the server perform tasks, such as concatenation, but does that also apply to aesthetic stuff as well? How about using FORMAT(val, 2) vs NUMBER_FORMAT(val, 2)?
It seems like double the work, because I will still need to fetch the non-concatenated value(s) to perform other calculations, like getting the sum of all rows.
i.total AS total,
CONCAT('$', FORMAT(i.total, 2)) AS nice_total,
... etc
$sum_total = 0;
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$sum_total += $row['total']; #calculate sum of all rows
?>
<tr>
<td><?php echo $row['nice_total']; ?></td> <!-- Example 1 -->
</tr>
<?php
}
?>
<tr>
<td>$<?php echo NUMBER_FORMAT($sum_total, 2); ?></td>
</tr>
VS
i.total AS total,
... etc
$sum_total = 0;
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$sum_total += $row['total']; #calculate sum of all rows
?>
<tr>
<td>$<?php echo NUMBER_FORMAT($row['total'], 2); ?></td> <!-- Example 2 -->
</tr>
<?php
}
?>
<tr>
<td>$<?php echo NUMBER_FORMAT($sum_total, 2); ?></td>
</tr>
I only have 4 fields to fetch with a similar structure (subtotal, tax1, tax2, total), but to fetch the same column twice for concatenation (or even formatting) feels wrong, especially if I have tons of rows; i'm just not sure how to measure the pros/cons. I do like to keep my logic as close together as possible, if that matters.
Thanks!
"It seems like double the work, because I will still need to fetch the non-concatenated value(s) to perform other calculations, like getting the sum of all rows."
Go with your gut here. Why make the DB server work when you can just echo the "$" as you are in your plain HTML? The only thing I that might be wrong with the 2nd approach is that you'll have a "$" printed regardless of whether or not there is an actual value yielded by $row['total'] ...