Text Color Based on DB Value

Discussion in 'ASP General' started by Chris Stanley, Jul 27, 2005.

  1. I am using dreamweaver to make an ASP page. I have a table of data
    from a MSSQL database that is currently showing on the ASP page. I
    want the ability to change the color based on a priority number that's
    in the MSSQL database (same recordset that the data comes from). I
    don't have the priority field out on the page but can put it out if
    need be. I have 3 different priority numbers and if the first row is
    priority 1 I want the font to be red and if the second row is priority
    2 I want the color to be yellow and so on.


    Chris Stanley, Jul 27, 2005
    1. Advertisements

  2. col = rs("priority")
    if col = 1 then
    fontcolor = "#FF0000"
    elseif col = 2 then
    fontcolor = "#FFFF00"
    end if
    response.write "<span style='color:" & fontcolor & "'>" & stuff & "</span>"
    Aaron Bertrand [SQL Server MVP], Jul 27, 2005
    1. Advertisements

  3. Thanks for the quick response. Is there a certain place I need to put
    this code???

    Chris Stanley, Jul 27, 2005

  4. You could use the priority number in your CSS class name like so:

    <style type="text/css">
    tr.priority1 { background-color: #ff0000; }
    tr.priority2 { background-color: #ffff00; }
    tr.priorityAndSoOn { background-color: #AndSoOn; }

    Do While Not yourRS.EOF
    sPriority = yourRS.Fields.Item("Priority").Value & "")
    <tr class="priority<%=sPriority%>">

    Ray at work
    Ray Costanzo [MVP], Jul 27, 2005
  5. Thanks for the quick response. Is there a certain place I need to put
    Uh, well, probably inside the loop...
    Aaron Bertrand [SQL Server MVP], Jul 27, 2005
  6. tr.priority1 { background-color: #ff0000; }
    Ooh, I like that approach even better than my suggestion.

    I get light-headed when I see HTML code that is half <font color=#blah>
    tags... especially when they're all the same. I always find myself pounding
    my head against the wall, dying to ask when they're going to look up this
    "CSS" concept they may have heard about.
    Aaron Bertrand [SQL Server MVP], Jul 27, 2005
  7. I'm very new to CSS, is this something DWeaver can do in the CSS
    Styles??? or do I need to just use the code above??

    Chris Stanley, Jul 27, 2005
  8. I'm trying my best always to use CSS, but it gets my goat. Unless I'm
    missing something, not ~every~ inline attribute has a CSS equivalent, and
    that's frustrating!

    Ray at work
    Ray Costanzo [MVP], Jul 27, 2005
  9. I haven't gotten anything to work so far. I'm probably doing something
    wrong. I tried the first code but no color changes. I didn't try the
    CSS b/c I don't really know what to do with it. I am very new to this.

    Chris Stanley, Jul 27, 2005
  10. Are you just going to leave us hanging like that!? :) What inline display
    attribute is not represented in CSS? Also, which version of CSS are we
    talking about 1, 2, 2.1, 3? Inquiring minds want to know.
    Chris Hohmann, Jul 27, 2005
  11. Can you show us the code you have thus far?
    Chris Hohmann, Jul 27, 2005
  12. OK, here's my code:

    <title>Surry County E-911 Active Calls</title>
    <meta http-equiv="Content-Type" content="text/html;
    <style type="text/css">
    <style type="text/css">
    <meta http-equiv="Refresh" content="30">

    <body bgcolor="#000000">
    <div align="center">
    <p><font color="#FFFFFF" size="7" face="Tahoma">Surry County E-911
    Active Calls</font></p>

    <p><font color="#FF0000"> </MM:DECORATION></font></p>
    <table width="97%" border="0" align="center">
    <td width="14%"><div align="left"><font color="#FF0000" size="5"
    <td width="16%"><div align="left"><font color="#FF0000" size="5"
    <td width="38%"><div align="left"><font color="#FF0000" size="5"
    <td width="18%"><div align="left"><font color="#FF0000" size="5"
    <td width="14%"><div align="left"><font color="#FF0000" size="5"
    While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
    <table width="97%" border="0" align="center">
    <td width="15%"><font color="#00FFFF"><strong><font size="3"
    <td width="16%"><font color="#00FFFF"><strong><font size="3"
    <td width="38%"><div align="left"><font color="#00FFFF"
    <td width="18%"><font color="#00FFFF"><strong><font size="3"
    <td width="13%"><font color="#00FFFF"><strong><font size="3"

    <font color="#FF0000"> </font>
    <p align="left"><font color="#FFFFFF" size="5" face="Tahoma">Total
    Active Calls:
    <p align="left">&nbsp;</p>

    Set Recordset1 = Nothing


    Chris Stanley, Jul 27, 2005
  13. Well, in a way I spoke too soon. I guess I was thinking of cellpadding and
    cellspacing, but I do understand that the padding and margin of the ~cells~
    replace those! But what about something as basic AND COMMON as <table

    I think what I'm most upset about is the fact that a span can't have a width
    (strictly speaking). I hate that.

    Ray at home
    Ray Costanzo [MVP], Jul 28, 2005
  14. I don't see anything that's an attempt to change colors. ?? I think your
    first step would be to get rid of those <font> tags you have. How about:

    <style type="text/css">
    tr.priority1 { background-color: #ff0000; }
    tr.priority2 { background-color: #ffff00; }

    <table width="97%" border="0" align="center">
    <tr class="priority<%=Recordset1.Fields.Item("Priority").Value%>">
    <td width="16%"><%=(Recordset1.Fields.Item("Complaint").Value)%></td>
    <td width="13%"><%=(Recordset1.Fields.Item("First_Unit").Value)%></td>

    I know this is easier said than done, but your first step actually ought to
    be to stop using Dreamweaver for code writing. If you want to use it for
    design, that's one thing, but it shouldn't be used for ASP code, imo.

    Ray at home
    Ray Costanzo [MVP], Jul 28, 2005
  15. Chris  Stanley

    Adrienne Guest

    Gazing into my crystal ball I observed "Ray Costanzo [MVP]" <my first
    name at lane 34 dot commercial> writing in
    table {text-align:center} /*centers the table content */
    table {margin-left:auto; margin-right:auto; width:whatever} /* centers the
    ~table~ in the viewport*/
    Span is an inline element, you really wouldn't want it to have width. But,
    you can always use display:block and then set a width. There is also
    display:table-inline, etc., but these some of these are not supported by
    our favorite browser. I very rarely use span, I usually style the element.
    What I really hate is max and min width not supported by IE, position:fixed
    not supported by IE, hover pseudo class only working for the A element in
    IE, alpha transparency in PNGs not supported by IE. If I could, I would go
    to everyone's Windows computer, remove IE, and replace it with Opera or
    Adrienne, Jul 28, 2005
  16. That did it. Thanks for the help.

    Chris Stanley, Jul 28, 2005
  17. The margin one is the work-around that I would typically use, but then I
    decide that it just makes 100 times more sense to stop being so absolute and
    just use the inline align="center" attribute, as much as I hate to do it.


    I understand why the span doesn't have a width, but I disagree with it. And
    as far as the display:block, that kinda defeats the purpose of the span.
    Also, if you want to get technical, I want to say that as soon as you do a
    display:inline to a block element like a table, that will void any width
    styles in strict browsers. 8[

    I'm just going to start writing console applications. :]

    Rya at work
    Ray Costanzo [MVP], Jul 28, 2005
  18. Amen to that! Just give us hover pseudo class support for lists and I'd be
    satisfied. We could then do away with JavaScript menus altogether. Hopefully
    the powers that be are listening. :) Here's a CSS/DHTML hybrid that uses
    behaviors to add support for Internet Explorer, if anyone is interested.

    Chris Hohmann, Jul 28, 2005
  19. I understand why the span doesn't have a width, but I disagree with it.

    If it's a <div> then use a <div>. A span is contained within another
    element that has some kind of defined width. If you need to define widths
    inside that way, then as much as I hate to suggest it, I would say use a
    table instead (or the very complex kind of CSS that properly replicates a
    table, but in a much more cumbersome fashion).
    Aaron Bertrand [SQL Server MVP], Jul 28, 2005
  20. I usually do wind up using tables when I'm trying to avoid doing so. I
    can't use a DIV in place of where I want a span with a width, because I'm
    wanting an inline element with a width specified. Like:

    <style type="text/css">
    div.tabs span { width: 200px; border: 1px solid #000000; }

    <div class="tabs">
    <span>Tab 1</span>
    <span>Tab 2</span>
    <span>This is Tab #3</span>

    Since the width won't be honored in strict browsers, I do wind up throwing
    it in a table. (Perhaps this isn't the best example.) Again, though,
    there's nothing wrong with tables. It's just kinda, uh, fun to try to not
    use them.

    Ray at work
    Ray Costanzo [MVP], Jul 28, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.