PHP code example of ucscode / table-generator

1. Go to this page and download the library: Download ucscode/table-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ucscode / table-generator example snippets


use Ucscode\HtmlComponent\TableGenerator;
use Ucscode\HtmlComponent\TableGenerator\Adapter\AssocArrayAdapter;

$data = [
    ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Jane Smith', 'email' => '[email protected]']
];

$adapter = new AssocArrayAdapter($data);
$htmlTableGenerator = new TableGenerator($adapter);

echo $htmlTableGenerator->render();

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();

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();

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

$adapter = new MysqliResultAdapter($result);
$htmlTableGenerator = new TableGenerator($adapter, new ActionsMiddleware());

echo $htmlTableGenerator->render();

$htmlTableGenerator = new TableGenerator($adapter);
$htmlTableGenerator->setMiddleWare(new ActionsMiddleware())->regenerate();

echo $htmlTableGenerator->render();

$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();

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

use Ucscode\HtmlComponent\TableGenerator\Contracts\AdapterInterface;

class CustomAdapter implements AdapterInterface 
{
    public function getTheadTr(): Tr { ... }
    public function getTbodyTrCollection(): TrCollection { ... }
    public function getPaginator(): Paginator { ... }
}