• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Setting Colors

New Here ,
Mar 29, 2007 Mar 29, 2007

Copy link to clipboard

Copied

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.
TOPICS
Advanced techniques

Views

353

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 29, 2007 Mar 29, 2007
<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.yourvaluecol...

Votes

Translate

Translate
LEGEND ,
Mar 29, 2007 Mar 29, 2007

Copy link to clipboard

Copied

<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>

--

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2007 Mar 29, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 30, 2007 Mar 30, 2007

Copy link to clipboard

Copied

LATEST
quote:

Originally posted by: scales76
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.



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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 30, 2007 Mar 30, 2007

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation