Zend Certified Engineer

phpguru.org

Free PHP, Javascript and C# code

Console Table class

A class for building console/ASCII based tables (like the ones the MySQL client produces). Has Append/Prepend/Insert methods and supports table headers and footers. Automatically handles setting the correct widths for columns.

Example

ConsoleTable table = new ConsoleTable();

table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});
table.AppendRow(new string[] {"foo", "bar", "jello"});

table.SetHeaders(new string[] {"First Column", "Second Column", "Third Column"});
table.SetFooters(new string[] {"Yabba"});

table.InsertRow(new string[] {"", "ferfr", "frf        r"}, 7);
table.PrependRow(new string[] {});

System.Console.Write(table.ToString());

Produces...

+--------------+---------------+--------------+
| First Column | Second Column | Third Column |
+--------------+---------------+--------------+
|              |               |              |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
| foo          | bar           | jello        |
|              |               |              |
|              |               |              |
|              |               |              |
|              | ferfr         | frf        r |
+--------------+---------------+--------------+
| Yabba        |               |              |
+--------------+---------------+--------------+

Download now

Link to me

If you use any of the code on this site (and if you don't I guess) or it makes your life easier, I'd appreciate a link - http://www.phpguru.org. Thanks.

Last modified: 16:27 29th December 2006