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

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 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;
?> 

classify() for alternating rows, columns, etc.

I often want different rows in a table to alternate in color, and I do this by assigning each row a class name and styling it with CSS. This is a simple helper method designed to return a class name based on the given row count.

It is also convenient for me to assign a class to table cells based on the type of content they hold. For example, if I have a cell with a float value in it, I want to display it with a monospace font, whereas if I have a cell with a string in it, I want to display it in a serif font.

# Determines the CSS class based on either the count given
# (returns 'even' or 'odd' as the CSS class name) or the class
# given (returns the string version of the class, lowercased,
# as the CSS class name)
def classify( count_or_class, include_class_text = true )
  if count_or_class.class == Fixnum
    value = ( count_or_class % 2 == 0 ? 'even' : 'odd' )
  else
    value = count_or_class.to_s.downcase
  end

  if include_class_text
    'class="' << value << '"'
  else
    value
  end
end


Example usage with alternating 'even'/'odd' row class names:
<table>
<% count = 0 %>
<% @collection.each do |value| %>
  <tr <%= classify( count ) %>>
    <td><%=h value %></td>
  </tr>
  <% count += 1 %>
<% end %>
</table>


Example usage with data type class names:
<tr>
<% @columns.each do |column| %>
  <% data = row.send( column.name ) %>
  <td <%= classify( data.class ) %>>
    <%=h data %>
  </td>
<% end %>
</tr>

Alternating table row colours the Rails way

There are several ways of doing this but it's best to use functionality built into Rails:

<tr class="<%= cycle("even", "odd") %>">
... use item ...
</tr>

API reference:
http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000423
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS