PHP Classes

PHP Console Table: Display data in a table of console text characters

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 177 All time: 8,749 This week: 673Up
Version License PHP version Categories
console-table 1.0.2MIT/X Consortium ...5PHP 5, Text processing, Console
Description 

Author

This class can display data in a table of console text characters.

It can add rows, headers and column cells to the definition of the table.

The class can automatically calculate the width of the table cells and output it using only text characters with characters like + - and | as dividers between cells.

Picture of Sithu Kyaw
Name: Sithu Kyaw <contact>
Classes: 2 packages by
Country: Myanmar Myanmar
Age: 41
All time rank: 11922 in Myanmar Myanmar
Week rank: 216 Up2 in Myanmar Myanmar Equal

Example

<?php

require 'src/LucidFrame/Console/ConsoleTable.php';

use
LucidFrame\Console\ConsoleTable;

function
_pr($string)
{
    if (
PHP_SAPI == 'cli') {
        echo
"\n";
        echo
'### '.$string.' ###';
        echo
"\n\n";
    } else {
        echo
'<h2>'.$string.'</h2>';
    }
}

_pr('Bordered Table (Default)');

$table = new ConsoleTable();
$table
   
->addHeader('Language')
    ->
addHeader('Year')
    ->
addRow()
        ->
addColumn('PHP')
        ->
addColumn(1994)
    ->
addRow()
        ->
addColumn('C++')
        ->
addColumn(1983)
    ->
addRow()
        ->
addColumn('C')
        ->
addColumn(1970)
    ->
display()
;

_pr('Bordered Table with Horizontal Lines');

$table = new ConsoleTable();
$table
   
->setHeaders(array('Language', 'Year'))
    ->
addRow(array('PHP', 1994))
    ->
addBorderLine()
    ->
addRow(array('C++', 1983))
    ->
addBorderLine()
    ->
addRow(array('C', 1970))
    ->
display()
;

_pr('Bordered Table with Horizontal Lines using showAllBorders()');

$table = new ConsoleTable();
$table
   
->setHeaders(array('Language', 'Year'))
    ->
addRow(array('PHP', 1994))
    ->
addRow(array('C++', 1983))
    ->
addRow(array('C', 1970))
    ->
showAllBorders()
    ->
display()
;

_pr('Bordered Table with Padding Width 2');

$table = new ConsoleTable();
$table
   
->setHeaders(array('Language', 'Year'))
    ->
addRow(array('PHP', 1994))
    ->
addRow(array('C++', 1983))
    ->
addRow(array('C', 1970))
    ->
setPadding(2)
    ->
display()
;

_pr('Bordered Table with Left Margin Width 4');

$table = new ConsoleTable();
$table
   
->setHeaders(array('Language', 'Year'))
    ->
addRow(array('PHP', 1994))
    ->
addRow(array('C++', 1983))
    ->
addRow(array('C', 1970))
    ->
setIndent(4)
    ->
display()
;

_pr('Non-bordered Table with Header');

$table = new ConsoleTable();
$table
   
->setHeaders(array('Language', 'Year'))
    ->
addRow(array('PHP', 1994))
    ->
addRow(array('C++', 1983))
    ->
addRow(array('C', 1970))
    ->
hideBorder()
    ->
display()
;

_pr('Non-bordered Table without Header');

$table = new ConsoleTable();
$table
   
->addRow(array('PHP', 1994))
    ->
addRow(array('C++', 1983))
    ->
addRow(array('C', 1970))
    ->
hideBorder()
    ->
display()
;


Details

PHP ConsoleTable

ConsoleTabe makes you easy to build console style tables. It helps you to display tabular data in terminal/shell. This is a component of PHPLucidFrame.

License: MIT

Composer Installation

composer require phplucidframe/console-table

Example 1: Bordered Table (Default)

require 'src/LucidFrame/Console/ConsoleTable.php';

$table = new LucidFrame\Console\ConsoleTable();
$table
    ->addHeader('Language')
    ->addHeader('Year')
    ->addRow()
        ->addColumn('PHP')
        ->addColumn(1994)
    ->addRow()
        ->addColumn('C++')
        ->addColumn(1983)
    ->addRow()
        ->addColumn('C')
        ->addColumn(1970)
    ->display()
;

You can also print the table using getTable method such as echo $table->getTable();

Output:

+----------+------+
| Language | Year |
+----------+------+
| PHP      | 1994 |
| C++      | 1983 |
| C        | 1970 |
+----------+------+

Example 2: Bordered Table with Padding Width 2

You can also use setHeaders() and addRow with Arrays.

require 'src/LucidFrame/Console/ConsoleTable.php';

$table = new LucidFrame\Console\ConsoleTable();
$table
    ->setHeaders(array('Language', 'Year'))
    ->addRow(array('PHP', 1994))
    ->addRow(array('C++', 1983))
    ->addRow(array('C', 1970))
    ->setPadding(2)
    ->display()
;

Output:

+------------+--------+
|  Language  |  Year  |
+------------+--------+
|  PHP       |  1994  |
|  C++       |  1983  |
|  C         |  1970  |
+------------+--------+

Example 3: Bordered Table with Left Margin Width 4

require 'src/LucidFrame/Console/ConsoleTable.php';

$table = new LucidFrame\Console\ConsoleTable();
$table
    ->setHeaders(array('Language', 'Year'))
    ->addRow(array('PHP', 1994))
    ->addRow(array('C++', 1983))
    ->addRow(array('C', 1970))
    ->setIndent(4)
    ->display()
;

Output:

    +----------+------+
    | Language | Year |
    +----------+------+
    | PHP      | 1994 |
    | C++      | 1983 |
    | C        | 1970 |
    +----------+------+

Example 4: Non-bordered Table with Header

require 'src/LucidFrame/Console/ConsoleTable.php';

$table = new LucidFrame\Console\ConsoleTable();
$table
    ->setHeaders(array('Language', 'Year'))
    ->addRow(array('PHP', 1994))
    ->addRow(array('C++', 1983))
    ->addRow(array('C', 1970))
    ->hideBorder()
    ->display()
;

Output:

 Language  Year
----------------
 PHP       1994
 C++       1983
 C         1970

Example 5: Non-bordered Table without Header

require 'src/LucidFrame/Console/ConsoleTable.php';

$table = new LucidFrame\Console\ConsoleTable();
$table
    ->addRow(array('PHP', 1994))
    ->addRow(array('C++', 1983))
    ->addRow(array('C', 1970))
    ->hideBorder()
    ->display()
;

Output:

 PHP  1994
 C++  1983
 C    1970

Example 6: Table with all borders

require 'src/LucidFrame/Console/ConsoleTable.php';

$table = new LucidFrame\Console\ConsoleTable();
$table
    ->setHeaders(array('Language', 'Year'))
    ->addRow(array('PHP', 1994))
    ->addRow(array('C++', 1983))
    ->addRow(array('C', 1970))
    ->showAllBorders()
    ->display()
;

Alternatively, you can use addBorderLine() for each row.

$table
    ->setHeaders(array('Language', 'Year'))
    ->addRow(array('PHP', 1994))
    ->addBorderLine()
    ->addRow(array('C++', 1983))
    ->addBorderLine()
    ->addRow(array('C', 1970))
    ->display()
;

Output

+----------+------+
| Language | Year |
+----------+------+
| PHP      | 1994 |
+----------+------+
| C++      | 1983 |
+----------+------+
| C        | 1970 |
+----------+------+

Test

If you have PHPUnit installed in your machine, you can run test at your project root.

composer install
phpunit tests

If you don't have PHPUnit, you can simply run this in your terminal.

php example.php

  Files folder image Files (9)  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file bootstrap.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file example.php Example Example usage
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. README

  Files folder image Files (9)  /  src  
File Role Description
Files folder imageLucidFrame (1 directory)

  Files folder image Files (9)  /  src  /  LucidFrame  
File Role Description
Files folder imageConsole (1 file)

  Files folder image Files (9)  /  src  /  LucidFrame  /  Console  
File Role Description
  Plain text file ConsoleTable.php Class Class source

  Files folder image Files (9)  /  tests  
File Role Description
Files folder imageLucidFrame (1 directory)

  Files folder image Files (9)  /  tests  /  LucidFrame  
File Role Description
Files folder imageConsole (1 file)

  Files folder image Files (9)  /  tests  /  LucidFrame  /  Console  
File Role Description
  Plain text file ConsoleTableTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:177
This week:0
All time:8,749
This week:673Up