<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: table code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 17:37:39 GMT</pubDate>
    <description>DZone Snippets: table code</description>
    <item>
      <title>PHP Dynamic Checkbox Table Creator (data retrieved from MySQL DB)</title>
      <link>http://snippets.dzone.com/posts/show/5450</link>
      <description>Hi All.&lt;br /&gt;This is a function for Dynamically Create a Checbox Table retrieving information for from a MySQL Database.&lt;br /&gt;As it is quite commented, it's also good for learning how this things work :)&lt;br /&gt;Hope you find it useful.&lt;br /&gt;Feedback is welcome.&lt;br /&gt;Cheers&lt;br /&gt;Dan&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function dynamic_checkbox_table ($sql_str, $col_label, $col_name, $val_checked="S", $cant_cols_tbl=3){&lt;br /&gt;/*&lt;br /&gt;	by Daniel Neumann&lt;br /&gt;	this script creates dynamically permite a table containing checkboxes &lt;br /&gt;	getting the data for the checkboxes from a MySQL DB&lt;br /&gt;	$sql_str, SQL select string to retrieve data from DB (see example in last comment line)&lt;br /&gt;	$col_label, DB column that has values for the checkbox label &lt;br /&gt;	$col_name, DB column that has values for the checkbox name&lt;br /&gt;	$val_checked="S", value when checked (value="" attribute) it uses the same value for all of them. If you whish to use a dynamic value from a DB, you should comment the line (it&#180;s explained next to the code in the middle of the function) and de-comment the other line (check the code,. you'll understand what I mean). Also, you should use this parameter to specify the column name for the values&lt;br /&gt;	$cant_cols_tbl=3, quantity of columns for the table, it defaults to 3&lt;br /&gt;	usage example: dynamic_checkbox_table("SELECT * FROM keywords", "Keyword", "ID_Keywrd");&lt;br /&gt;*/&lt;br /&gt;	&lt;br /&gt;	//connect DB and run query&lt;br /&gt;	$db="MyDB";&lt;br /&gt;	$db_user="MyUser";&lt;br /&gt;	$pass="MyPass";&lt;br /&gt;	$host="localhost";&lt;br /&gt;	@mysql_connect($host,$db_user,$pass);&lt;br /&gt;	@mysql_select_db($db) or die ("cannot connect to DB");&lt;br /&gt;	$q_resultado = mysql_query($sql_str);&lt;br /&gt;	mysql_close();&lt;br /&gt;	if (mysql_num_rows($q_resultado)==0) exit("no rows returned");&lt;br /&gt;	&lt;br /&gt;	$next_row = mysql_fetch_array($q_resultado); //fetch first row&lt;br /&gt;	&lt;br /&gt;	$output = "&lt;table  border=\"1\"&gt;\n"; //open table tag&lt;br /&gt;	do {&lt;br /&gt;		$output .= "&lt;tr&gt;\n"; //open row tag&lt;br /&gt;		for ($i=1 ; $i &lt;= $cant_cols_tbl ; $i++ ){ //loops as many times as $cant_cols_tbl&lt;br /&gt;			$row=$next_row; //assign $row, next row will be checking next one, that avoids starting a new row when it's gonna be empty&lt;br /&gt;			$output .= "&lt;td&gt;"; //open TD tag&lt;br /&gt;			$output .= (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$val_checked.'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;//			echo (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$row[$val_checked].'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should de-comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;			$next_row = mysql_fetch_array($q_resultado); //retrieve next row&lt;br /&gt;			$output .= "&lt;/td&gt;\n"; //close TD&lt;br /&gt;		} //close for loop&lt;br /&gt;		$output .= "&lt;/tr&gt;\n"; //close row&lt;br /&gt;	} while ($next_row); //close do-while (and checks if there's another row)&lt;br /&gt;	$output .= "&lt;/table&gt;\n"; //close table&lt;br /&gt;	return $output; &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 May 2008 12:09:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5450</guid>
      <author>dneuma (Daniel Neumann)</author>
    </item>
    <item>
      <title>SQL  -&gt; Check Column exists in table, if not, add</title>
      <link>http://snippets.dzone.com/posts/show/4663</link>
      <description>// Check to see if column exists and then create if not&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS&lt;br /&gt;WHERE TABLE_NAME = &#8216;TEST&#8217; AND COLUMN_NAME = &#8216;TEST_DATE&#8217;)&lt;br /&gt;BEGIN&lt;br /&gt;   ALTER TABLE TEST ADD TEST_DATE DATETIME&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 17 Oct 2007 08:58:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4663</guid>
      <author>dubby (Dave)</author>
    </item>
    <item>
      <title>How to draw your own table header... (with borders)</title>
      <link>http://snippets.dzone.com/posts/show/3931</link>
      <description>// It's a bit  hacky at the beginning, but nstableheadercell wasn't exactly making things easy for me&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;- (void)_drawThemeContents:(NSRect)cellFrame highlighted:(BOOL)highlighted inView:(NSTableHeaderView *)view;&lt;br /&gt;{&lt;br /&gt;	int index = [view columnAtPoint:NSMakePoint(cellFrame.origin.x + 1,cellFrame.origin.y + 1)];&lt;br /&gt;	NSRect headerRect;&lt;br /&gt;	if(index != -1)&lt;br /&gt;		headerRect = [view headerRectOfColumn:index];&lt;br /&gt;	else&lt;br /&gt;		headerRect = NSZeroRect;&lt;br /&gt;	&lt;br /&gt;	[headerImage drawInRect:cellFrame&lt;br /&gt;				   fromRect:NSZeroRect&lt;br /&gt;				  operation:NSCompositeSourceOver&lt;br /&gt;				   fraction:1.0];&lt;br /&gt;	&lt;br /&gt;	[[NSColor colorWithCalibratedRed:207.0/255.0&lt;br /&gt;							   green:207.0/255.0&lt;br /&gt;								blue:207.0/255.0&lt;br /&gt;							   alpha:1.0] set];&lt;br /&gt;	NSRectFill(NSMakeRect(headerRect.origin.x, headerRect.origin.y + 1, headerRect.size.width, headerRect.size.height - 2));&lt;br /&gt;	[headerImage drawInRect:NSMakeRect(headerRect.origin.x, headerRect.origin.y, headerRect.size.width - 1, headerRect.size.height)&lt;br /&gt;				   fromRect:NSZeroRect&lt;br /&gt;				  operation:NSCompositeSourceOver&lt;br /&gt;				   fraction:1.0];&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 28 Apr 2007 19:48:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3931</guid>
      <author>aptiva (Fj&#195;&#182;lnir &#195;?sgeirsson)</author>
    </item>
    <item>
      <title>Helper for quicly creating standard tables</title>
      <link>http://snippets.dzone.com/posts/show/3623</link>
      <description>I often want to display an array of objects as a table on a page, and I end up doing the the same things over and over again. I wrote this helper method to speed things up:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def table(collection, headers, options = {}, &amp;proc)&lt;br /&gt;  options.reverse_merge!({&lt;br /&gt;    :placeholder  =&gt; 'Nothing to display',&lt;br /&gt;    :caption      =&gt; nil,&lt;br /&gt;    :summary      =&gt; nil,&lt;br /&gt;    :footer       =&gt; ''&lt;br /&gt;  })&lt;br /&gt;  placeholder_unless collection.any?, options[:placeholder] do&lt;br /&gt;    summary = options[:summary] || "A list of #{collection.first.class.to_s.pluralize}"&lt;br /&gt;    output = "&lt;table summary=\"#{summary}\"&gt;\n"&lt;br /&gt;    output &lt;&lt; content_tag('caption', options[:caption]) if options[:caption]&lt;br /&gt;    output &lt;&lt; "\t&lt;caption&gt;#{options[:caption]}&lt;/caption&gt;\n" if options[:caption]&lt;br /&gt;    output &lt;&lt; content_tag('thead', content_tag('tr', headers.collect { |h| "\n\t" + content_tag('th', h) }))&lt;br /&gt;    output &lt;&lt; "&lt;tfoot&gt;&lt;tr&gt;" + content_tag('th', options[:footer], :colspan =&gt; headers.size) + "&lt;/tr&gt;&lt;/tfoot&gt;\n" if options[:footer]&lt;br /&gt;    output &lt;&lt; "&lt;tbody&gt;\n"&lt;br /&gt;    concat(output, proc.binding)&lt;br /&gt;    collection.each do |row|&lt;br /&gt;      proc.call(row, cycle('odd', 'even'))&lt;br /&gt;    end&lt;br /&gt;    concat("&lt;/tbody&gt;\n&lt;/table&gt;\n", proc.binding)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Writing...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% table(@posts, %w{ID title}) do |post, klass| -%&gt;&lt;br /&gt;    &lt;tr class="&lt;%= klass %&gt;"&gt;&lt;br /&gt;      &lt;td&gt;&lt;%= post.id&lt;/td&gt;&lt;br /&gt;      &lt;td&gt;&lt;%= post.title &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;% end -%&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;results in...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;table summary="A list of posts"&gt;&lt;br /&gt;  &lt;thead&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;th&gt;ID&lt;/th&gt;&lt;br /&gt;      &lt;th&gt;Title&lt;/th&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;  &lt;/thead&gt;&lt;br /&gt;  &lt;tfoot&gt;&lt;tr&gt;&lt;td colspan="2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tfoot&gt;&lt;br /&gt;  &lt;tbody&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;1&lt;/td&gt;&lt;br /&gt;      &lt;td&gt;My first post&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;  &lt;/tbody&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Or, when the collection is an empty array (collection.any? returns false), a &lt;a href="http://snippets.dzone.com/posts/show/2929"&gt;placeholder message&lt;/a&gt; is displayed:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;p class="placeholder"&gt;Nothing to display&lt;/p&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;So you pass in your collection and an array of strings as your table headers as the first two arguments, and the third is a hash of options you can use to set the contents of the table's summary-attribute, the caption and footer-elements and the placeholder. &lt;br /&gt;&lt;br /&gt;The summary attribute defaults to "A list of [objects]", where 'objects' is derived from the class name of the collection. &lt;br /&gt;&lt;br /&gt;The function finally takes a block for every element in the collection, yeilding it the element and either 'odd' or 'even' so you can use CSS-classes to apply a zebra stripes effect.</description>
      <pubDate>Sat, 03 Mar 2007 20:52:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3623</guid>
      <author>arjanvandergaag (Arjan van der Gaag)</author>
    </item>
    <item>
      <title>Model from Table Name</title>
      <link>http://snippets.dzone.com/posts/show/3020</link>
      <description>Get the Rails model based on the table name.  Assumes standard table name conventions.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def model_for_table(table_name)&lt;br /&gt;  table_name.classify.constantize&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 20 Nov 2006 06:52:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3020</guid>
      <author>dcmanges (Dan Manges)</author>
    </item>
    <item>
      <title>Check whether table exists</title>
      <link>http://snippets.dzone.com/posts/show/2687</link>
      <description>Checks whether mysql table exists.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;SHOW TABLES LIKE 'table_name'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 23 Sep 2006 18:02:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2687</guid>
      <author>stancell (Algimantas Stancelis)</author>
    </item>
    <item>
      <title>ZebraStripes</title>
      <link>http://snippets.dzone.com/posts/show/2380</link>
      <description>&lt;code&gt;&lt;br /&gt;/*&lt;br /&gt; * This script requires:&lt;br /&gt; *   1. Prototype version 1.5 or greater&lt;br /&gt; *     - Homepage: http://prototype.conio.net/&lt;br /&gt; *     - Download: http://script.aculo.us/downloads&lt;br /&gt; *   2. DomReady addon for Prototype&lt;br /&gt; *     - Homepage: http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype&lt;br /&gt; *     - Download: http://www.vivabit.com/code/domready/domready.js&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * I needed something capable of not just unobtrusively running when a page&lt;br /&gt; * was loaded but also easily being called at my whim as a part of an Ajax&lt;br /&gt; * transaction.&lt;br /&gt; */&lt;br /&gt;var ZebraStripes = {&lt;br /&gt;    /*&lt;br /&gt;     * Call this function when you want something striped.  It will figure out&lt;br /&gt;     * how to stripe the element.&lt;br /&gt;     */&lt;br /&gt;    stripe: function(el) {&lt;br /&gt;        el = $(el);&lt;br /&gt;        switch (el.tagName) {&lt;br /&gt;            case "TABLE":&lt;br /&gt;                this._stripeTable(el);&lt;br /&gt;                break;&lt;br /&gt;            case "OL":&lt;br /&gt;            case "UL":&lt;br /&gt;                this._stripeNormalList(el);&lt;br /&gt;                break;&lt;br /&gt;            case "DL":&lt;br /&gt;                this._stripeDefinitionList(el);&lt;br /&gt;                break;&lt;br /&gt;        }&lt;br /&gt;    },&lt;br /&gt;    /***************************************************************************&lt;br /&gt;     * Everything below here is psuedo-private&lt;br /&gt;     **************************************************************************/&lt;br /&gt;    /*&lt;br /&gt;     * This class name will be applied to the odd numbered elements&lt;br /&gt;     */&lt;br /&gt;    _altClassName: "alt",&lt;br /&gt;    /*&lt;br /&gt;     * This property persists the data that tells the object whether&lt;br /&gt;     * to stripe (_isEven == false) or unstripe (_isEven == true) an element&lt;br /&gt;     */&lt;br /&gt;    _isEven: true,&lt;br /&gt;    /*&lt;br /&gt;     * Cycles the _isEven property of this object between true and false&lt;br /&gt;     */&lt;br /&gt;    _cycle: function() {&lt;br /&gt;        this._isEven = ! this._isEven;&lt;br /&gt;    },&lt;br /&gt;    /*&lt;br /&gt;     * As a part of the Ajax-friendliness, it is important that we remove the&lt;br /&gt;     * alt class from elements as well as add it.&lt;br /&gt;     */&lt;br /&gt;    _stripeElement: function(el) {&lt;br /&gt;        el = $(el);&lt;br /&gt;        if (this._isEven) {&lt;br /&gt;            el.removeClassName(this._altClassName);&lt;br /&gt;        } else {&lt;br /&gt;            el.addClassName(this._altClassName);&lt;br /&gt;        }&lt;br /&gt;    },&lt;br /&gt;    /*&lt;br /&gt;     * This works to stripe the child nodes of TABLE, TBODY, OL and UL elements.&lt;br /&gt;     */&lt;br /&gt;    _stripeElements: function(els) {&lt;br /&gt;        els = $(els);&lt;br /&gt;        if (els.length == 0) {&lt;br /&gt;            return&lt;br /&gt;        }&lt;br /&gt;        var parent = els[0].parentNode;&lt;br /&gt;        this._isEven = true;&lt;br /&gt;        for (var i = 0; i &lt; els.length; i++ ) {&lt;br /&gt;            if ((parent == els[i].parentNode) &amp;&amp; (els[i].visible)) {&lt;br /&gt;                this._stripeElement(els[i]);&lt;br /&gt;                this._cycle();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    },&lt;br /&gt;    /*&lt;br /&gt;     * TBODY is not necessary, but I recommend it.  I debated about striping&lt;br /&gt;     * THEAD and TFOOT, but chose not to.  Might be added later.&lt;br /&gt;     */&lt;br /&gt;    _stripeTable: function(table) {&lt;br /&gt;        table = $(table);&lt;br /&gt;        if (table.getElementsByTagName("tbody")) {&lt;br /&gt;            var tableBodies = table.getElementsByTagName("tbody");&lt;br /&gt;            for (var i = 0; i &lt; tableBodies.length; i++) {&lt;br /&gt;                this._stripeElements(tableBodies[i].getElementsByTagName("tr"));&lt;br /&gt;            }&lt;br /&gt;        } else {&lt;br /&gt;            this._stripeElements(table.getElementsByTagName("tr"));&lt;br /&gt;        }&lt;br /&gt;    },&lt;br /&gt;    /*&lt;br /&gt;     * This stripes OL and UL since they both have LI and only LI child nodes.&lt;br /&gt;     */&lt;br /&gt;    _stripeNormalList: function(list) {&lt;br /&gt;        list = $(list);&lt;br /&gt;        this._stripeElements(list.getElementsByTagName("li"));&lt;br /&gt;    },&lt;br /&gt;    /*&lt;br /&gt;     * I have seen other function out there that can stripe DL, but they all&lt;br /&gt;     * assumed that each DT would have only one DD following it.  That is not&lt;br /&gt;     * always the case, and not the case in the project that spawned this&lt;br /&gt;     * javascript.&lt;br /&gt;     */&lt;br /&gt;    _stripeDefinitionList: function(list) {&lt;br /&gt;        list = $(list);&lt;br /&gt;        Element.cleanWhitespace(list);&lt;br /&gt;        var children = list.childNodes;&lt;br /&gt;        var previousDt;&lt;br /&gt;        for (var i = 0; i &lt; children.length; i++) {&lt;br /&gt;            switch (children[i].tagName) {&lt;br /&gt;                case "DT":&lt;br /&gt;                    if (children[i].visible) {&lt;br /&gt;                        this._stripeElement(children[i]);&lt;br /&gt;                        this._cycle();&lt;br /&gt;                    }&lt;br /&gt;                    previousDt = children[i];&lt;br /&gt;                    break;&lt;br /&gt;                case "DD":&lt;br /&gt;                    if (previousDt.visible) {&lt;br /&gt;                        this._stripeElement(children[i]);&lt;br /&gt;                    }&lt;br /&gt;                    break;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * This function will go ahead and stripe all the elligible elements on the&lt;br /&gt; * page when the page first loads.&lt;br /&gt; */&lt;br /&gt;function initZebraStripes() {&lt;br /&gt;    var toStripe = $$("dl.striped")&lt;br /&gt;        .concat($$("ol.striped"))&lt;br /&gt;        .concat($$("table.striped"))&lt;br /&gt;        .concat($$("ul.striped"));&lt;br /&gt;&lt;br /&gt;    toStripe.each(&lt;br /&gt;        function(el) {&lt;br /&gt;            ZebraStripes.stripe(el);&lt;br /&gt;        }&lt;br /&gt;    );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Event.onDOMReady(initZebraStripes);&lt;br /&gt;// Or you could substitute a different loader:&lt;br /&gt;//Event.observe(window, 'load', initZebraStripes)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Aug 2006 17:32:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2380</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>pretty tables for rails models</title>
      <link>http://snippets.dzone.com/posts/show/2176</link>
      <description>From http://www.rubyinside.com/columnized-text-datasets-in-rails-71.html:&lt;br /&gt;Inspired by: http://blog.caboo.se/articles/2006/06/10/pretty-tables-for-ruby-objects&lt;br /&gt;&lt;br /&gt;It gives a MySQL-command-line-client style textual view of data stored in your Rails database. The syntax worked like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Something.find(:all, :conditions =&gt; &#226;&#8364;&#732;whatever&#226;&#8364;&#732;).pretty_print&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;&lt;br /&gt;  protected&lt;br /&gt;&lt;br /&gt;    def columnized_row(fields, sized)&lt;br /&gt;      r = []&lt;br /&gt;      fields.each_with_index do |f, i|&lt;br /&gt;        r &lt;&lt; sprintf(&#226;&#8364;?%0-#{sized[i]}s&#226;&#8364;&#339;, f.to_s.gsub(/\n|\r/, &#226;&#8364;&#732;&#226;&#8364;&#8482;).slice(0, sized[i]))&lt;br /&gt;      end&lt;br /&gt;      r.join(&#226;&#8364;&#8482; | &#226;&#8364;&#732;)&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;  public&lt;br /&gt;&lt;br /&gt;  def columnized(options = {})&lt;br /&gt;    sized = {}&lt;br /&gt;    self.each do |row|&lt;br /&gt;      row.attributes.values.each_with_index do |value, i|&lt;br /&gt;        sized[i] = [sized[i].to_i, row.attributes.keys[i].length, value.to_s.length].max&lt;br /&gt;        sized[i] = [options[:max_width], sized[i].to_i].min if options[:max_width]&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    table = []&lt;br /&gt;    table &lt;&lt; header = columnized_row(self.first.attributes.keys, sized)&lt;br /&gt;    table &lt;&lt; header.gsub(/./, &#226;&#8364;&#732;-&#226;&#8364;&#732;)&lt;br /&gt;    self.each { |row| table &lt;&lt; columnized_row(row.attributes.values, sized) }&lt;br /&gt;    table.join(&#226;&#8364;?\n&#226;&#8364;&#339;)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;  def columnized(options = {})&lt;br /&gt;    [*self].columnized(options)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To use:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; puts Post.find(:all).columnized(:max_width =&gt; 10)&lt;br /&gt;updated_at | title      | private | url | thumb      | metadata | movie      | id  | views | content    | user_id | created_at&lt;br /&gt;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&lt;br /&gt;Wed May 31 | tetwer     | 0       |     |            |          |            | 909 | 0     | video:xyzz | 1       | Wed May 31&lt;br /&gt;Wed May 31 | bbbb       | 0       |     |            |          |            | 1   | 15    | // descrip | 1       | Tue May 23&lt;br /&gt;Wed May 31 | cxzcxzx    | 0       |     |            |          |            | 906 | 19    | // descrip | 1       | Tue May 23&lt;br /&gt;Wed May 31 | jklklkl;   | 0       |     |            |          |            | 907 | 35    | // descrip | 1       | Tue May 23&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If you want to use it with your project, put the code into lib/columnized.rb, use require &#226;&#8364;&#732;columnized&#226;&#8364;&#8482;, and you&#226;&#8364;&#8482;re ready to roll. Unlike courtenay&#226;&#8364;&#8482;s version, mine only supports max_width, but I didn&#226;&#8364;&#8482;t consider changing the column separator too important.</description>
      <pubDate>Tue, 13 Jun 2006 01:56:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2176</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>go from model to associated table name and back</title>
      <link>http://snippets.dzone.com/posts/show/2135</link>
      <description>Given a table object, it returns the related string object; e.g. SubAttribute =&gt; 'sub-attribute'.  Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def stringify_table( table, replace_char = '-', pluralize = false )&lt;br /&gt;  string = table.to_s.gsub( /([A-Za-z])([A-Z])/, '\1' &lt;&lt; replace_char.to_s &lt;&lt; '\2' )&lt;br /&gt;  string = string.pluralize if pluralize&lt;br /&gt;  string&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Given a string akin to the name of a table, it returns the related table object; e.g. 'sub_attributes' =&gt; SubAttribute.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def tablify_string( string )&lt;br /&gt;  eval( string.to_s.gsub( /_id/, '' ).singularize.split( '_' ).collect { |word| word.capitalize }.join )&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 03 Jun 2006 00:50:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2135</guid>
      <author>moneypenny ()</author>
    </item>
    <item>
      <title>PHP - InversoMoltiplicativo</title>
      <link>http://snippets.dzone.com/posts/show/2022</link>
      <description>// Calcolo degli inversi moltiplicativi&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;	&lt;head&gt;&lt;br /&gt;		&lt;title&gt;Calcolo degli inversi moltiplicativi&lt;/title&gt;&lt;br /&gt;	&lt;/head&gt;&lt;br /&gt;&lt;br /&gt;	&lt;body&gt;&lt;br /&gt;		&lt;center&gt;&lt;br /&gt;		&lt;b&gt;Cancolo dell'Inverso Moltiplicativo&lt;/b&gt;&lt;br /&gt;		&lt;br&gt;&lt;hr&gt;&lt;br&gt;&lt;br /&gt;		&lt;table border="1" align="center"&gt;&lt;br /&gt;		&lt;?php&lt;br /&gt;			for($i=0; $i&lt;26; $i++)&lt;br /&gt;			{&lt;br /&gt;		?&gt;&lt;br /&gt;			&lt;tr&gt;&lt;br /&gt;		&lt;?php&lt;br /&gt;				for($j=0; $j&lt;26; $j++)&lt;br /&gt;				{&lt;br /&gt;					if($i==0) echo "&lt;td bgcolor=\"yellow\" align=\"center\"&gt;" . $j . "&lt;/td&gt;";&lt;br /&gt;					else&lt;br /&gt;					if($j==0) echo "&lt;td bgcolor=\"yellow\" align=\"center\"&gt;" . $i . "&lt;/td&gt;";&lt;br /&gt;					else&lt;br /&gt;					{&lt;br /&gt;						echo "&lt;td align=\"center\"&gt;" . $i*$j . "&lt;/td&gt;";&lt;br /&gt;						$matrice[$i-1][$j-1] = $i*$j;&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;		?&gt;&lt;br /&gt;			&lt;/tr&gt;&lt;br /&gt;		&lt;?php&lt;br /&gt;			}&lt;br /&gt;&lt;br /&gt;			for($i=0; $i&lt;25; $i++)&lt;br /&gt;				for($j=0; $j&lt;25; $j++)&lt;br /&gt;					$matrice[$i][$j] = $matrice[$i][$j]%26;&lt;br /&gt;		?&gt;&lt;br /&gt;		&lt;/table&gt;&lt;br /&gt;		&lt;br&gt;&lt;hr&gt;&lt;br /&gt;		&lt;b&gt;Tabella dell'Inverso Moltiplicativo&lt;/b&gt;&lt;br /&gt;		&lt;br&gt;&lt;hr&gt;&lt;br&gt;&lt;br /&gt;		&lt;table border="1" align="center"&gt;&lt;br /&gt;		&lt;?php&lt;br /&gt;			for($i=0; $i&lt;26; $i++)&lt;br /&gt;			{&lt;br /&gt;				echo "&lt;tr&gt;";&lt;br /&gt;				&lt;br /&gt;				for($j=0; $j&lt;26; $j++)&lt;br /&gt;				{&lt;br /&gt;					if($i==0) echo "&lt;td bgcolor=\"yellow\" align=\"center\"&gt;" . $j . "&lt;/td&gt;";&lt;br /&gt;					else&lt;br /&gt;					if($j==0) echo "&lt;td bgcolor=\"yellow\" align=\"center\"&gt;" . $i . "&lt;/td&gt;";&lt;br /&gt;					else&lt;br /&gt;					{&lt;br /&gt;						echo "&lt;td align=\"center\" " . (($matrice[$i-1][$j-1] == 1)?"bgcolor=\"red\"":"") . "\"&gt;" . $matrice[$i-1][$j-1] . "&lt;/td&gt;";&lt;br /&gt;						&lt;br /&gt;						if($matrice[$i-1][$j-1] == 1)&lt;br /&gt;							$np[] = $i;&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;&lt;br /&gt;				echo "&lt;/tr&gt;";&lt;br /&gt;			}&lt;br /&gt;		?&gt;&lt;br /&gt;		&lt;/table&gt;&lt;br /&gt;		&lt;br&gt;&lt;br /&gt;		&lt;i&gt;&lt;b&gt;I numeri primi con 26 sono:&lt;br /&gt;		&lt;?php&lt;br /&gt;			for($i=0; $i&lt;count($np); $i++)&lt;br /&gt;				echo $np[$i] . " ";&lt;br /&gt;		?&gt;&lt;br /&gt;		&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;		&lt;br&gt;&lt;br&gt;&lt;br /&gt;		Si prendono le coppie di numeri che contengono il numero 1 al loro interno, esempio (1,1), (3,9)......&lt;br /&gt;		&lt;/center&gt;&lt;br /&gt;	&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 15 May 2006 02:02:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2022</guid>
      <author>whitetiger ()</author>
    </item>
  </channel>
</rss>
