SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    Fontana, Ca
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database Row Color

    When the database results are displayed on a page I would like the rows to apear in a different color, every other color being different, not unlike the rows appeared in this forum. Can some help me accomplish this?

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,294
    Mentioned
    123 Post(s)
    Tagged
    1 Thread(s)
    This is more a coding question than a database question. It's a simple matter of assigning the proper class based on the row being processed. What language are you talking about? If it's asp, then you would have something like this(assuming you have two stylesheet class records with different color assigned to them):

    Code:
    <%
    strSQL = "SELECT Field1, Field2 FROM Tablename"
    rs.Open strSQL, myConn
    if rs.EOF and rs.BOF then
       recCount = -1
    else
    	arrRecSet = rs.getrows(-1)
    	recCount  = ubound(arrRecSet,2)
    end if
     
    for i = 0 to reccount
    	if i mod 2 = 0 then
    	  Response.Write "<tr class=""evenrow"">"
       else
    	  Response.Write "<tr class=""oddrow"">"
       end if
       :
       :	 ' rest of table coding goes here....
    next
    %>
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  3. #3
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's the logic i use

    Code:
      loop over query
    	 use_class = "mainrow"
    	 if (query.CurrentRow % 2 == 0)
    		use_class = "altrow"
    	 output (" <tr class=\"" + use_class + "\">")
       end loop
    query.CurrentRow is basically where you're at in the loop. (i.e. if you're on record number 3, then CurrentRow % 2 = 1)

    my css might look like this:

    Code:
       .mainrow  { background-color: #FFFFFF; }
       .altrow	{ background-color: #CCCCCC; }
    that's quick, easy, and has always worked. (If you want to reverse the row colors, you can say if (currentrow % 2 == 1)).

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    <table>
    <CFOUTPUT QUERY="queryname">
    <tr bgcolor="##<CFIF (queryname.CurrentRow MOD 2) IS 1>F4FFF4<CFELSE>F4F4FF</CFIF>">
    <td>#queryname.foo#</td></tr>
    </CFOUTPUT>
    </table>

    see http://www.macromedia.com/support/ul..._colors04.html
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does anyone have a php equivalent for row coloring?

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    oops, i'm in the wrong forum, i gave a coldfusion answer, my apologies

  7. #7
    SitePoint Member
    Join Date
    May 2003
    Location
    Fontana, Ca
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry if I posted this in the wrong area.

    A client aked if I could accomplish this in her site, I usually don't fool with databases but I said I would look into it. I failed to recognize that the site was built originally in FrontPage 2002, using a database wizard? I do not have much familiarity with FP, I normally use DW. Would the recommendations listed above still work? Language is ASP.

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bvarvel
    does anyone have a php equivalent for row coloring?
    http://www.sitepointforums.com/showt...threadid=76466

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  9. #9
    SitePoint Guru
    Join Date
    Sep 1999
    Location
    Singapore
    Posts
    854
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Xavier1
    Sorry if I posted this in the wrong area.

    A client aked if I could accomplish this in her site, I usually don't fool with databases but I said I would look into it. I failed to recognize that the site was built originally in FrontPage 2002, using a database wizard? I do not have much familiarity with FP, I normally use DW. Would the recommendations listed above still work? Language is ASP.
    It doesn't matter which editor was used to build the pages.

    Dave's code will work fine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •