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:

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.


1. A simple table with thead and tbody:


2. You can also add data, CSS styling, class and id to an element:


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:


4. Similarly, you can specify the position for a row in table body:


5. PHP Table Generator supports rowspan and colspan for cells as well:


6. Last, but not least you can have caption and footer in your table:


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.

Where to download?