Re: How to format CLI tabular data?
| From: | Jim Lucas | Date: | Tue, 09 Oct 2007 19:30:40 +0000 |
| Subject: | Re: How to format CLI tabular data? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-262992@lists.php.net to get a copy of this message | ||
tedd wrote:
At 12:05 AM -0700 10/9/07, Jim Lucas wrote:I wouldn't use that to display tabular data in a browser. I will attach what I use to display in a browser. I updated the comments, I think everything is correct. Let me know what you think <?php /** * This class is for creating an HTML table * * Example usage * $table = new table(); * $table->addHeader( array('ID', 'FirstName', 'LastName'), 'header'); * $table->addData( array(1, 'Jim', 'L'), 'rowA'); * $table->addData( array(2, 'Tim', 'H'), 'rowB'); * $table->addData( array(1, 'Jim', 'B'), 'rowA'); * $table->addData( array(2, 'Chris', 'C'), 'rowB'); * echo $table->get('table_one', 'some_class'); * * or * * $data[] = array('id' => 1, 'first_name' => 'John', 'last_name' => 'Doe'); * $data[] = array('id' => 2, 'first_name' => 'Jonny', 'last_name' => 'Buck'); * $data[] = array('id' => 3, 'first_name' => 'James', 'last_name' => 'Smith'); * $table = new table(); * $table->dump($dataArray); * echo $table->to_s(); */ class table { /** * Purpose is to contain table data for display purposes later * * @type array() */ var $table = array(); /** * Constructor. * * @return Class Object */ function __construct() { return $this; } /** * Constructor, php4 style * * @return Class Object */ function table() { return $this->__construct(); } /** * Creates a table header row * * This calls to the addRow method to create a list of cells with the $data submitted * It creates <th>...</th> cells * * @param Your 1-dimensional array of data * @param Value you would like to set id for the <tr id="..."> tag * * @return Class Object */ function addHeader($data, $row_id=null) { $this->addRow($data, 'th', $row_id); return $this; } /** * Creates a table data row * * This calls to the addRow method to create a list of cells with the $data submitted * It creates <td>...</td> cells * * @param Your 1-dimensional array of data * @param Value you would like to set id for the <tr id="..."> tag * * @return Class Object */ function addData($data, $row_id=null) { $this->addRow($data, 'td', $row_id); return $this; } /** * Creates a table row, when called * * @param Data to be displayed * @param Which type of cells are we creating? td (default) or th * @param ID value to be appended to the <tr id='row_...'> tag * * @return Class Object */ function addRow($data, $type='td', $row_id=null) { $this->table[] = "<tr id='row_{$row_id}'>"; foreach ( $data AS $id => $cell ) { $this->table[] = "<{$type} class='col_{$id}'>". htmlspecialchars($cell)."</{$type}>"; } $this->table[] = '</tr>'; return $this; } /** * Just build a table from an array full of data * * @param Your 2-dimensional array of data * * @return Class Object */ function dump($data=array()) { for ( $x=0; $x<count($data); $x++ ) { if ( ( $x % 20) == 0 ) { $this->addHeader(array_keys($data[$x])); } $this->addData($data[$x]); } return $this; } /** * Generate and return the built table * * @param Specify the ID value for the table id attribute * @param Specify a class for the table class attribute * * @return if data found, return formatted table * if no data found, return 'No data to display' message */ function get($id='', $class='') { if ( count($this->table) ) { return "\n<table id='{$id}' class='{$class}' cellpadding='0' cellspacing='0'>\n". join("\n", $this->table)."\n</table>\n"; } return 'No data to display'; } /** * Generate and return the built table * * The purpose of this method is to be forwards compatible. * Most of my new CMS tries to detect if an object has a to_s() method * * @return results from the get() method */ function to_s() { return $this->get(); } } -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William ShakespeareDaevid Vincent wrote:-snip- code Jim: Thanks for the code. I've altered it to display a table in a browser: http://webbytedd.com/bbb/table/ Code is shown there. Cheers, tedd Wasn't he asking for something that was to display in the console?Does PHP have any tools like this? I'd prefer not to pull in some PEAR package or other bloat, but I'll take what I can get if it actually works well. I was hoping there are some tips/tricks to show simple tabular data: -snip- notice basic things like alignment, length of data (as in 'tabs' won't work), etc.Try this out