Forum Moderators: coopster

Message Too Old, No Replies

Format of numbers

red/black

         

johnnydequino

4:49 am on Aug 3, 2004 (gmt 0)

10+ Year Member



I am working with php/mysql database. My database has both negative and postive numbers. Is there any way to make the numbers return red if negative or black if positive in php, or would java script be best? My entire site is php and would love to use php if possible.

jd

dkin

5:54 am on Aug 3, 2004 (gmt 0)

10+ Year Member



I am very new to php as well and there is probably a better way than this but I will try.

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.

Warboss Alex

1:55 pm on Aug 3, 2004 (gmt 0)

10+ Year Member



I think the CSS gurus would be more annoyed at you using the <font> tag than at the PHP!

You could probably do it in less code, something like:

<?php

if ($num <= 0) $color = 'red'; else $color = 'black';
print "<font color='$color'>$num</font>";

?>

coopster

2:43 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



First, both answers are spot-on. But I would like to take a moment to address the "complication" matter.

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>

Advantage? Maybe we want the negative figures to be green now instead of red and the positive numbers to be blue. Now, all we have to do is change the stylesheet.
<style type="text/css"> 
.negative {color: #00FF00;}
.positive {color: #0000FF;}
</style>
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 ;)

dkin

5:45 pm on Aug 3, 2004 (gmt 0)

10+ Year Member



Do not mean to argue or be a nuisance but when you give a code like that to someone who has started learning php yesterday all that is done is complicate things. That is how I learned, by trying to navigate my way through complicated (or simplified) code. But how many new phpers know of the Ternary operator.

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

coopster

7:33 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The link regarding the ternary operator above takes you to the PHP manual page that explains that particular comparison operator quite nicely. The ternary operator does exactly what the snippet provided in msg#2 does, just in a single line as opposed to multiple lines of code.

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.

johnnydequino

2:02 am on Aug 23, 2004 (gmt 0)

10+ Year Member



I am still working on this red/black positive/negative number issue.

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

Patrick Taylor

2:15 am on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jd, I thought the makings of an answer were in posts #2 and #3? From that you can easily detect whether the number is positive or negative and use that to set any variable you like, depending on the way you're actually displaying your output html.

dkin

2:40 am on Aug 23, 2004 (gmt 0)

10+ Year Member



if ($num <= "0") {
print "<font color=\"red\">($num)</font>";
}
else {
print "<font color=\"000000\">$num</font>";
}

if using for multiple rows put it in a loop.

Spook

8:17 am on Aug 23, 2004 (gmt 0)

10+ Year Member



Hi coopster.

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

johnnydequino

12:51 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



I can't seem to get a number to return. Here is my code below:

<? if ($row->rebateprice <= 0) $color = '#CC0000'; else $color = '#000000';
"<font color='$color'>$row->rebateprice</font>";?>

The page returns, put the rebateprice is blank. Syntax error?

jd

coopster

2:27 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



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.



jd, you need to send it to the browser.
<? 
if ($row->rebateprice <= 0) {
$color = '#CC0000';
} else {
$color = '#000000';
}
print [php.net] "<font color='$color'>$row->rebateprice</font>";
?>

Patrick Taylor

2:36 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Should it not be $rebateprice?

coopster

2:37 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, probably not, as I believe mysql_fetch_object() [php.net] is being used here.

johnnydequino

2:52 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Yes, that is correct, I am using a mysql_fetch_object().

Last question, anyway to get the negative numbers with brackets like this (100)?

It now prints red or black, very nice!

Thanks all for your help!

jd

coopster

3:08 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Since you have decided to use this method, we'll continue down that road...
<?  
if ($row->rebateprice <= 0) {
$row->rebateprice = "($row->rebateprice)"; // added parentheses to the value
$color = '#CC0000';
} else {
$color = '#000000';
}
print "<font color='$color'>$row->rebateprice</font>";
?>

johnnydequino

3:28 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Perfect! Thanks all for your help. PHP is the coolest language out there.

jd

Spook

4:05 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Yes, see the "ternary operator as opposed to an if/else statement" link in msg#4.

Ah yes - The scroll bar! That's what its for!

Many thanks

Spook