diff --git a/outputtxt.php b/outputtxt.php index e0eb2be..7bfb2d8 100644 --- a/outputtxt.php +++ b/outputtxt.php @@ -7,6 +7,9 @@ require_once ("output.php"); /** Display in Text the datas provided */ class outputtxt extends output { + /** Display an array on table format (TRUE) or tree format (FALSE) */ + public $table = TRUE; + /** Display in Text the datas provided @param mixed $data The data to be displayed */ public function out ($data) @@ -26,7 +29,92 @@ class outputtxt extends output elseif (is_integer ($data)) echo "$data\n"; elseif (is_array ($data)) - echo substr (print_r ($data, TRUE), 8, -3)."\n"; + { + if ($this->table === true) + { + // TABLE FORMAT + // Look at sizes of each field + $sizes = array (); + foreach ($data as $line) + { + if (! is_array ($line)) + { + // 1 dimension array : force in tree format + $this->table = false; + break; + } + foreach ($line as $key=>$val) + { + if (is_array ($val)) + { + // 3 dimensions array : force in tree format + $this->table = false; + break 2; + } + if (! isset ($sizes[$key])) + $sizes[$key] = strlen ($val); + else + $sizes[$key] = max ($sizes[$key], strlen ($val)); + } + } + + if ($this->table === true) + { + // Display the table if it is not a tree format + // Prepare the border + $line = reset ($data); + $border = ""; + $border .= "+"; + $col = 0; + foreach ($line as $key=>$val) + { + if ($col > 0) + $border .= "+"; + $border .= str_repeat ("-", 2+$sizes[$key]); + $col ++; + } + $border .= "+\n"; + echo "$border"; + echo "| "; + // Display titles + $col = 0; + foreach ($line as $key=>$val) + { + if ($col > 0) + echo " | "; + $start = round (($sizes[$key]-strlen ($key))/2); + echo str_repeat (" ", $start); + echo $key; + echo str_repeat (" ", $sizes[$key]-$start-strlen ($key)); + $col ++; + } + echo " |\n"; + echo "$border"; + + // Display datas + foreach ($data as $line) + { + echo "| "; + $col = 0; + foreach ($line as $key=>$val) + { + if ($col > 0) + echo " | "; + echo sprintf ("%".$sizes[$key]."s", $val); + $col ++; + } + echo " |\n"; + } + echo "$border"; + } + } + + if ($this->table === false) + { + // TREE FORMAT + echo substr (print_r ($data, TRUE), 8, -3)."\n"; + } + } elseif ($data === NULL) echo "NULL\n"; else