4 Replies Latest reply on Mar 30, 2007 5:29 AM by Dan Bracuk

    Setting Colors

    scales76
      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.
        • 1. Re: Setting Colors
          Level 7
          <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
          • 2. Re: Setting Colors
            scales76 Level 1
            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.

            • 3. Re: Setting Colors
              SeanACorfield Level 1
              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?
              • 4. Re: Setting Colors
                Dan Bracuk Level 5
                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