Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Kenneth Ellis McCall http://geeklab.net/ellis/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Alternating table colors for rows and columns.

This code will provide 8 colors for cells and set of 8 lighter colors for alternating rows.
   1  
   2  <?php
   3  $sql  = 'SELECT * FROM `table` WHERE `column` = \'something\'';
   4  $qry = mysql_query($sql) or die(mysql_error());
   5  
   6  // Cell colors for odd rows
   7  $c1  = array('#00CCCC', '#33CC00', '#CC0000', '#CCCC00', '#0000CC', '#FF6600', '#CC00CC', '#00CC99');
   8  
   9  // Cell Colors for even rows
  10  $c2  = array('#66FFFF', '#66FF00', '#FF3300', '#FFFF33', '#0033FF', '#FF9933', '#FF33CC', '#00FF99');
  11  
  12  // Row Counter
  13  $cnt  = 0;
  14  
  15  $ret = '<table>';
  16  while($row = mysql_fetch_assoc($qry))
  17  {
  18    // Cell counter
  19    $cnt2 = 0;
  20    $ret  .= '<tr>';
  21    foreach($row as $key=>$val)
  22     {
  23      $ret .= '<td bgcolor="';
  24  
  25      if($cnt % 2)
  26        {
  27          $ret .= $c1[$cnt2];
  28        }
  29      else
  30        {
  31          $ret .= $c2[$cnt2];
  32        }
  33  
  34      $ret .= '">'.$val.'</td>';
  35      ++$cnt2;
  36     }
  37    ++$cnt;
  38    $ret .= '</tr>'."\n";
  39  }
  40  $ret .= '</table>';
  41  
  42  echo $ret;
  43  ?> 
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS