Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Color Output Values (GREEN, RED for positive, negative)?

Status
Not open for further replies.

rockypierre

Instructor
Aug 27, 2001
5
0
0
US
I want to change the font color of a <cfoutput> result to GREEN for positive, RED for negative.

Do I use <cfif>?

What would the code look like?
 
Yes...

Code:
<CFIF resultvar is &quot;positive&quot;>
  <CFSET currcolor=&quot;green&quot;>
<CFELSE>
  <CFSET currcolor=&quot;red&quot;>
</CFIF>

<CFOUTPUT>
 <FONT color=&quot;#currcolor#&quot;>#resultvar#</FONT>
</CFOUTPUT>
 
just to go that little bit further than webmigit. you can use the sgn function to get the sign of the variable (+ or -)

so your code would look like this:

<CFIF SGN(resultvar) IS 1>
<!--- positive result --->
<CFSET currcolor=&quot;green&quot;>
<CFELSEIF SGN(resultvar) IS -1>
<!--- negative result --->
<CFSET currcolor=&quot;red&quot;>
<CFELSE>
<!--- result is 0--->
<CFSET currcolor=&quot;black&quot;>
</CFIF>

<CFOUTPUT>
<FONT color=&quot;#currcolor#&quot;>#resultvar#</FONT>
</CFOUTPUT>
 
Ok, thanks, CFIF SGN works correctly for the FIRST record, but, since I'm outputting these in a single row, one at a time, (using startrow=&quot;1&quot;, startrow=&quot;2&quot;, etc.), ALL the outputs of #NOFSDIFF# in &quot;currcolor&quot; come out red, based on (I assume) the value of the FIRST record, which is negative (the second and third are positive, and should be green).

I should need to enter the CFIF statement only ONCE (not for EACH output), correct?

Here's the code:

<CFIF SGN (#TickerDiffQ.NOFSDIFF#) IS 1>
<!--- positive result --->
<CFSET currcolor=&quot;green&quot;>
<CFELSEIF SGN(#TickerDiffQ.NOFSDIFF#) IS -1>
<!--- negative result --->
<CFSET currcolor=&quot;red&quot;>
<CFELSE>
<!--- result is 0--->
<CFSET currcolor=&quot;black&quot;>
</CFIF>



<cfoutput query=&quot;TickerDiffQ&quot; STARTROW=&quot;1&quot; MAXROWS=&quot;1&quot;>

<div align=&quot;center&quot;> <font size=&quot;4&quot;><i><font color=&quot;blue&quot;><b>#MAKEABBR#</b></font></i></font></div>
</td>

<td rowspan=&quot;2&quot; height=&quot;48&quot; bgcolor=&quot;white&quot;>
<div align=&quot;center&quot;> <font size=&quot;4&quot;><i><font color=&quot;blue&quot;><b>#MODELABBR#</b></font></i></font></div>
</td>

<td height=&quot;23&quot; bgcolor=&quot;white&quot;>
<div align=&quot;center&quot;><font face=&quot;Georgia, Times New Roman, Times, serif&quot;><b><font color=&quot;black&quot;>#NOFSNEW#<br>#FLEET#%</font></b></font></div></td>

<td height=&quot;48&quot; bgcolor=&quot;white&quot;>
<div align=&quot;center&quot;><font face=&quot;Georgia, Times New Roman, Times, serif&quot;><b><font color=&quot;currcolor&quot;>#NOFSDIFF#</font></b></font></div>
</cfoutput></td>
<!--EMPTY SPACER CELL HERE: -->

<td rowspan=&quot;2&quot; height=&quot;48&quot; bgcolor=&quot;white&quot;> </td>

<!--(ok) NEXT RECORD TABLE CELLS HERE: -->
<!-- ********* -->

<td height=&quot;48&quot; bgcolor=&quot;white&quot;>

<cfoutput query=&quot;TickerDiffQ&quot; STARTROW=&quot;2&quot; MAXROWS=&quot;1&quot;>

<div align=&quot;center&quot;> <font size=&quot;4&quot;><i><font color=&quot;blue&quot;><b>#MAKEABBR#</b></font></i></font></div>
</td>

<td rowspan=&quot;2&quot; height=&quot;48&quot; bgcolor=&quot;white&quot;>
<div align=&quot;center&quot;> <font size=&quot;4&quot;><i><font color=&quot;blue&quot;><b>#MODELABBR#</b></font></i></font></div>
</td>

<td height=&quot;23&quot; bgcolor=&quot;white&quot;>
<div align=&quot;center&quot;><font face=&quot;Georgia, Times New Roman, Times, serif&quot;><b><font color=&quot;black&quot;>#NOFSNEW#<br>#FLEET#%</font></b></font></div>
</td>
<td height=&quot;48&quot; bgcolor=&quot;white&quot;>
<div align=&quot;center&quot;><font face=&quot;Georgia, Times New Roman, Times, serif&quot;><b><font color=&quot;currcolor&quot;>#NOFSDIFF#</font></b></font></div>
</cfoutput></td>
<!--EMPTY SPACER CELL HERE: -->

<td rowspan=&quot;2&quot; height=&quot;48&quot; bgcolor=&quot;white&quot;> </td>

<!--(ok) NEXT RECORD TABLE CELLS HERE: -->
<!-- ********* -->
 
:)

<font color=&quot;#IIf(TickerDiffQ.CurrentRow, DE(&quot;Black&quot;), DE(&quot;Yellow&quot;))#&quot;> Sylvano
dsylvano@hotmail.com
 
This is because you are accessing each field this way:

Code:
<CFIF SGN (#TickerDiffQ.NOFSDIFF#) IS 1>

You should not use the query name &quot;TickerDiffQ&quot; in front of the field name, since this ALWAYS refers to the first row in the query. You should instead do:

Code:
<CFIF SGN(NOFSDIFF) IS 1>

Also notice that I removed the # signs, since they are extraneous in this case because you are not outputting the variable.

-Tek
 
I am sorry, I posted wrong snippet before; You are looking for module here. if module of the query current row returns true (any non-zero number), the first value is outputed; DE() is used to &quot;delay evaluation&quot; of in plain english &quot;just print it&quot;;

<font color=&quot;#IIf(TickerDiffQ.CurrentRow MOD 2, DE(&quot;Black&quot;), DE(&quot;Yellow&quot;))#&quot;> Sylvano
dsylvano@hotmail.com
 
WWebspider, I've never had a use for it so I can't say for sure but I've heard the performance on DE() is awful... goes very slow... Have you heard/had any experience with this?

I mean, it might work but is the slowness worth it if it is slow?

Tony
 
The Evaluate(), DE(), and IIf() functions will give you highly flexible code, but they are processing intensive and should not be used when not really needed;
For what you need IIf() would be perfect, but you are right, you have to consider the processing time;
I suggest you to use GetTickCount() function to get the execution time with and without the use of IIf() function, and then you can decide what option is better in this case.

to use GetTickCount():

<cfset start = GetTickCount()>
code to execute
<cfset totaltime = GetTickCount() - start>
<output>#totaltime#</output> Sylvano
dsylvano@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top