Hi,

I am extracting results from a Mysql database and showing them in a table.

Everyday when my script runs, it extracts the current days results which could be 1 row or 4 or 5 rows.

What I want to be able to do is alternate the row color every other day or by date such that all rows for a given day have the same color and the rows for the next day have the alternate color.

thanks for any suggestions?
sg

    data setup

    # original dates, used to instantiate DateTime objects
    $ds = array(	DateTime::createFromFormat('Y-m-d', '2011-07-30'),
    				DateTime::createFromFormat('Y-m-d', '2011-07-30'),
    				DateTime::createFromFormat('Y-m-d', '2011-07-31'),
    				DateTime::createFromFormat('Y-m-d', '2011-08-01')
    );
    # change dates into days, represented as "Mon", "Tue" etc
    foreach($ds as &$d)
    {
    	$d = $d->format('D');
    }
    

    stylesheet

    # create css style rules based on the above
    .Mon { color: #FFF; }
    .Tue { color: #F00; }
    .WED { color: #0F0; }
    

    output

    # use the class attribute to make the css styles apply to each element as needed
    foreach ($ds as $d)
    {
    	printf('<div class="%s">%s</div>', $d, $rowData);
    }
    

      You could also keep track of the last seen day/date (useful if some might be missing) and if it differs switch colors. See the snippet (not completed or tested)

      function switchColor() {
         GLOBAL $colors
         if( $colors['current'] == $colors['b'] )
            $colors['current'] = $colors['a'];
         else
            $colors['current'] = $colors['b'];
      }
      
      $prevdate = '';
      $colors = array(
                 'current' => '',
                 'a' => '#FFF',
                 'b' => '#000'
            );
      while( $row = $result->fetch_assoc() ) {
         if( $prevdate != $row['date'] )
           switchColor();
      
         echo '<span style="color:'. $colors['current'] .';">'. $row['date'] .' - '. $row['data'] .'</span><br />';
      
         $prevdate = $row['date'];
      }
      
        Write a Reply...