PHP Table Generator is a tool that is simply used to generate HTML tables without dealing with HTML. I, personally, had to write HTML tags for tables again and again. Besides, mixing HTML with PHP is not recommended. Instead you can use MVC frameworks to be able to separate your logic from your presentation. However, if your application is simple enough you can still take advantage of PHP Table Generator. It is easy to use and in this post I will show you how to use it.
Thanks to phpDocumentor all the classes and functions are documented here. You can have a look at the documentation to get yourself familiar with PHP Table Generator.
Structure of PHP Table Generator
As you know a HTML table consists of different elements in this order:
1 2 3 4 5 6 7 8 9 10 11 |
- <table> - <caption> - <thead> - <tr> - <th> - <tfoot> - <tr> - <td> - <tbody> - <tr> - <td> |
According to the structure above there is a class for each element and each class has got some properties such as “content”, “class”, “id”, “data” and etc. So if you’re familiar with OOP you can easily use PHP Table Generator. Let’s have a look at some examples.
Examples
1. A simple table with thead
and tbody
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php require_once('../classes/tableGenerator.class.php'); /** * it's not recommended to embed css. But for the sake of demo, it's fine */ $css =<<<EOF <style> table, th, td { border: 1px solid black; } </style> EOF; echo $css; /** * first initialize the cells with content */ $cell1 = new Cell('Cell 1 Content'); $cell2 = new Cell('Cell 2 Content'); /** * initialize a row with cells * * you can also add cells later to the row using addCells() */ $row1 = new Row(array($cell1, $cell2)); /** * do the same thing to generate second row with cell3 and cell4 */ $cell3 = new Cell('Cell 3 Content'); $cell4 = new Cell('Cell 4 Content'); $row2 = new Row(array($cell3, $cell4)); /** * once more */ $cell5 = new Cell('Cell 5 Content'); $cell6 = new Cell('Cell 6 Content'); $row3 = new Row(array($cell5, $cell6)); /** * initialize the head with row1 * * again row can be added later using addRow() method. * remember that head just accepts one row. So in this case there is no need to have an array */ $head = new Head($row1); /** * initialize the body with row2 and row3 the same as head * * again rows can be added later using addRow() or addRows() */ $body = new Body(array($row2, $row3)); /** * initialize the table object */ $table = new Table(); /** * at the end add head and body to the table */ $table->addHead($head); $table->addBody($body); /** * print table. Don't forget this one! */ $table->display(); |
2. You can also add data, CSS styling, class and id to an element:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php require_once('../classes/tableGenerator.class.php'); $css =<<<EOF <style> table, th, td { border: 1px solid black; } .firstRow { background-color: lightgray; } #fifthCell { background-color: lightyellow; } </style> EOF; echo $css; $cell1 = new Cell('Cell 1 Content'); $cell1->addScope('col'); $cell2 = new Cell('Cell 2 Content'); $cell2->addScope('col'); $row1 = new Row(array($cell1, $cell2)); $row1->class = 'firstRow'; $cell3 = new Cell('Cell 3 Content'); $cell4 = new Cell('Cell 4 Content'); $cell4->style = 'background-color: lightblue;'; $row2 = new Row(array($cell3, $cell4)); $cell5 = new Cell('Cell 5 Content'); $cell5->id = 'fifthCell'; $cell6 = new Cell('Cell 6 Content'); $row3 = new Row(array($cell5, $cell6)); $head = new Head($row1); $head->addData(array('myData', 'myDataValue')); $body = new Body(array($row2, $row3)); $table = new Table(); $table->addHead($head); $table->addBody($body); $table->display(); |
As you see, CSS styling, class and id are public properties. You can set them as easy as $cell->class = 'myClass';
. However, I made data attribute a private property because an array containing data name as the first item and data value as the second item should be passed to addData()
.
3. If you need to add a cell to a specific position in a row:
1 2 |
$row2 = new Row(array($cell3)); $row2->addCell($cell2, 0); |
4. Similarly, you can specify the position for a row in table body:
1 |
$body->addRow($row2, 0); |
5. PHP Table Generator supports rowspan
and colspan
for cells as well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php require_once('../classes/tableGenerator.class.php'); $css =<<<EOF <style> table, th, td { border: 1px solid black; } .firstRow { background-color: lightgray; } #fifthCell { background-color: lightyellow; } </style> EOF; echo $css; $cell1 = new Cell('Cell 1 Content'); $cell1->addScope('col'); $cell2 = new Cell('Cell 2 Content'); $cell2->addScope('col'); $row1 = new Row(array($cell1, $cell2)); $row1->class = 'firstRow'; $cell3 = new Cell('Cell 3 Content'); $cell3->addRowspan(2); $cell4 = new Cell('Cell 4 Content'); $cell4->style = 'background-color: lightblue;'; $row2 = new Row(array($cell3, $cell4)); $cell5 = new Cell('Cell 5 Content'); $cell5->id = 'fifthCell'; $row3 = new Row(array($cell5)); $cell6 = new Cell('Cell 6 Content'); $cell6->addColspan(2); $row4 = new Row(array($cell6)); $head = new Head($row1); $head->addData(array('myData', 'myDataValue')); $body = new Body(array($row2, $row3, $row4)); $table = new Table(); $table->addHead($head); $table->addBody($body); $table->display(); |
6. Last, but not least you can have caption
and footer
in your table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?php require_once('../classes/tableGenerator.class.php'); $css =<<<EOF <style> table, th, td { border: 1px solid black; } .firstRow { background-color: lightgray; } #fifthCell { background-color: lightyellow; } </style> EOF; echo $css; $cell1 = new Cell('Cell 1 Content'); $cell1->addScope('col'); $cell2 = new Cell('Cell 2 Content'); $cell2->addScope('col'); $row1 = new Row(array($cell1, $cell2)); $row1->class = 'firstRow'; $cell3 = new Cell('Cell 3 Content'); $cell3->addRowspan(2); $cell4 = new Cell('Cell 4 Content'); $cell4->style = 'background-color: lightblue;'; $row2 = new Row(array($cell3, $cell4)); $cell5 = new Cell('Cell 5 Content'); $cell5->id = 'fifthCell'; $row3 = new Row(array($cell5)); $cell6 = new Cell('Cell 6 Content'); $cell6->addColspan(2); $row4 = new Row(array($cell6)); $cell7 = new Cell('Cell 7 Content'); $cell8 = new Cell('Cell 8 Content'); $row5 = new Row(array($cell7, $cell8)); $footer = new Footer($row5); $caption = new Caption('This is caption content'); $caption->class = 'captionClass'; $caption->addData(array('captionData', 'captionDataValue')); $head = new Head($row1); $head->addData(array('myData', 'myDataValue')); $body = new Body(array($row2, $row3, $row4)); $table = new Table(); $table->addCaption($caption); $table->addHead($head); $table->addBody($body); $table->addFooter($footer); $table->display(); |
As you’ve noticed, since head
and footer
can only have one row, you just need to pass one row to the constructor
function, whereas you should pass an array of rows to the body
.
Leave a Reply