Alternating Background Color of DB resultset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clintw
    New Member
    • Feb 2007
    • 10

    #1

    Alternating Background Color of DB resultset

    Hi all,

    I've been doing my head in trying to get this to work, but I keep running into a snag. Basically I have a database table with a list of events for the year, with fields for Month, Event Description, FullDate (used for sorting). When I input the result set into a HTML table, this works fine, alternating backgrounds colors for the individual rows is no problem - my problem comes in trying to alternate the background colors by Month (grouping common rows with one color), ie. each alternate month has different color to the one before. Only using two colors to differentiate.

    Any suggestions how to get the logic right?

    Thanks
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Keep the month in a variable and compare this with every new month in the result. If the new month is equal to previous one don't change the color otherwise do and save the new month in the variable to compare in the next result.

    Comment

    • clintw
      New Member
      • Feb 2007
      • 10

      #3
      Originally posted by mwasif
      Keep the month in a variable and compare this with every new month in the result. If the new month is equal to previous one don't change the color otherwise do and save the new month in the variable to compare in the next result.
      This was my understanding of the problem, except in practice it didnt work because it doesn't take the colors into account. I ended up the first entry of every month after the first one being in the alternate color. Theres a step I'm neglecting and I just can't think straight anymore.

      Code:
      // $row[0] holds the name of the month
      
      if(empty($lastmonth)) $lastmonth = $row[0];
      
      if ($lastmonth == $row[0]) 
      {
             $color = "#D8DBFE";
      }
      else 
      {
             $lastmonth = $row[0];
             $color = "#A6ACFD";
      }

      Comment

      • clintw
        New Member
        • Feb 2007
        • 10

        #4
        I actually got my own problem solved, but the actual code that got the job done is just too hideous to post on here. Guess I'll spend rest of my time trying to clean it up. Thanks for the reply mwasif. I thought more would have tried as well, but I figure this was a tricky one to solve.

        Comment

        Working...