erguncaner / table
非常简单的PHP表格生成器
v0.1.1
2019-02-15 14:05 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-09-09 02:08:46 UTC
README
又一个非常简单的HTML表格生成器,用于PHP
composer require erguncaner/table dev-master
生成如下所示的表格结构
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();
这将生成以下HTML
<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>