PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Sithu Kyaw  >  PHP Console Table  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: PHP Console Table
Display data in a table of console text characters
Author: By
Last change: Add a handy method showAllBorders() to display all border lines
Allow to add horizontal border lines for each row
Output with <pre> tag when it is not running from CLI
changes according to psr-2
composer use php>=5.3, reinserted example.php
rm example.php
integrated composer + phpunit testing
Date: 2 years ago
Size: 2,195 bytes
 

Contents

Class file image Download
<?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()
;

For more information send a message to info at phpclasses dot org.