Forum Moderators: coopster
if ($num <= "0") {
print "<font color=\"red\">$num</font>";
}
else {
print "<font color=\"000000\">$num</font>";
}
Like I said there is most likely a more complicated way to do this, many people seem to like as complicated code as they can but for a new phper this should suffice.
Let me know.
I wouldn't say that "people seem to like as complicated code as they can", but more along the lines of less code and more function. At one point, most of us have written strings of logic and still do when the situation calls for it. What we have learned over the years is that we can simplify our lives by employing certain programming strategies/tactics. One of those, as mentioned, is Cascading Style Sheets (CSS). CSS allows us to separate markup and information.
This example might look more "complicated", but it really isn't. You notice that the same logic (here I've demonstrated the ternary operator [php.net] as opposed to an if/else statement) has been used to determine whether or not the figure meets certain markup restrictions, and if so, we apply them.
<?php
$table = ''; //initialize
for ($i = -5; $i <= 5; $i++) {
$table .= '<tr><td class="';
$table .= ($i < 0)? 'negative' : 'positive';
$table .= "\">$i</td></tr>";
}
?>
<html><head><title>Title</title>
<style type="text/css">
.negative {color: #FF0000;}
.positive {color: #000000;}
</style>
</head><body>
<table>
<?php print $table;?>
</table>
</body></html>
<style type="text/css">So you see, I wouldn't call it complication, but probably quite the opposite, simplification. If we all keep sharing our knowledge here, we all get to keep learning the tricks of the trade ;)
.negative {color: #00FF00;}
.positive {color: #0000FF;}
</style>
Coopster I completely respect yo for your talent and knowledge of php, I hope I will know how to use the Ternary operator someday (no sarcasm),you have helped me many many times. Its just some codes that people give to help starters out confuses and/or angers me.
Anyhow any of these codes should work.
Cheers
None of us are here to confuse or anger anyone, especially folks that contribute and offer help to someone in need! That is why I opened my post with a credit to both responders. We don't ever want anyone to feel intimidated nor afraid to post questions or offer assistance. Every respectful contribution is invited and welcomed. But please note that you will often see different ways to accomplish goals on this forum. That is one of the biggest advantages of a forum where everybody is contributing -- we all benefit. If not, you may never had known about the ternary operator :-)
Thanks for the compliment and please accept mine -- thank you, your contribution here is sincerely appreciated.
I am returning numbers from a mysql database, in multiple <?row>. Is there anyway the numbers returned get turned into red or black depending on positive, negative number, and have the negative number with red brackets like this? (100)
Is there a possible function to do this even though the numbers are coming from mysql?
Thanks for everyones help -
jd
Not wishing to hijack this thread, but could you explain the following lines of your code for me?
Firstly:
for ($i = -5; $i <= 5; $i++)
The counter I understand, but why did you chose the limits [-5 and <=5].
And secondly:
$table .= ($i < 0)? 'negative' : 'positive';
I don't remember seeing this anywhere. Presumably this is a different way of writing an if/else statment. But the use of the ()? I have never seen, nor the : between the two possible values of the variable.
Thanks in advance.
Spook
why did you chose the limits [-5 and <=5]
Random number range example to show negative and positive numbers.
Presumably this is a different way of writing an if/else statment.
Yes, see the "ternary operator as opposed to an if/else statement" link in msg#4.
<?
if ($row->rebateprice <= 0) {
$color = '#CC0000';
} else {
$color = '#000000';
}
print [php.net] "<font color='$color'>$row->rebateprice</font>";
?>