<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rows code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 19:42:30 GMT</pubDate>
    <description>DZone Snippets: rows code</description>
    <item>
      <title>Alternating table colors for rows and columns.</title>
      <link>http://snippets.dzone.com/posts/show/4410</link>
      <description>This code will provide 8 colors for cells and set of 8 lighter colors for alternating rows.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$sql  = 'SELECT * FROM `table` WHERE `column` = \'something\'';&lt;br /&gt;$qry = mysql_query($sql) or die(mysql_error());&lt;br /&gt;&lt;br /&gt;// Cell colors for odd rows&lt;br /&gt;$c1  = array('#00CCCC', '#33CC00', '#CC0000', '#CCCC00', '#0000CC', '#FF6600', '#CC00CC', '#00CC99');&lt;br /&gt;&lt;br /&gt;// Cell Colors for even rows&lt;br /&gt;$c2  = array('#66FFFF', '#66FF00', '#FF3300', '#FFFF33', '#0033FF', '#FF9933', '#FF33CC', '#00FF99');&lt;br /&gt;&lt;br /&gt;// Row Counter&lt;br /&gt;$cnt  = 0;&lt;br /&gt;&lt;br /&gt;$ret = '&lt;table&gt;';&lt;br /&gt;while($row = mysql_fetch_assoc($qry))&lt;br /&gt;{&lt;br /&gt;  // Cell counter&lt;br /&gt;  $cnt2 = 0;&lt;br /&gt;  $ret  .= '&lt;tr&gt;';&lt;br /&gt;  foreach($row as $key=&gt;$val)&lt;br /&gt;   {&lt;br /&gt;    $ret .= '&lt;td bgcolor="';&lt;br /&gt;&lt;br /&gt;    if($cnt % 2)&lt;br /&gt;      {&lt;br /&gt;        $ret .= $c1[$cnt2];&lt;br /&gt;      }&lt;br /&gt;    else&lt;br /&gt;      {&lt;br /&gt;        $ret .= $c2[$cnt2];&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;    $ret .= '"&gt;'.$val.'&lt;/td&gt;';&lt;br /&gt;    ++$cnt2;&lt;br /&gt;   }&lt;br /&gt;  ++$cnt;&lt;br /&gt;  $ret .= '&lt;/tr&gt;'."\n";&lt;br /&gt;}&lt;br /&gt;$ret .= '&lt;/table&gt;';&lt;br /&gt;&lt;br /&gt;echo $ret;&lt;br /&gt;?&gt; &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 10 Aug 2007 16:59:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4410</guid>
      <author>ellisgl (Kenneth Ellis McCall)</author>
    </item>
    <item>
      <title>classify() for alternating rows, columns, etc.</title>
      <link>http://snippets.dzone.com/posts/show/2569</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;# Determines the CSS class based on either the count given&lt;br /&gt;# (returns 'even' or 'odd' as the CSS class name) or the class&lt;br /&gt;# given (returns the string version of the class, lowercased,&lt;br /&gt;# as the CSS class name)&lt;br /&gt;def classify( count_or_class, include_class_text = true )&lt;br /&gt;  if count_or_class.class == Fixnum&lt;br /&gt;    value = ( count_or_class % 2 == 0 ? 'even' : 'odd' )&lt;br /&gt;  else&lt;br /&gt;    value = count_or_class.to_s.downcase&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  if include_class_text&lt;br /&gt;    'class="' &lt;&lt; value &lt;&lt; '"'&lt;br /&gt;  else&lt;br /&gt;    value&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example usage with alternating 'even'/'odd' row class names:&lt;br /&gt;&lt;code&gt;&lt;table&gt;&lt;br /&gt;&lt;% count = 0 %&gt;&lt;br /&gt;&lt;% @collection.each do |value| %&gt;&lt;br /&gt;  &lt;tr &lt;%= classify( count ) %&gt;&gt;&lt;br /&gt;    &lt;td&gt;&lt;%=h value %&gt;&lt;/td&gt;&lt;br /&gt;  &lt;/tr&gt;&lt;br /&gt;  &lt;% count += 1 %&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/table&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example usage with data type class names:&lt;br /&gt;&lt;code&gt;&lt;tr&gt;&lt;br /&gt;&lt;% @columns.each do |column| %&gt;&lt;br /&gt;  &lt;% data = row.send( column.name ) %&gt;&lt;br /&gt;  &lt;td &lt;%= classify( data.class ) %&gt;&gt;&lt;br /&gt;    &lt;%=h data %&gt;&lt;br /&gt;  &lt;/td&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/tr&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Sep 2006 21:00:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2569</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>Alternating table row colours the Rails way</title>
      <link>http://snippets.dzone.com/posts/show/924</link>
      <description>There are several ways of doing this but it's best to use functionality built into Rails:&lt;br /&gt;&lt;br /&gt;  &lt;tr class="&lt;%= cycle("even", "odd") %&gt;"&gt;&lt;br /&gt;    ... use item ...&lt;br /&gt;  &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;API reference:&lt;br /&gt;http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000423</description>
      <pubDate>Thu, 01 Dec 2005 05:38:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/924</guid>
      <author>hammed (Hammed)</author>
    </item>
  </channel>
</rss>
