ucscode / table-generator
Lightweight PHP library that simplifies the process of creating tables with minimal code
Requires
- php: >=8.1
- ucscode/easy-paginator: ^2.0
- ucscode/uss-element: ^3.5
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10
README
Table Generator is a powerful yet flexible PHP library that allows you to create and manipulate HTML tables dynamically with minimal effort. Whether you need to generate tables from MySQL query results, CSV data, associative arrays, or any custom format, this package provides a structured and extensible way to do so.
Features
- Adapter System — Convert various data structures (MySQL result, CSV, JSON, Doctrine, etc.) into tables.
- Pagination Support — Easily paginate large datasets using ucscode/easy-paginator.
- Middleware Customization — Modify table rows dynamically (e.g., add action buttons, hide sensitive data).
- Modular Components — Each table element (thead, tbody, tr, td, etc.) is an instance, allowing for full customization.
- Meta — Store temporary values without exposing them in HTML.
- DOM Manipulation with UssElement — Directly set attributes, classes, or IDs on table elements.
Installation
Install via Composer:
composer require ucscode/table-generator
Quick Example
use Ucscode\HtmlComponent\TableGenerator; use Ucscode\HtmlComponent\TableGenerator\Adapter\AssocArrayAdapter; $data = [ ['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com'], ['id' => 2, 'name' => 'Jane Smith', 'email' => 'jane@example.com'] ]; $adapter = new AssocArrayAdapter($data); $htmlTableGenerator = new TableGenerator($adapter); echo $htmlTableGenerator->render();
Table of Contents
Adapters
Adapters are responsible for structuring data into an HTML table. Each adapter converts a specific data format into table rows and columns.
Available Adapters
- CsvArrayAdapter — Converts a 2D array (CSV-like data) into a table.
- AssocArrayAdapter — Uses associative arrays where the keys become the table headers.
- MysqliResultAdapter — Transforms a MySQLi result object into a table.
Example: Using a MySQLi Adapter
use Ucscode\HtmlComponent\TableGenerator; use Ucscode\HtmlComponent\TableGenerator\Adapter\MysqliResultAdapter; $mysqli = new mysqli("localhost", "user", "password", "database"); $result = $mysqli->query("SELECT id, name, email FROM users"); $adapter = new MysqliResultAdapter($result); $htmlTableGenerator = new TableGenerator($adapter); echo $htmlTableGenerator->render();
Pagination
Pagination is integrated using ucscode/easy-paginator which allows only a subset of table rows is displayed per page.
Example: Paginating a CSV Table
use Ucscode\HtmlComponent\TableGenerator\Adapter\CsvArrayAdapter; use Ucscode\EasyPaginator\Paginator; $data = [...]; // Your CSV-like array data $paginator = new Paginator(0, 10, 2); // 10 items per page, on page 2 $adapter = new CsvArrayAdapter($data, $paginator); $htmlTableGenerator = new TableGenerator($adapter); echo $htmlTableGenerator->render();
Middleware
Middleware allows modification of table rows before rendering. This is useful when data sources cannot be directly modified (e.g., MySQL results).
Example: Adding an "Actions" Column
use Ucscode\HtmlComponent\TableGenerator\Abstraction\AbstractMiddleware; use Ucscode\HtmlComponent\TableGenerator\Component\Section\Tr; use Ucscode\HtmlComponent\TableGenerator\Component\Td; class ActionsMiddleware extends AbstractMiddleware { public function process(Table $table): Table { $table->getAttributes()->set('class', 'table table-striped'); $this->iterateTrsIn($table, function(Tr $tr) { $actions = new Td('<a href="#">Edit</a> | <a href="#">Delete</a>'); $tr->addCell($actions); }); return $table; } }
Apply middleware when creating the table:
$adapter = new MysqliResultAdapter($result); $htmlTableGenerator = new TableGenerator($adapter, new ActionsMiddleware()); echo $htmlTableGenerator->render();
Alternatively, apply middleware and regenerate the table structure
$htmlTableGenerator = new TableGenerator($adapter); $htmlTableGenerator->setMiddleWare(new ActionsMiddleware())->regenerate(); echo $htmlTableGenerator->render();
Table Components
Each part of the table (thead, tbody, tr, td, etc.) is an instance, allowing manipulation before rendering.
$table = new Table(); $thead = new Thead(); $tr = new Tr(); $tr->addCell(new Th("ID")); $tr->addCell(new Th("Name")); $thead->addTr($tr); $table->addThead($thead); echo $table->render();
Meta
The Meta
instance store metadata that neither affects table rendering nor appear in the HTML output.
$tr->getMeta()->set('shared-data', 'value');
Customization
Custom adapters can be created by implementing AdapterInterface
:
use Ucscode\HtmlComponent\TableGenerator\Contracts\AdapterInterface; class CustomAdapter implements AdapterInterface { public function getTheadTr(): Tr { ... } public function getTbodyTrCollection(): TrCollection { ... } public function getPaginator(): Paginator { ... } }
For more details, see the full documentation.
License
This package is licensed under the MIT License.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request.
If the project was helpful to you, please add a star