Create HTML tables that are easy to visualize and see what is going on.
Create a <table>
.
<table>
attributes.
Table <caption>
.
Create a <thead>
row.
<tr>
attributes.
<th>
attributes for all of this rows cells.
Create a <tfoot>
row.
<tr>
attributes.
<td>
attributes for all of this rows cells.
Create a <tbody>
row.
<tr>
attributes.
<td>
attributes for all of this rows cells.
Create a <th>
or <td>
cell.
The cell's attributes.
The (optional) cell's value.
Closes any remaining open tags.
Add the following to your composer.json
file.
{
"require": {
"bootpress/table": "^1.0"
}
}
<?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:
One | Two | Three |
Or in other words:
<table>
<tbody>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</tbody>
</table>
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.
$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.
$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;
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>
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.
$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();
Five | Six |
|
<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>