PHPackages                             ucscode/table-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ucscode/table-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ucscode/table-generator
=======================

Lightweight PHP library that simplifies the process of creating tables with minimal code

3.2.3(1y ago)3106MITPHPPHP &gt;=8.1

Since Feb 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ucscode/table-generator)[ Packagist](https://packagist.org/packages/ucscode/table-generator)[ RSS](/packages/ucscode-table-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (8)Used By (0)

Table Generator
===============

[](#table-generator)

[![Latest Stable Version](https://camo.githubusercontent.com/841703d220af69fd4433e8776a68e750f0fb31f193144d77fd9bd21d03af4d34/68747470733a2f2f706f7365722e707567782e6f72672f756373636f64652f7461626c652d67656e657261746f722f762f737461626c65)](https://packagist.org/packages/ucscode/table-generator)[![License](https://camo.githubusercontent.com/1969bf582b9f7325c49af65bf23fc1d163dc398eb6379164fca94f7772e8e729/68747470733a2f2f706f7365722e707567782e6f72672f756373636f64652f7461626c652d67656e657261746f722f6c6963656e7365)](https://packagist.org/packages/ucscode/table-generator)[![Total Downloads](https://camo.githubusercontent.com/9ff7895852d3a1e4177a5e5c12f3889a50db154a6b666f6838bb71197ed9b259/68747470733a2f2f706f7365722e707567782e6f72672f756373636f64652f7461626c652d67656e657261746f722f646f776e6c6f616473)](https://packagist.org/packages/ucscode/table-generator)

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
--------

[](#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](https://github.com/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
------------

[](#installation)

Install via Composer:

```
composer require ucscode/table-generator
```

Quick Example
-------------

[](#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
-----------------

[](#table-of-contents)

1. [Adapters](#adapters)
2. [Pagination](#pagination)
3. [Middleware](#middleware)
4. [Table Components](#table-components)
5. [Meta](#meta)
6. [Customization](#customization)
7. [Interactive Example](#interactive-example)

Adapters
--------

[](#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

[](#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

[](#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)

Pagination is integrated using [ucscode/easy-paginator](https://github.com/ucscode/easy-paginator) which allows only a subset of table rows is displayed per page.

### Example: Paginating a CSV Table

[](#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)

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

[](#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('Edit | Delete');
            $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
----------------

[](#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
----

[](#meta)

The `Meta` instance store metadata that neither affects table rendering nor appear in the HTML output.

```
$tr->getMeta()->set('shared-data', 'value');
```

Customization
-------------

[](#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](http://tablegenerator.ucscode.com).

---

### License

[](#license)

This package is licensed under the MIT License.

### Contributing

[](#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

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance44

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

7

Last Release

442d ago

Major Versions

2.2.0 → 3.0.02025-02-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/65673b1b31e87471999a7614d107e7e061a38bf72191d149c66c1b943124e09c?d=identicon)[ucscode](/maintainers/ucscode)

---

Top Contributors

[![ucscode](https://avatars.githubusercontent.com/u/34024404?v=4)](https://github.com/ucscode "ucscode (45 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ucscode-table-generator/health.svg)

```
[![Health](https://phpackages.com/badges/ucscode-table-generator/health.svg)](https://phpackages.com/packages/ucscode-table-generator)
```

###  Alternatives

[spatie/laravel-menu

Html menu generator for Laravel

9812.8M10](/packages/spatie-laravel-menu)[spatie/laravel-flash

A lightweight package to flash messages

6631.8M16](/packages/spatie-laravel-flash)[symfony/ux-vue

Integration of Vue.js in Symfony

35834.3k4](/packages/symfony-ux-vue)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
