use BootPress\Table\Component as Table;
![License MIT][badge-license]
![PHP 7 Supported][badge-php]
![Code Climate][badge-code-climate]
, (*1)
Create HTML tables that are easy to visualize and see what is going on., (*2)
Installation
Add the following to your composer.json
file., (*3)
``` bash
{
"require": {
"bootpress/table": "^1.0"
}
}, (*4)
## A Simple Example
```php
<?php
use BootPress\Table\Component as Table;
$table = new Table;
$html = $table->open();
$html .= $table->row();
$html .= $table->cell('', 'One');
$html .= $table->cell('', 'Two');
$html .= $table->cell('', 'Three');
$html .= $table->close();
echo $html;
That will give you three cells in a row:, (*5)
Or in other words:, (*6)
<table>
<tbody>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</tbody>
</table>
Colspan and Rowspan
Notice that we use a syntax for attributes that keeps it compact, yet readable. Basically, every attribute is separated by a '|' (single pipe), and we drop the quotes., (*7)
$html = $table->open('border=1|class=special');
$html .= $table->row();
$html .= $table->cell('rowspan=2', 'Two Rows');
$html .= $table->cell('', 'One');
$html .= $table->cell('', 'Two');
$html .= $table->row();
$html .= $table->cell('colspan=2', 'Buckle my shoe');
$html .= $table->close();
echo $html;
Two Rows |
One |
Two |
Buckle my shoe |
<table border="1" class="special">
<tbody>
<tr>
<td rowspan="2">Two Rows</td>
<td>One</td>
<td>Two</td>
</tr><tr>
<td colspan="2">Buckle my shoe</td>
</tr>
</tbody>
</table>
It is not necessary to pass a cells content to the method. It will still be wrapped appropriately., (*8)
$html = $table->open('border=1', 'Caption');
$html .= $table->head();
$html .= $table->cell('colspan=2') . 'Header';
$html .= $table->row();
$html .= $table->cell() . 'Three';
$html .= $table->cell() . 'Four';
$html .= $table->foot();
$html .= $table->cell('colspan=2') . 'Shut the door';
$html .= $table->close();
echo $html;
Caption
Header |
Three |
Four |
Shut the door |
<table border="1">
<caption>Caption</caption>
<thead>
<tr>
<th colspan="2">Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Three</td>
<td>Four</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2"> Shut the door</td>
</tr>
</tfoot>
</table>
Nested Table
When you $table->close()
, everything is reset and you can make another $table->open()
without any problems. For nesting tables though, you'll need to create another instance of the class., (*9)
$t1 = new Table;
$t2 = new Table;
$html = $t1->open();
$html .= $t1->row();
$html .= $t1->cell('', 'Five');
$html .= $t1->cell() . 'Six';
$html .= $t1->cell();
$html .= $t2->open('border=1');
$html .= $t2->row();
$html .= $t2->cell('', 'Pick');
$html .= $t2->cell('', 'Up');
$html .= $t2->cell('', 'Sticks');
$html .= $t2->close();
$html .= $t1->close();
<table>
<tbody>
<tr>
<td>Five</td>
<td>Six</td>
<td>
<table border="1">
<tbody>
<tr>
<td>Pick</td>
<td>Up</td>
<td>Sticks</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
License
The MIT License (MIT). Please see License File for more information., (*10)