class GuaTable { var $colWidths = array(); public $headings = null; var $rows = array(); function setHeaders($headings) { $this->headings = $headings; $this->updateColWidths($headings); } function updateColWidths($colVals) { $limit = count($colVals); for($colIdx=0;$colIdx<$limit;$colIdx++) { $this->colWidths[$colIdx] = max(strlen($colVals[$colIdx]), $this->colWidths[$colIdx]); } } function addRow($row) { $this->rows[] = $row; $this->updateColWidths($row); } function padCols($colVals, $underline=false) { $str = ""; $line = $underline ? "\n" : ""; $limit = count($colVals); for($colIdx=0;$colIdx<$limit;$colIdx++) { $width = $this->colWidths[$colIdx] + 2; $str .= str_pad($colVals[$colIdx], $width); if ($underline) $line .= str_pad("", $width,"-"); } return $str.$line; } function getTable() { $str = $this->padCols($this->headings, true); foreach($this->rows as $row) { $str .= "\n". $this->padCols($row); } return $str; } }