Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / March 2007



Tip: Looking for answers? Try searching our database.

Setting Colors

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
scales76 - 30 Mar 2007 04:52 GMT
I am wondering how to use a cfif statement to set the background color of a table cell based on the percentage number that is outputted from a db.
Azadi - 30 Mar 2007 05:19 GMT
<td style="background-color:<cfif yourvalue lt 50>##333399;<cfelseif
[yourvalue] lt 75>##990000;<cfelse>##000000;</cfif>">#yourvalue#</td>

that may be a lot of cfif's though, depending how many values and cells
you have...

if your output table only shows one value on a row (instead of several
values side by side in columns), then you could do something like:

<table>
<cfoutut query="yourquery">
<cfif yourquery.yourvaluecolumn lt 50>
    <cfset bgc = "##333399">
<cfelseif yourquery.yourvaluecolumn lt 75>
    <cfset bgc = "##990000">
<cfelse>
    <cfset bgc = "">
</cfif>
<tr><td style="background-color:#bgc#;">yourvalue</td></tr>
</cfoutput>
</table>

Signature

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com

scales76 - 30 Mar 2007 05:53 GMT
Thanks I think I can work with that , it is to display results from a survey,
there are 137 answers, depending on the answer it will display a different
color, e.g.  out of 1-4 ,if it is 3-4 it is green, if it is 2-3 it is yellow,
if is it below 2 it is red. it is the same for all the questions, to save code
is there a way to dynamically control it, using a loop.
Dan Bracuk - 30 Mar 2007 13:29 GMT
[q][i]Originally posted by: [b][b]scales76[/b][/b][/i]
Thanks I think I can work with that , it is to display results from a survey,
there are 137 answers, depending on the answer it will display a different
color, e.g.  out of 1-4 ,if it is 3-4 it is green, if it is 2-3 it is yellow,
if is it below 2 it is red. it is the same for all the questions, to save code
is there a way to dynamically control it, using a loop.

[/q]
Do it in your query.  In most db's except access, the syntax is
select case
when this = that then something
when this > that then something_else
else another_thing_altogether
end as an_alias
SeanACorfield - 30 Mar 2007 08:10 GMT
You might want to create an array of color values and use that instead of if /
loop stuff:

<cfset colors = arrayNew(1)>
<cfset colors[1] = "red">
<cfset colors[2] = "yellow">
<cfset colors[3] = "yellow">
<cfset colors[4] = "green">

(or whatever color values you need)

Then you can simply use colors[answer] as the color.

Hope that helps?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.