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.
<?php
$sql  = 'SELECT * FROM `table` WHERE `column` = \'something\'';
$qry = mysql_query($sql) or die(mysql_error());

// Cell colors for odd rows
$c1  = array('#00CCCC', '#33CC00', '#CC0000', '#CCCC00', '#0000CC', '#FF6600', '#CC00CC', '#00CC99');

// Cell Colors for even rows
$c2  = array('#66FFFF', '#66FF00', '#FF3300', '#FFFF33', '#0033FF', '#FF9933', '#FF33CC', '#00FF99');

// Row Counter
$cnt  = 0;

$ret = '<table>';
while($row = mysql_fetch_assoc($qry))
{
  // Cell counter
  $cnt2 = 0;
  $ret  .= '<tr>';
  foreach($row as $key=>$val)
   {
    $ret .= '<td bgcolor="';

    if($cnt % 2)
      {
        $ret .= $c1[$cnt2];
      }
    else
      {
        $ret .= $c2[$cnt2];
      }

    $ret .= '">'.$val.'</td>';
    ++$cnt2;
   }
  ++$cnt;
  $ret .= '</tr>'."\n";
}
$ret .= '</table>';

echo $ret;
?> 
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS