Advertisement
phpaddict

PHP Array to Text Table

Feb 9th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.69 KB | None | 0 0
  1. <?php /*
  2. // PHP Array to Text Tables (taken from http://tonylandis.com/php/php-text-tables-class/)
  3.  
  4. // Usage and Output Example
  5.  
  6. $data = array(
  7. array('company'=>'AIG', 'id'=>1, 'balance'=> '-$99,999,999,999.00'),
  8. array('company'=>'Wachovia', 'id'=>2, 'balance'=> '-$10,000,000.00'),
  9. array('company'=>'HP', 'id'=>3, 'balance'=> '$555,000.000.00'),
  10. array('company'=>'IBM', 'id'=>4, 'balance'=> '$12,000.00')
  11. );
  12.  
  13. $renderer = new ArrayToTextTable($data);
  14. $renderer->showHeaders(true);
  15. $renderer->render();
  16.  
  17. // Text Table Formatted Output of above example:
  18.  
  19. // +----------+----+---------------------+
  20. // | COMPANY  | ID |       BALANCE       |
  21. // +----------+----+---------------------+
  22. // | AIG      | 1  | -$99,999,999,999.00 |
  23. // | Wachovia | 2  | -$10,000,000.00     |
  24. // | HP       | 3  | $555,000.000.00     |
  25. // | IBM      | 4  | $12,000.00          |
  26. // +----------+----+---------------------+
  27.  
  28. // Here is the class:
  29.  
  30. /**
  31. * Array to Text Table Generation Class
  32. *
  33. * @author Tony Landis <tony@tonylandis.com>
  34. * @link http://www.tonylandis.com/
  35. * @copyright Copyright (C) 2006-2009 Tony Landis
  36. * @license http://www.opensource.org/licenses/bsd-license.php
  37. */
  38. class ArrayToTextTable
  39. {
  40.     /**
  41. * @var array The array for processing
  42. */
  43.     private $rows;
  44.  
  45.     /**
  46. * @var int The column width settings
  47. */
  48.     private $cs = array();
  49.  
  50.     /**
  51. * @var int The Row lines settings
  52. */
  53.     private $rs = array();
  54.  
  55.     /**
  56. * @var int The Column index of keys
  57. */
  58.     private $keys = array();
  59.  
  60.     /**
  61. * @var int Max Column Height (returns)
  62. */
  63.     private $mH = 2;
  64.  
  65.     /**
  66. * @var int Max Row Width (chars)
  67. */
  68.     private $mW = 30;
  69.  
  70.     private $head = false;
  71.     private $pcen = "+";
  72.     private $prow = "-";
  73.     private $pcol = "|";
  74.    
  75.    
  76.     /** Prepare array into textual format
  77. *
  78. * @param array $rows The input array
  79. * @param bool $head Show heading
  80. * @param int $maxWidth Max Column Height (returns)
  81. * @param int $maxHeight Max Row Width (chars)
  82. */
  83.     public function ArrayToTextTable($rows)
  84.     {
  85.         $this->rows =& $rows;
  86.         $this->cs=array();
  87.         $this->rs=array();
  88.  
  89.         if(!$xc = count($this->rows)) return false;
  90.         $this->keys = array_keys($this->rows[0]);
  91.         $columns = count($this->keys);
  92.        
  93.         for($x=0; $x<$xc; $x++)
  94.             for($y=0; $y<$columns; $y++)
  95.                 $this->setMax($x, $y, $this->rows[$x][$this->keys[$y]]);
  96.     }
  97.    
  98.     /**
  99. * Show the headers using the key values of the array for the titles
  100. *
  101. * @param bool $bool
  102. */
  103.     public function showHeaders($bool)
  104.     {
  105.        if($bool) $this->setHeading();
  106.     }
  107.    
  108.     /**
  109. * Set the maximum width (number of characters) per column before truncating
  110. *
  111. * @param int $maxWidth
  112. */
  113.     public function setMaxWidth($maxWidth)
  114.     {
  115.         $this->mW = (int) $maxWidth;
  116.     }
  117.    
  118.     /**
  119. * Set the maximum height (number of lines) per row before truncating
  120. *
  121. * @param int $maxHeight
  122. */
  123.     public function setMaxHeight($maxHeight)
  124.     {
  125.         $this->mH = (int) $maxHeight;
  126.     }
  127.    
  128.     /**
  129. * Prints the data to a text table
  130. *
  131. * @param bool $return Set to 'true' to return text rather than printing
  132. * @return mixed
  133. */
  134.     public function render($return=false)
  135.     {
  136.         if($return) ob_start(null, 0, true);
  137.  
  138.         $this->printLine();
  139.         $this->printHeading();
  140.        
  141.         $rc = count($this->rows);
  142.         for($i=0; $i<$rc; $i++) $this->printRow($i);
  143.        
  144.         $this->printLine(false);
  145.  
  146.         if($return) {
  147.             $contents = ob_get_contents();
  148.             ob_end_clean();
  149.             return $contents;
  150.         }
  151.     }
  152.  
  153.     private function setHeading()
  154.     {
  155.         $data = array();
  156.         foreach($this->keys as $colKey => $value)
  157.         {
  158.             $this->setMax(false, $colKey, $value);
  159.             $data[$colKey] = strtoupper($value);
  160.         }
  161.         if(!is_array($data)) return false;
  162.         $this->head = $data;
  163.     }
  164.  
  165.     private function printLine($nl=true)
  166.     {
  167.         print $this->pcen;
  168.         foreach($this->cs as $key => $val)
  169.             print $this->prow .
  170.                 str_pad('', $val, $this->prow, STR_PAD_RIGHT) .
  171.                 $this->prow .
  172.                 $this->pcen;
  173.         if($nl) print "\n";
  174.     }
  175.  
  176.     private function printHeading()
  177.     {
  178.         if(!is_array($this->head)) return false;
  179.  
  180.         print $this->pcol;
  181.         foreach($this->cs as $key => $val)
  182.             print ' '.
  183.                 str_pad($this->head[$key], $val, ' ', STR_PAD_BOTH) .
  184.                 ' ' .
  185.                 $this->pcol;
  186.  
  187.         print "\n";
  188.         $this->printLine();
  189.     }
  190.  
  191.     private function printRow($rowKey)
  192.     {
  193.         // loop through each line
  194.         for($line=1; $line <= $this->rs[$rowKey]; $line++)
  195.         {
  196.             print $this->pcol;
  197.             for($colKey=0; $colKey < count($this->keys); $colKey++)
  198.             {
  199.                 print " ";
  200.                 print str_pad(substr($this->rows[$rowKey][$this->keys[$colKey]], ($this->mW * ($line-1)), $this->mW), $this->cs[$colKey], ' ', STR_PAD_RIGHT);
  201.                 print " " . $this->pcol;
  202.             }
  203.             print "\n";
  204.         }
  205.     }
  206.  
  207.     private function setMax($rowKey, $colKey, &$colVal)
  208.     {
  209.         $w = mb_strlen($colVal);
  210.         $h = 1;
  211.         if($w > $this->mW)
  212.         {
  213.             $h = ceil($w % $this->mW);
  214.             if($h > $this->mH) $h=$this->mH;
  215.             $w = $this->mW;
  216.         }
  217.  
  218.         if(!isset($this->cs[$colKey]) || $this->cs[$colKey] < $w)
  219.             $this->cs[$colKey] = $w;
  220.  
  221.         if($rowKey !== false && (!isset($this->rs[$rowKey]) || $this->rs[$rowKey] < $h))
  222.             $this->rs[$rowKey] = $h;
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement