Text Tables Generator

Text Tables Generator

Generates a plain text table, which can be copied or writen into any text file, And in some cases rendered to a browser output

__construct($row_size, $column_size, $padding) : [Object]

 Constructs the Text Table blueprint
Paramters Type Required Descritpion
$row_size int true The number of row(s) the table will contain.
default: void
$column_size int true The number of column(s) the table will contain.
default: void
$padding int false The padding between each string in all specified column.
default: 1
<?php

    $row_size    = 3;
    $column_size = 2;
    $pad_size    = 2;

    $table = new TextTable($row_size, $column_size, $pad_size);

    //Draws a empty table with 3 row, 2 columns and pads each column content twice

config($config) : [void]

 A simple method for altering the default Text Table configuration.
Paramters Type Required Descritpion
$config array true Array of configuration options.

Keys:

type [html|file]: default: html

This option is provided for those who wants to write the rendered table to a file.

border[true|false]: default: false

Prevents left border assignment to an empty column

padding[any single character]: default: ' '

Unlike the pad length used in construct which repeats a pad character to a specific length and side(left,right or middle), this is used to set the character that will be repeated withen a specified column. Example if padding = '+', pad Lenght = 2, and column content is 'FooBar' The blue print of this table will look like |++FooBar++|

line[<br />|<p />]: default: <br />

Browser new line(does not apply to writing into file)

<?php

//outputing to browser

$table->config(array(
    'border'   => 1,      //allow right border for empty columns

    'padding'  => '_',    //default: space(' ')

    'line'     => "<p />" //default: <br />

));

//writing to file

$table->config(array(
    'type'     => 'file',
    'border'   => 1,      //allow right border for empty columns

    'padding'  => ' ',    //default: space

));

put($row_and_olumn, $column_content): [Object]

 Adds defined content into a specific column.
Paramters Type Required Descritpion
$row_and_olumn string true The row and column number(seperated by a comma(,)) where content will be added.
default: void
$column_content int true The content that will be added to the specified row and column.
default: void
<?php
    $table->put('1,1', 'FooBar');
    //Assigns 'FooBar' to Row 1 Column 1

    
     $table->put('5,3', 'Bar');
    //Assigns 'Bar' to Row 5 Column 3

align($position): [Object]

 Aligns column content to a certain position.
Paramters Type Required Descritpion
$position string true Indicates the postion the closest column content will be aligned to.

Optional Parameters

'L': Aligns closest column content to left
'R': Aligns closest column content to right

default: BOTH
<?php

    $table->put('1,1', 'FooBar')->align('L');
    //Aligns 'FooBar' to the left of Row 1 Column 1

    
     $table->put('5,3', 'Bar')->align('R');
    //Aligns 'Bar' to the right of Row 5 Column 3

    
    $table->put('2,4', 'Foo');
    //Aligns 'Foo' to the middle of Row 2 Column 4

render(): [string]

 Builds blueprint


<?php

    $table->render();

Fair Usage

Basic usage of Text Table
<?php  

//using configuration

$table = new TextTable(2, 2, 2);

echo $table->put('1,1', 'Foo')
           ->put('1,2', 'Bar')
           ->put('2,1', 'FooBar')
           ->put('2,2', 'BarFoo')
           ->render();

The above code Outputs:


+----------+----------+
|   Foo    |   Bar    |
+----------+----------+
|  FooBar  |  BarFoo  |
+----------+----------+

Fair Usage

Using the align method
<?php  
/*
* Aligning 'FooBar' to the right of row 2 column 1;
*          'BarFoo' to the left of row 2 column 2 
*           And every other to center
*             
*/
$table = new TextTable(2, 2, 2);

echo $table->put('1,1', 'Foo')
           ->put('1,2', 'Bar')
           ->put('2,1', 'FooBar')->align('R')
           ->put('2,2', 'BarFoo')->align('L')
           ->render();

The above code Outputs:


+----------+----------+
|   Foo    |   Bar    |
+----------+----------+
|    FooBar|BarFoo    |
+----------+----------+

Fair Usage

Using the config method
<?php

//without border assignment  

$table = new TextTable(1, 2, 2);
echo $table->put('1,1', 'Foo')
           ->put('1,2', '')
           ->render();
		   

//with border assignment  		   

$table = new TextTable(1, 2, 2);
echo $table->config(['border' => true])
           ->put('1,1', 'Foo')
           ->put('1,2', '')
           ->render();

The above code Outputs:


+-------+----+
|  Foo       |
+-------+----+

+-------+-----+
|  Foo  |     |
+-------+-----+

Fair Usage

Writing to a file
<?php

//creates table with 2 rows, 3 columns and twice column content padding

$table = new TextTable(2, 3, 2);
$table->config(array(
    'type'   => 'file',
    'border' => true
));
$table->put('1,1', 'Foo')
      ->put('1,3', 'Bar')
      ->put('2,1', 'FooBar')
      ->put('2,3', 'BarFoo');
	  
file_put_contents('text.log', $table->render());

text.log content:


+----------+-----+----------+
|   Foo    |     |   Bar    |
+----------+-----+----------+
|  FooBar  |     |  BarFoo  |
+----------+-----+----------+

Subscribe to our Newsletter for latest news.

© ghostff 2014 - All rights reserved.