erguncaner / table
Very simple table generator for PHP
Installs: 2 510
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 0
Forks: 3
Open Issues: 0
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2025-06-09 03:57:02 UTC
README
Yet another very simple html table generator for PHP
composer require erguncaner/table dev-master
Generate a table structure as shown below
require_once __DIR__.'/vendor/autoload.php'; use erguncaner\Table\Table; use erguncaner\Table\TableColumn; use erguncaner\Table\TableRow; use erguncaner\Table\TableCell; // Sample data $posts = [ ['id'=>1, 'title'=>'Title 1'], ['id'=>2, 'title'=>'Title 2'], ['id'=>3, 'title'=>'Title 3'], ]; // First create a table $table = new Table([ 'id'=>'post-table' ]); // Create table columns with a column key and column object $table->addColumn('id', new TableColumn('ID', ['class'=>'id-column'])); $table->addColumn('title', new TableColumn('TITLE')); // Then add rows foreach($posts as $post){ // Associate cells with columns $cells = [ 'id' => new TableCell($post['id'], ['class'=>'id-cell']), 'title' => new TableCell($post['title']), ]; // define row attributes $attrs = [ 'id' => 'post-'.$post['id'] ]; $table->addRow(new TableRow($cells, $attrs)); } // Finally generate html $html = $table->html();
This will generate html below
<table id="post-table"> <thead> <tr><th class="id-column">ID</th><th>TITLE</th></tr> </thead> <tbody> <tr id="post-1"><td class="id-cell">1</td><td>Title 1</td></td> <tr id="post-2"><td class="id-cell">2</td><td>Title 2</td></td> <tr id="post-3"><td class="id-cell">3</td><td>Title 3</td></td> </tbody> <tfoot></tfoot> </table>